dev.marksman.collectionviews.ReverseVector 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;
class ReverseVector extends ConcreteVector implements NonEmptyVector {
private final NonEmptyVector underlying;
private ReverseVector(NonEmptyVector underlying) {
this.underlying = underlying;
}
@Override
public int size() {
return underlying.size();
}
@Override
public A unsafeGet(int index) {
if (index < 0 || index >= size()) {
throw new IndexOutOfBoundsException();
}
return underlying.unsafeGet(size() - 1 - index);
}
static NonEmptyVector reverseVector(NonEmptyVector underlying) {
if (underlying.size() < 2) {
return underlying;
} else if (underlying instanceof ReverseVector>) {
return ((ReverseVector) underlying).underlying;
} else {
return new ReverseVector<>(underlying);
}
}
}