All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.marksman.collectionviews.EmptyVector Maven / Gradle / Ivy

There is a newer version: 1.2.3
Show newest version
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 predicate) {
        Objects.requireNonNull(predicate);
        return nothing();
    }

    @Override
    public Maybe findIndex(Fn1 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 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 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 f) {
        return emptyVector();
    }

    @Override
    public Iterator iterator() {
        return emptyIterator();
    }

    @Override
    public Maybe> toNonEmpty() {
        return nothing();
    }

    @Override
    public ImmutableNonEmptyVector toNonEmptyOrThrow() {
        throw Vectors.nonEmptyError().apply();
    }

    @SuppressWarnings("unchecked")
    static  EmptyVector emptyVector() {
        return (EmptyVector) INSTANCE;
    }

}