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

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

There is a newer version: 1.2.3
Show newest version
package dev.marksman.collectionviews;

class ImmutableReverseVector extends ConcreteVector
        implements ImmutableNonEmptyVector {
    private final ImmutableNonEmptyVector underlying;

    private ImmutableReverseVector(ImmutableNonEmptyVector 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  ImmutableNonEmptyVector immutableReverseVector(ImmutableNonEmptyVector underlying) {
        if (underlying.size() < 2) {
            return underlying;
        } else if (underlying instanceof ImmutableReverseVector) {
            return ((ImmutableReverseVector) underlying).underlying;
        } else {
            return new ImmutableReverseVector<>(underlying);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy