dev.marksman.collectionviews.EmptyVector 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;
import com.jnape.palatable.lambda.adt.Maybe;
import com.jnape.palatable.lambda.adt.hlist.Tuple2;
import com.jnape.palatable.lambda.functions.Fn1;
import java.util.Iterator;
import java.util.Objects;
import static com.jnape.palatable.lambda.adt.Maybe.nothing;
import static dev.marksman.collectionviews.Validation.*;
import static java.util.Collections.emptyIterator;
final class EmptyVector extends ConcreteVector implements ImmutableVector, Primitive {
private static final EmptyVector> INSTANCE = new EmptyVector<>();
@Override
public int size() {
return 0;
}
@Override
public A unsafeGet(int index) {
throw new IndexOutOfBoundsException();
}
@Override
public Maybe find(Fn1 super A, ? extends Boolean> predicate) {
Objects.requireNonNull(predicate);
return nothing();
}
@Override
public Maybe findIndex(Fn1 super A, ? extends Boolean> predicate) {
Objects.requireNonNull(predicate);
return nothing();
}
@Override
public boolean isEmpty() {
return true;
}
@Override
public ImmutableVector drop(int count) {
validateDrop(count);
return this;
}
@Override
public ImmutableVector dropRight(int count) {
validateDrop(count);
return this;
}
@Override
public ImmutableVector dropWhile(Fn1 super A, ? extends Boolean> predicate) {
Objects.requireNonNull(predicate);
return this;
}
@Override
public ImmutableVector reverse() {
return this;
}
@Override
public ImmutableVector slice(int startIndex, int endIndexExclusive) {
validateSlice(startIndex, endIndexExclusive);
return this;
}
@Override
public ImmutableVector take(int count) {
validateTake(count);
return this;
}
@Override
public ImmutableVector takeRight(int count) {
validateTake(count);
return this;
}
@Override
public ImmutableVector takeWhile(Fn1 super A, ? extends Boolean> predicate) {
Objects.requireNonNull(predicate);
return this;
}
@Override
public ImmutableVector toImmutable() {
return this;
}
@Override
public ImmutableVector> zipWithIndex() {
return emptyVector();
}
@Override
public Maybe get(int index) {
return nothing();
}
@Override
public ImmutableVector fmap(Fn1 super A, ? extends B> f) {
return emptyVector();
}
@Override
public Iterator iterator() {
return emptyIterator();
}
@Override
public Maybe extends ImmutableNonEmptyVector> toNonEmpty() {
return nothing();
}
@Override
public ImmutableNonEmptyVector toNonEmptyOrThrow() {
throw Vectors.nonEmptyError().apply();
}
@SuppressWarnings("unchecked")
static EmptyVector emptyVector() {
return (EmptyVector) INSTANCE;
}
}