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

dev.marksman.collectionviews.ImmutableVectorZip 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 ImmutableVectorZip extends ConcreteVector
        implements ImmutableNonEmptyVector {

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

    private ImmutableVectorZip(Fn2 fn, ImmutableNonEmptyVector first, ImmutableNonEmptyVector 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  ImmutableVectorZip immutableVectorZip(Fn2 fn,
                                                                    ImmutableNonEmptyVector first, ImmutableNonEmptyVector second) {
        return new ImmutableVectorZip<>(fn, first, second);
    }

}