dev.marksman.collectionviews.ImmutableVectorZip 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 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);
}
}