123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- import { injectable } from 'inversify';
- import { IStorage } from '../interfaces/storages/IStorage';
- import { initializable } from '../decorators/Initializable';
- import { RandomGeneratorUtils } from '../utils/RandomGeneratorUtils';
- @injectable()
- export abstract class ArrayStorage <T> implements IStorage <T> {
- /**
- * @type {T[]}
- */
- @initializable()
- protected storage: T[];
- /**
- * @type {string}
- */
- @initializable()
- protected storageId: string;
- /**
- * @type {number}
- */
- private storageLength: number = 0;
- /**
- * @param key
- * @returns {T}
- */
- public get (key: number): T {
- const value: T | undefined = this.storage[key];
- if (!value) {
- throw new Error(`No value found in array storage with key \`${key}\``);
- }
- return value;
- }
- /**
- * @param value
- * @returns {number | null}
- */
- public getKeyOf (value: T): number | null {
- const key: number = this.storage.indexOf(value);
- return key >= 0 ? key : null;
- }
- /**
- * @returns {number}
- */
- public getLength (): number {
- return this.storageLength;
- }
- /**
- * @returns {T[]}
- */
- public getStorage (): T[] {
- return this.storage;
- }
- /**
- * @returns {string}
- */
- public getStorageId (): string {
- return this.storageId;
- }
- /**
- * @param args
- */
- public initialize (...args: any[]): void {
- this.storage = [];
- this.storageId = RandomGeneratorUtils.getRandomString(6);
- }
- /**
- * @param storage
- * @param mergeId
- */
- public mergeWith (storage: this, mergeId: boolean = false): void {
- this.storage = [...this.storage, ...storage.getStorage()];
- if (mergeId) {
- this.storageId = storage.getStorageId();
- }
- }
- /**
- * @param key
- * @param value
- */
- public set (key: number, value: T): void {
- if (key === this.storageLength) {
- this.storage.push(value);
- } else {
- this.storage.splice(key, 0, value);
- }
- this.storageLength++;
- }
- }
|