dev.marksman.collectionviews.VectorSlicing 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.Fn3;
import java.util.function.Supplier;
import static dev.marksman.collectionviews.Validation.validateDrop;
final class VectorSlicing {
private VectorSlicing() {
}
@SuppressWarnings("unchecked")
static > V sliceImpl(Fn3 factory, int sourceSize,
Supplier underlyingSupplier, int startIndex, int requestedSize) {
if (startIndex == 0 && requestedSize >= sourceSize) {
return underlyingSupplier.get();
} else if (startIndex >= sourceSize) {
return (V) Vectors.empty();
} else {
int available = Math.max(sourceSize - startIndex, 0);
int sliceSize = Math.min(available, requestedSize);
return factory.apply(startIndex, sliceSize, underlyingSupplier.get());
}
}
@SuppressWarnings("unchecked")
static > V dropImpl(Fn3 factory, int count, V source) {
validateDrop(count, source);
if (count == 0) {
return source;
}
int sourceSize = source.size();
if (count >= sourceSize) {
return (V) Vectors.empty();
}
return factory.apply(count, sourceSize - count, source);
}
}