dev.marksman.collectionviews.ImmutableVectorCons Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of collection-views Show documentation
Show all versions of collection-views Show documentation
Low overhead, protected views over Java collections
package dev.marksman.collectionviews;
final class ImmutableVectorCons extends ConcreteVector
implements ImmutableNonEmptyVector, Primitive {
private final A head;
private final ImmutableVector tail;
ImmutableVectorCons(A head, ImmutableVector tail) {
this.head = head;
this.tail = tail;
}
@Override
public A head() {
return head;
}
@Override
public int size() {
return 1 + tail.size();
}
@Override
public A unsafeGet(int index) {
if (index < 0 || index >= size()) {
throw new IndexOutOfBoundsException();
}
if (index == 0) {
return head;
} else {
return tail.unsafeGet(index - 1);
}
}
}