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

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

There is a newer version: 1.2.3
Show newest version
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);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy