|
| 1 | +/* |
| 2 | + * Scala (https://www.scala-lang.org) |
| 3 | + * |
| 4 | + * Copyright EPFL and Lightbend, Inc. |
| 5 | + * |
| 6 | + * Licensed under Apache License 2.0 |
| 7 | + * (http://www.apache.org/licenses/LICENSE-2.0). |
| 8 | + * |
| 9 | + * See the NOTICE file distributed with this work for |
| 10 | + * additional information regarding copyright ownership. |
| 11 | + */ |
| 12 | + |
| 13 | +package scala |
| 14 | +package collection |
| 15 | +package mutable |
| 16 | +import language.experimental.captureChecking |
| 17 | + |
| 18 | +private[mutable] trait CheckedIndexedSeqView[+A] extends IndexedSeqView[A] { |
| 19 | + this: CheckedIndexedSeqView[A]^ => |
| 20 | + |
| 21 | + protected val mutationCount: () => Int |
| 22 | + |
| 23 | + override def iterator: Iterator[A]^{this} = new CheckedIndexedSeqView.CheckedIterator(this, mutationCount()) |
| 24 | + override def reverseIterator: Iterator[A]^{this} = new CheckedIndexedSeqView.CheckedReverseIterator(this, mutationCount()) |
| 25 | + |
| 26 | + override def appended[B >: A](elem: B): IndexedSeqView[B]^{this} = new CheckedIndexedSeqView.Appended(this, elem)(mutationCount) |
| 27 | + override def prepended[B >: A](elem: B): IndexedSeqView[B]^{this} = new CheckedIndexedSeqView.Prepended(elem, this)(mutationCount) |
| 28 | + override def take(n: Int): IndexedSeqView[A]^{this} = new CheckedIndexedSeqView.Take(this, n)(mutationCount) |
| 29 | + override def takeRight(n: Int): IndexedSeqView[A]^{this} = new CheckedIndexedSeqView.TakeRight(this, n)(mutationCount) |
| 30 | + override def drop(n: Int): IndexedSeqView[A]^{this} = new CheckedIndexedSeqView.Drop(this, n)(mutationCount) |
| 31 | + override def dropRight(n: Int): IndexedSeqView[A]^{this} = new CheckedIndexedSeqView.DropRight(this, n)(mutationCount) |
| 32 | + override def map[B](f: A => B): IndexedSeqView[B]^{this, f} = new CheckedIndexedSeqView.Map(this, f)(mutationCount) |
| 33 | + override def reverse: IndexedSeqView[A]^{this} = new CheckedIndexedSeqView.Reverse(this)(mutationCount) |
| 34 | + override def slice(from: Int, until: Int): IndexedSeqView[A]^{this} = new CheckedIndexedSeqView.Slice(this, from, until)(mutationCount) |
| 35 | + override def tapEach[U](f: A => U): IndexedSeqView[A]^{this, f} = new CheckedIndexedSeqView.Map(this, { (a: A) => f(a); a})(mutationCount) |
| 36 | + |
| 37 | + override def concat[B >: A](suffix: IndexedSeqView.SomeIndexedSeqOps[B]): IndexedSeqView[B]^{this} = new CheckedIndexedSeqView.Concat(this, suffix)(mutationCount) |
| 38 | + override def appendedAll[B >: A](suffix: IndexedSeqView.SomeIndexedSeqOps[B]): IndexedSeqView[B]^{this} = new CheckedIndexedSeqView.Concat(this, suffix)(mutationCount) |
| 39 | + override def prependedAll[B >: A](prefix: IndexedSeqView.SomeIndexedSeqOps[B]): IndexedSeqView[B]^{this} = new CheckedIndexedSeqView.Concat(prefix, this)(mutationCount) |
| 40 | +} |
| 41 | + |
| 42 | +private[mutable] object CheckedIndexedSeqView { |
| 43 | + import IndexedSeqView.SomeIndexedSeqOps |
| 44 | + |
| 45 | + @SerialVersionUID(3L) |
| 46 | + private[mutable] class CheckedIterator[A](self: IndexedSeqView[A]^, mutationCount: => Int) |
| 47 | + extends IndexedSeqView.IndexedSeqViewIterator[A](self) { |
| 48 | + private[this] val expectedCount = mutationCount |
| 49 | + override def hasNext: Boolean = { |
| 50 | + MutationTracker.checkMutationsForIteration(expectedCount, mutationCount) |
| 51 | + super.hasNext |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @SerialVersionUID(3L) |
| 56 | + private[mutable] class CheckedReverseIterator[A](self: IndexedSeqView[A]^, mutationCount: => Int) |
| 57 | + extends IndexedSeqView.IndexedSeqViewReverseIterator[A](self) { |
| 58 | + private[this] val expectedCount = mutationCount |
| 59 | + override def hasNext: Boolean = { |
| 60 | + MutationTracker.checkMutationsForIteration(expectedCount, mutationCount) |
| 61 | + super.hasNext |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @SerialVersionUID(3L) |
| 66 | + class Id[+A](underlying: SomeIndexedSeqOps[A]^)(protected val mutationCount: () => Int) |
| 67 | + extends IndexedSeqView.Id(underlying) with CheckedIndexedSeqView[A] |
| 68 | + |
| 69 | + @SerialVersionUID(3L) |
| 70 | + class Appended[+A](underlying: SomeIndexedSeqOps[A]^, elem: A)(protected val mutationCount: () => Int) |
| 71 | + extends IndexedSeqView.Appended(underlying, elem) with CheckedIndexedSeqView[A] |
| 72 | + |
| 73 | + @SerialVersionUID(3L) |
| 74 | + class Prepended[+A](elem: A, underlying: SomeIndexedSeqOps[A]^)(protected val mutationCount: () => Int) |
| 75 | + extends IndexedSeqView.Prepended(elem, underlying) with CheckedIndexedSeqView[A] |
| 76 | + |
| 77 | + @SerialVersionUID(3L) |
| 78 | + class Concat[A](prefix: SomeIndexedSeqOps[A]^, suffix: SomeIndexedSeqOps[A]^)(protected val mutationCount: () => Int) |
| 79 | + extends IndexedSeqView.Concat[A](prefix, suffix) with CheckedIndexedSeqView[A] |
| 80 | + |
| 81 | + @SerialVersionUID(3L) |
| 82 | + class Take[A](underlying: SomeIndexedSeqOps[A]^, n: Int)(protected val mutationCount: () => Int) |
| 83 | + extends IndexedSeqView.Take(underlying, n) with CheckedIndexedSeqView[A] |
| 84 | + |
| 85 | + @SerialVersionUID(3L) |
| 86 | + class TakeRight[A](underlying: SomeIndexedSeqOps[A]^, n: Int)(protected val mutationCount: () => Int) |
| 87 | + extends IndexedSeqView.TakeRight(underlying, n) with CheckedIndexedSeqView[A] |
| 88 | + |
| 89 | + @SerialVersionUID(3L) |
| 90 | + class Drop[A](underlying: SomeIndexedSeqOps[A]^, n: Int)(protected val mutationCount: () => Int) |
| 91 | + extends IndexedSeqView.Drop[A](underlying, n) with CheckedIndexedSeqView[A] |
| 92 | + |
| 93 | + @SerialVersionUID(3L) |
| 94 | + class DropRight[A](underlying: SomeIndexedSeqOps[A]^, n: Int)(protected val mutationCount: () => Int) |
| 95 | + extends IndexedSeqView.DropRight[A](underlying, n) with CheckedIndexedSeqView[A] |
| 96 | + |
| 97 | + @SerialVersionUID(3L) |
| 98 | + class Map[A, B](underlying: SomeIndexedSeqOps[A]^, f: A => B)(protected val mutationCount: () => Int) |
| 99 | + extends IndexedSeqView.Map(underlying, f) with CheckedIndexedSeqView[B] |
| 100 | + |
| 101 | + @SerialVersionUID(3L) |
| 102 | + class Reverse[A](underlying: SomeIndexedSeqOps[A]^)(protected val mutationCount: () => Int) |
| 103 | + extends IndexedSeqView.Reverse[A](underlying) with CheckedIndexedSeqView[A] { |
| 104 | + override def reverse: IndexedSeqView[A] = underlying match { |
| 105 | + case x: IndexedSeqView[A] => x |
| 106 | + case _ => super.reverse |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @SerialVersionUID(3L) |
| 111 | + class Slice[A](underlying: SomeIndexedSeqOps[A]^, from: Int, until: Int)(protected val mutationCount: () => Int) |
| 112 | + extends AbstractIndexedSeqView[A] with CheckedIndexedSeqView[A] { |
| 113 | + protected val lo = from max 0 |
| 114 | + protected val hi = (until max 0) min underlying.length |
| 115 | + protected val len = (hi - lo) max 0 |
| 116 | + @throws[IndexOutOfBoundsException] |
| 117 | + def apply(i: Int): A = underlying(lo + i) |
| 118 | + def length: Int = len |
| 119 | + } |
| 120 | +} |
0 commit comments