import { List } from './List'; import { Stream } from './stream'; import { Collection } from './Collection'; import { isNullOrUndefined } from './Core'; import { ObjectStream } from './stream'; export class ArrayList 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 { if (!isNullOrUndefined(e)) { this.values.push(e); return true; } else { return false; } } public addAll(col: Collection): boolean { if (!isNullOrUndefined(col)) { if (col.stream().allMatch((each) => !isNullOrUndefined(each))) { this.values.push(...col.toArray()); return true; } else { return false; } } else { return false; } } public clear(): void { this.values = []; } 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 { return this.removeIf((each) => this.equalizer(e, each)); } public removeAll(col: Collection): boolean { return this.removeIf((each) => col.stream().anyMatch((en) => this.equalizer(en, each))); } /** * 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 { const initSize = this.size(); this.values = this.values.filter((each) => !predicate(each)); return this.size() !== initSize; } public retainAll(col: Collection): boolean { return this.removeIf((each) => col.stream().noneMatch((en) => this.equalizer(en, each))); } 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 { return this.removeIf((each) => this.values.findIndex((e) => this.equalizer(e, each)) === index); } public set(index: number, e: E): E { if (this.values.length - 1 <= index && index >= 0) { return this.values[index] = e; } else { throw new Error('List out of bounds for index=[' + index + ']'); } } public subList(fromIndex: number, toIndex: number): List { return new ArrayList(this.values.splice(fromIndex, toIndex)); } public sort(comparator: (c1: E, c2: E) => number): void { this.values = this.values.sort(comparator); } public replaceAll(predicate: (c1: E) => boolean, fun: () => E): void { for (let index = 0; index < this.values.length; index++) { if (predicate(this.values[index])) { this.values[index] = fun(); } } } public [Symbol.iterator](): Iterator { return this.values[Symbol.iterator](); } }