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

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

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

import com.jnape.palatable.lambda.functions.Fn2;

final class VectorZip extends ConcreteVector
        implements NonEmptyVector {

    private final Fn2 fn;
    private final NonEmptyVector first;
    private final NonEmptyVector second;
    private final int size;

    private VectorZip(Fn2 fn, NonEmptyVector first, NonEmptyVector second) {
        this.fn = fn;
        this.first = first;
        this.second = second;
        this.size = Math.min(first.size(), second.size());
    }

    @Override
    public C head() {
        return unsafeGet(0);
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public C unsafeGet(int index) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException();
        }
        return fn.apply(first.unsafeGet(index), second.unsafeGet(index));
    }

    static  VectorZip vectorZip(Fn2 fn, NonEmptyVector first, NonEmptyVector second) {
        return new VectorZip<>(fn, first, second);
    }

}