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

net.cassite.f.MList Maven / Gradle / Ivy

package net.cassite.f;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collector;

@JsonFormat(shape = JsonFormat.Shape.ARRAY)
@JsonDeserialize(using = MListDeserializer.class)
public interface MList extends List, AsTransformable> {
    static  Collector> collector() {
        return MListCollector.collector();
    }

    static  Collector> mutableCollector() {
        return MutableMListCollector.collector();
    }

    static  MList modifiable() {
        return new SimpleMutableMListImpl<>();
    }

    static  MList modifiable(@NotNull Collection c) {
        if (c == null)
            throw new NullPointerException();
        MList ls = new SimpleMutableMListImpl<>();
        ls.addAll(c);
        return ls;
    }

    @SafeVarargs
    static  MList modifiable(@NotNull E... es) {
        if (es == null)
            throw new NullPointerException();
        return modifiable(Arrays.asList(es));
    }

    static  MList unit() {
        return new SimpleMutableMListImpl().immutable();
    }

    static  MList unit(@NotNull Collection c) {
        if (c == null)
            throw new NullPointerException();
        if (c instanceof MList) {
            //noinspection unchecked
            return ((MList) c).immutable();
        }
        return new SimpleMutableMListImpl(c).immutable();
    }

    @SafeVarargs
    static  MList unit(@NotNull E... es) {
        if (es == null)
            throw new NullPointerException();
        return unit(Arrays.asList(es)).immutable();
    }

    default MList immutable() {
        if (this instanceof Immutable) {
            return this;
        }
        return new ImmutableMListImpl<>(this);
    }

    default MList mutable() {
        return new SimpleMutableMListImpl<>(this);
    }

    default E head() {
        if (isEmpty())
            throw new IndexOutOfBoundsException("list is empty");
        return get(0);
    }

    default MList tail() {
        return new TailMListImpl<>(this.immutable());
    }

    default E last() {
        if (isEmpty())
            throw new IndexOutOfBoundsException("list is empty");
        return get(size() - 1);
    }

    default MList init() {
        return new InitMListImpl<>(this.immutable());
    }

    default  MList map(@NotNull Function mapper) {
        if (mapper == null)
            throw new NullPointerException();
        return new LazyMListImpl<>(this.immutable(), (ls, u) -> ls.add(mapper.apply(u)));
    }

    default  MList flatMap(@NotNull Function> mapper) {
        if (mapper == null)
            throw new NullPointerException();
        return new LazyMListImpl<>(this.immutable(), (ls, u) -> ls.addAll(mapper.apply(u)));
    }

    default MList filter(@NotNull Predicate predicate) {
        if (predicate == null)
            throw new NullPointerException();
        return new LazyMListImpl<>(this.immutable(), (ls, u) -> {
            if (predicate.test(u)) {
                ls.add(u);
            }
        });
    }

    @Override
    MList subList(int from, int to);
}