fix(simulator): detect string emptyness properly
[e-mobility-charging-stations-simulator.git] / src / charging-station / SharedLRUCache.ts
1 import LRUCache from 'mnemonist/lru-map-with-delete';
2
3 import { Bootstrap } from '../internal';
4 import type { ChargingStationConfiguration } from '../types/ChargingStationConfiguration';
5 import type { ChargingStationTemplate } from '../types/ChargingStationTemplate';
6 import Utils from '../utils/Utils';
7
8 enum CacheType {
9 CHARGING_STATION_TEMPLATE = 'chargingStationTemplate',
10 CHARGING_STATION_CONFIGURATION = 'chargingStationConfiguration',
11 }
12
13 type CacheableType = ChargingStationTemplate | ChargingStationConfiguration;
14
15 export default class SharedLRUCache {
16 private static instance: SharedLRUCache | null = null;
17 private readonly lruCache: LRUCache<string, CacheableType>;
18
19 private constructor() {
20 this.lruCache = new LRUCache<string, CacheableType>(
21 Bootstrap.getInstance().numberOfChargingStationTemplates +
22 Bootstrap.getInstance().numberOfChargingStations
23 );
24 }
25
26 public static getInstance(): SharedLRUCache {
27 if (SharedLRUCache.instance === null) {
28 SharedLRUCache.instance = new SharedLRUCache();
29 }
30 return SharedLRUCache.instance;
31 }
32
33 public hasChargingStationConfiguration(chargingStationConfigurationHash: string): boolean {
34 return this.has(this.getChargingStationConfigurationKey(chargingStationConfigurationHash));
35 }
36
37 public setChargingStationConfiguration(
38 chargingStationConfiguration: ChargingStationConfiguration
39 ): void {
40 if (this.isChargingStationConfigurationCacheable(chargingStationConfiguration)) {
41 this.set(
42 this.getChargingStationConfigurationKey(chargingStationConfiguration.configurationHash),
43 chargingStationConfiguration
44 );
45 }
46 }
47
48 public getChargingStationConfiguration(
49 chargingStationConfigurationHash: string
50 ): ChargingStationConfiguration {
51 return this.get(
52 this.getChargingStationConfigurationKey(chargingStationConfigurationHash)
53 ) as ChargingStationConfiguration;
54 }
55
56 public deleteChargingStationConfiguration(chargingStationConfigurationHash: string): void {
57 this.delete(this.getChargingStationConfigurationKey(chargingStationConfigurationHash));
58 }
59
60 public hasChargingStationTemplate(chargingStationTemplateHash: string): boolean {
61 return this.has(this.getChargingStationTemplateKey(chargingStationTemplateHash));
62 }
63
64 public setChargingStationTemplate(chargingStationTemplate: ChargingStationTemplate): void {
65 this.set(
66 this.getChargingStationTemplateKey(chargingStationTemplate.templateHash),
67 chargingStationTemplate
68 );
69 }
70
71 public getChargingStationTemplate(chargingStationTemplateHash: string): ChargingStationTemplate {
72 return this.get(
73 this.getChargingStationTemplateKey(chargingStationTemplateHash)
74 ) as ChargingStationTemplate;
75 }
76
77 public deleteChargingStationTemplate(chargingStationTemplateHash: string): void {
78 this.delete(this.getChargingStationTemplateKey(chargingStationTemplateHash));
79 }
80
81 public clear(): void {
82 this.lruCache.clear();
83 }
84
85 private getChargingStationConfigurationKey(hash: string): string {
86 return `${CacheType.CHARGING_STATION_CONFIGURATION}${hash}`;
87 }
88
89 private getChargingStationTemplateKey(hash: string): string {
90 return `${CacheType.CHARGING_STATION_TEMPLATE}${hash}`;
91 }
92
93 private has(key: string): boolean {
94 return this.lruCache.has(key);
95 }
96
97 private get(key: string): CacheableType | undefined {
98 return this.lruCache.get(key);
99 }
100
101 private set(key: string, value: CacheableType): void {
102 this.lruCache.set(key, value);
103 }
104
105 private delete(key: string): void {
106 this.lruCache.delete(key);
107 }
108
109 private isChargingStationConfigurationCacheable(
110 chargingStationConfiguration: ChargingStationConfiguration
111 ): boolean {
112 return (
113 Utils.isNullOrUndefined(chargingStationConfiguration?.configurationKey) === false &&
114 Utils.isNullOrUndefined(chargingStationConfiguration?.stationInfo) === false &&
115 Utils.isNullOrUndefined(chargingStationConfiguration?.configurationHash) === false &&
116 Utils.isEmptyArray(chargingStationConfiguration?.configurationKey) === false &&
117 Utils.isEmptyObject(chargingStationConfiguration?.stationInfo) === false &&
118 Utils.isNotEmptyString(chargingStationConfiguration?.configurationHash) === true
119 );
120 }
121 }