fix: brown paper bag issue at referencing the same literal object instance
[e-mobility-charging-stations-simulator.git] / src / charging-station / SharedLRUCache.ts
index 01e18734b673295004a3c033a5db37bd382d6c58..b58dc79ee6a9509c3fc5a79bf4605487b500d174 100644 (file)
@@ -1,22 +1,25 @@
-import LRUCache from 'mnemonist/lru-map-with-delete';
+import LRUCache from 'mnemonist/lru-map-with-delete.js';
 
-import type ChargingStationConfiguration from '../types/ChargingStationConfiguration';
-import type ChargingStationTemplate from '../types/ChargingStationTemplate';
-import Utils from '../utils/Utils';
+import { Bootstrap } from './internal';
+import type { ChargingStationConfiguration, ChargingStationTemplate } from '../types';
+import { Utils } from '../utils';
 
 enum CacheType {
-  CHARGING_STATION_TEMPLATE = 'chargingStationTemplate',
-  CHARGING_STATION_CONFIGURATION = 'chargingStationConfiguration',
+  chargingStationTemplate = 'chargingStationTemplate',
+  chargingStationConfiguration = 'chargingStationConfiguration',
 }
 
 type CacheableType = ChargingStationTemplate | ChargingStationConfiguration;
 
-export default class SharedLRUCache {
+export class SharedLRUCache {
   private static instance: SharedLRUCache | null = null;
   private readonly lruCache: LRUCache<string, CacheableType>;
 
   private constructor() {
-    this.lruCache = new LRUCache<string, CacheableType>(1000);
+    this.lruCache = new LRUCache<string, CacheableType>(
+      Bootstrap.getInstance().numberOfChargingStationTemplates +
+        Bootstrap.getInstance().numberOfChargingStations
+    );
   }
 
   public static getInstance(): SharedLRUCache {
@@ -79,18 +82,18 @@ export default class SharedLRUCache {
   }
 
   private getChargingStationConfigurationKey(hash: string): string {
-    return CacheType.CHARGING_STATION_CONFIGURATION + hash;
+    return `${CacheType.chargingStationConfiguration}${hash}`;
   }
 
   private getChargingStationTemplateKey(hash: string): string {
-    return CacheType.CHARGING_STATION_TEMPLATE + hash;
+    return `${CacheType.chargingStationTemplate}${hash}`;
   }
 
   private has(key: string): boolean {
     return this.lruCache.has(key);
   }
 
-  private get(key: string): CacheableType {
+  private get(key: string): CacheableType | undefined {
     return this.lruCache.get(key);
   }
 
@@ -106,12 +109,12 @@ export default class SharedLRUCache {
     chargingStationConfiguration: ChargingStationConfiguration
   ): boolean {
     return (
-      !Utils.isNullOrUndefined(chargingStationConfiguration?.configurationKey) &&
-      !Utils.isNullOrUndefined(chargingStationConfiguration?.stationInfo) &&
-      !Utils.isNullOrUndefined(chargingStationConfiguration?.configurationHash) &&
-      !Utils.isEmptyArray(chargingStationConfiguration?.configurationKey) &&
-      !Utils.isEmptyObject(chargingStationConfiguration?.stationInfo) &&
-      !Utils.isEmptyString(chargingStationConfiguration?.configurationHash)
+      Utils.isNullOrUndefined(chargingStationConfiguration?.configurationKey) === false &&
+      Utils.isNullOrUndefined(chargingStationConfiguration?.stationInfo) === false &&
+      Utils.isNullOrUndefined(chargingStationConfiguration?.configurationHash) === false &&
+      Utils.isNotEmptyArray(chargingStationConfiguration?.configurationKey) === true &&
+      Utils.isEmptyObject(chargingStationConfiguration?.stationInfo) === false &&
+      Utils.isNotEmptyString(chargingStationConfiguration?.configurationHash) === true
     );
   }
 }