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

org.reactfx.collection.MaterializedListModification Maven / Gradle / Ivy

There is a newer version: 1.11
Show newest version
package org.reactfx.collection;

import java.util.List;

import org.reactfx.util.Lists;

public interface MaterializedListModification extends ListModificationLike {

    /**
     * Doesn't create defensive copies of the passed lists.
     * Therefore, they must not be modified later.
     */
    static  MaterializedListModification create(int pos, List removed, List added) {
        return new MaterializedListModificationImpl(pos, removed, added);
    }

    List getAdded();

    @Override
    default int getAddedSize() { return getAdded().size(); }

    default MaterializedListModification trim() {
        return Lists.commonPrefixSuffixLengths(getRemoved(), getAdded()).map((pref, suff) -> {
            if(pref == 0 && suff == 0) {
                return this;
            } else {
                return create(
                        getFrom() + pref,
                        getRemoved().subList(pref, getRemovedSize() - suff),
                        getAdded().subList(pref, getAddedSize() - suff));
            }
        });
    }
}

final class MaterializedListModificationImpl
implements MaterializedListModification {
    private final int from;
    private final List removed;
    private final List added;

    MaterializedListModificationImpl(
            int from, List removed, List added) {
        this.from = from;
        this.removed = removed;
        this.added = added;
    }

    @Override public int getFrom() { return from; }
    @Override public List getRemoved() { return removed; }
    @Override public List getAdded() { return added; }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy