dev.marksman.collectionviews.VectorZip Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of collection-views Show documentation
Show all versions of collection-views Show documentation
Low overhead, protected views over Java collections
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);
}
}