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

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

There is a newer version: 1.2.3
Show newest version
package dev.marksman.collectionviews;

import com.jnape.palatable.lambda.functions.Fn1;

final class MappedVector extends ConcreteVector implements NonEmptyVector {
    private final MapperChain mapper;
    private final NonEmptyVector underlying;

    MappedVector(MapperChain mapper, NonEmptyVector underlying) {
        this.mapper = mapper;
        this.underlying = underlying;
    }

    @Override
    public int size() {
        return underlying.size();
    }

    @SuppressWarnings("unchecked")
    @Override
    public A unsafeGet(int index) {
        return (A) mapper.apply(underlying.unsafeGet(index));
    }

    @SuppressWarnings("unchecked")
    @Override
    public  NonEmptyVector fmap(Fn1 f) {
        return new MappedVector<>(mapper.add((Fn1) f),
                underlying);
    }
}