All files / src/internal/views/read-only read-only-collection.ts

100% Statements 44/44
100% Branches 12/12
100% Functions 28/28
100% Lines 40/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177                                            9042x                                 9042x 9042x 9042x 9042x 9042x         83x       1224x       958x       558x       22x       1009x 1009x       217x       156x 156x 8x   156x       19x 19x 43x         78x       100x       130x       100x       115x       100x       100x       131x                           4389x         13x       33x 21x       25x 19x       31x               6x       6x       6x       6x      
// Read-only collections that compare by identity, because their family has no value equality:
// an unmodifiable collection or sequenced collection, and a map's value view. Reads, iteration,
// `forEach` and cursors go to the source, and members can be mapped on the way out (a map's
// entries become read-only entries); every change is refused before its arguments are looked at.
 
import type { Cursor, ReadonlyCollection } from '../../../interfaces/collection'
import type { ReadonlySequencedCollection, SequencedCollection } from '../../../interfaces/list'
import { SEQUENCED_COLLECTION_BRAND } from '../../brand'
import { apply } from '../../intrinsics'
import { AbstractCollection } from '../../skeletons/abstract-collection'
import { ReadOnlyCursor } from './read-only-cursor'
import { empty, refuse } from './refuse'
import { type ReadOnlyKind, registerReadOnly } from './registry'
import { hideSource, ReadOnlyIterator, SOURCE } from './source-key'
 
/**
 * A read-only view of `source`, printed with `tag`, equal only to itself. With `reveal`, each
 * member is passed through it before it is handed out.
 *
 * @internal
 */
export class ReadOnlyCollection<E> extends AbstractCollection<E> {
  protected readonly iterationMode = 'live' as const
  declare readonly [SOURCE]: ReadonlyCollection<E>
  private readonly tag: string
  protected readonly reveal: ((member: E) => E) | undefined
 
  /**
   * @param kind - The family the view registers as; each library subclass passes its own.
   * @param exact - The class whose instances register as `kind`; an instance of any other class,
   *   such as a subclass defined outside the library, is not registered.
   */
  constructor(
    source: ReadonlyCollection<E>,
    tag: string,
    reveal?: (member: E) => E,
    kind: ReadOnlyKind = 'collection',
    exact: unknown = ReadOnlyCollection,
  ) {
    super()
    hideSource(this, source)
    this.tag = tag
    this.reveal = reveal
    registerReadOnly(this, new.target === exact ? kind : undefined, source, tag, reveal)
  }
 
  /** `member` as handed out: through `reveal` when there is one. */
  protected shown(member: E): E {
    return this.reveal === undefined ? member : this.reveal(member)
  }
 
  get [Symbol.toStringTag](): string {
    return this.tag
  }
 
  get size(): number {
    return this[SOURCE].size
  }
 
  override get revision(): number {
    return this[SOURCE].revision
  }
 
  override isEmpty(): boolean {
    return this[SOURCE].isEmpty()
  }
 
  [Symbol.iterator](): IterableIterator<E> {
    const members = this[SOURCE][Symbol.iterator]()
    return new ReadOnlyIterator(members, this.reveal)
  }
 
  override has(element: E): boolean {
    return this[SOURCE].has(element)
  }
 
  override toArray(): E[] {
    const members = this[SOURCE].toArray()
    if (this.reveal !== undefined) {
      for (let i = 0; i < members.length; i++) members[i] = this.reveal(members[i] as E)
    }
    return members
  }
 
  override forEach(action: (element: E) => void, thisArg?: unknown): void {
    this.requireFunction(action, 'forEach', 'action')
    this[SOURCE].forEach((element) => {
      apply(action, thisArg, [this.shown(element)])
    })
  }
 
  cursor(): Cursor<E> {
    return new ReadOnlyCursor(this[SOURCE].cursor(), `${this.className}.cursor`, this.reveal)
  }
 
  add(_element: E): this {
    throw refuse(`${this.className}.add`)
  }
 
  override addAll(_elements: Iterable<E>): boolean {
    throw refuse(`${this.className}.addAll`)
  }
 
  override delete(_element: E): boolean {
    throw refuse(`${this.className}.delete`)
  }
 
  override removeAll(_elements: Iterable<E>): boolean {
    throw refuse(`${this.className}.removeAll`)
  }
 
  override retainAll(_elements: Iterable<E>): boolean {
    throw refuse(`${this.className}.retainAll`)
  }
 
  override removeIf(_predicate: (element: E) => unknown): boolean {
    throw refuse(`${this.className}.removeIf`)
  }
 
  override clear(): void {
    throw refuse(`${this.className}.clear`)
  }
}
 
/**
 * A read-only view of a sequenced collection: the ends and a reversed view, equal only to itself.
 *
 * @internal
 */
export class ReadOnlySequencedCollection<E>
  extends ReadOnlyCollection<E>
  implements SequencedCollection<E>
{
  constructor(source: ReadonlySequencedCollection<E>, tag: string, reveal?: (member: E) => E) {
    super(source, tag, reveal, 'sequenced-collection', ReadOnlySequencedCollection)
  }
 
  /** Marks every sequenced collection for `isSequencedCollection`, across loaded copies. */
  get [SEQUENCED_COLLECTION_BRAND](): true {
    return true
  }
 
  getFirst(): E {
    if (this[SOURCE].isEmpty()) throw empty(`${this.className}.getFirst`)
    return this.shown((this[SOURCE] as ReadonlySequencedCollection<E>).getFirst())
  }
 
  getLast(): E {
    if (this[SOURCE].isEmpty()) throw empty(`${this.className}.getLast`)
    return this.shown((this[SOURCE] as ReadonlySequencedCollection<E>).getLast())
  }
 
  reversed(): SequencedCollection<E> {
    return new ReadOnlySequencedCollection(
      (this[SOURCE] as ReadonlySequencedCollection<E>).reversed(),
      this.className,
      this.reveal,
    )
  }
 
  addFirst(_element: E): this {
    throw refuse(`${this.className}.addFirst`)
  }
 
  addLast(_element: E): this {
    throw refuse(`${this.className}.addLast`)
  }
 
  removeFirst(): E {
    throw refuse(`${this.className}.removeFirst`)
  }
 
  removeLast(): E {
    throw refuse(`${this.className}.removeLast`)
  }
}