import { List } from './List'; import { Stream } from './stream'; import { Collection } from './Collection'; import { isNullOrUndefined } from './Core'; import { ObjectStream } from './stream'; export class ImmutableArrayList implements List { private values: E[]; private equalizer: (c1: any, c2: any) => boolean; public constructor(values?: E[], equalizer?: (c1: any, c2: any) => boolean) { this.values = values === undefined ? [] : values; this.equalizer = equalizer === undefined ? (c1, c2) => c1 === c2 : equalizer; } public add(e: E): boolean { throw new Error('Unsupported Operation!'); } public addAll(col: Collection): boolean { throw new Error('Unsupported Operation!'); } public clear(): void { throw new Error('Unsupported Operation!'); } public contains(e: E): boolean { return this.values.some((each) => this.equalizer(e, each)); } public containsAll(col: Collection): boolean { return !isNullOrUndefined(col) ? col.toArray().every((each) => this.contains(each)) : false; } public isEmpty(): boolean { return this.size() === 0; } public remove(e: any): boolean { throw new Error('Unsupported Operation!'); } public removeAll(col: Collection): boolean { throw new Error('Unsupported Operation!'); } /** * ESSENCIAL TO REMOVE METHODS * @param predicate to be used when determining if a element is to be remove */ public removeIf(predicate: (e: E) => boolean): boolean { throw new Error('Unsupported Operation!'); } public retainAll(col: Collection): boolean { throw new Error('Unsupported Operation!'); } public size(): number { return this.values.length; } public stream(): Stream { return ObjectStream.of(this.values); } public toArray(): E[] { return [...this.values]; } public forEach(consumer: (c: E) => void): void { for (const value of this) { consumer(value); } } public indexOf(e: E): number { return this.values.findIndex((each: E) => this.equalizer(e, each)); } public get(index: number): E { return this.values[index]; } public lastIndexOf(e: E): number { return this.values.filter((each) => this.equalizer(e, each)).length - 1; } public removeIfIndex(index: number): boolean { throw new Error('Unsupported Operation!'); } public set(index: number, e: E): E { throw new Error('Unsupported Operation!'); } public subList(fromIndex: number, toIndex: number): List { throw new Error('Unsupported Operation!'); } public sort(comparator: (c1: E, c2: E) => number): void { throw new Error('Unsupported Operation!'); } public replaceAll(predicate: (c1: E) => boolean, fun: () => E): void { throw new Error('Unsupported Operation!'); } public [Symbol.iterator](): Iterator { return this.values[Symbol.iterator](); } }