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

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

There is a newer version: 1.2.3
Show newest version
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);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy