ArrayStorage.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { inject, injectable, postConstruct } from 'inversify';
  2. import { ServiceIdentifiers } from '../container/ServiceIdentifiers';
  3. import { IArrayStorage } from '../interfaces/storages/IArrayStorage';
  4. import { IOptions } from '../interfaces/options/IOptions';
  5. import { IRandomGenerator } from '../interfaces/utils/IRandomGenerator';
  6. import { initializable } from '../decorators/Initializable';
  7. @injectable()
  8. export abstract class ArrayStorage <V> implements IArrayStorage <V> {
  9. /**
  10. * @type {V[]}
  11. */
  12. @initializable()
  13. protected storage!: V[];
  14. /**
  15. * @type {string}
  16. */
  17. @initializable()
  18. protected storageId!: string;
  19. /**
  20. * @type {IRandomGenerator}
  21. */
  22. protected readonly randomGenerator: IRandomGenerator;
  23. /**
  24. * @type {IOptions}
  25. */
  26. protected readonly options: IOptions;
  27. /**
  28. * @type {number}
  29. */
  30. private storageLength: number = 0;
  31. /**
  32. * @param {IRandomGenerator} randomGenerator
  33. * @param {IOptions} options
  34. */
  35. protected constructor (
  36. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  37. @inject(ServiceIdentifiers.IOptions) options: IOptions
  38. ) {
  39. this.randomGenerator = randomGenerator;
  40. this.options = options;
  41. }
  42. @postConstruct()
  43. public initialize (): void {
  44. this.storage = [];
  45. this.storageId = this.randomGenerator.getRandomString(6);
  46. }
  47. /**
  48. * @param {number} key
  49. * @returns {V | undefined}
  50. */
  51. public get (key: number): V | undefined {
  52. return this.storage[key];
  53. }
  54. /**
  55. * @param {number} key
  56. * @returns {V}
  57. */
  58. public getOrThrow (key: number): V {
  59. const value: V | undefined = this.get(key);
  60. if (!value) {
  61. throw new Error(`No value found in array storage with key \`${key}\``);
  62. }
  63. return value;
  64. }
  65. /**
  66. * @param {V} value
  67. * @returns {number}
  68. */
  69. public getKeyOf (value: V): number | null {
  70. const key: number = this.storage.indexOf(value);
  71. return key >= 0 ? key : null;
  72. }
  73. /**
  74. * @returns {number}
  75. */
  76. public getLength (): number {
  77. return this.storageLength;
  78. }
  79. /**
  80. * @returns {V[]}
  81. */
  82. public getStorage (): V[] {
  83. return this.storage;
  84. }
  85. /**
  86. * @returns {string}
  87. */
  88. public getStorageId (): string {
  89. return this.storageId;
  90. }
  91. /**
  92. * @param {this} storage
  93. * @param {boolean} mergeId
  94. */
  95. public mergeWith (storage: this, mergeId: boolean = false): void {
  96. this.storage = [...this.storage, ...storage.getStorage()];
  97. if (mergeId) {
  98. this.storageId = storage.getStorageId();
  99. }
  100. }
  101. /**
  102. * @param {number} key
  103. * @param {V} value
  104. */
  105. public set (key: number, value: V): void {
  106. if (key === this.storageLength) {
  107. this.storage.push(value);
  108. } else {
  109. this.storage.splice(key, 0, value);
  110. }
  111. this.storageLength++;
  112. }
  113. }