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

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

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

import java.util.ArrayList;
import java.util.List;

import javafx.collections.ObservableList;

import org.reactfx.util.Lists;

public interface ListModification extends ListModificationLike {

    List getAddedSubList();

    ListModification trim();

    default MaterializedListModification materialize() {
        return MaterializedListModification.create(
                getFrom(),
                getRemoved(),
                new ArrayList<>(getAddedSubList()));
    }
}

final class ListModificationImpl
implements ListModification {
    private int position;
    private List removed;
    private int addedSize;
    private final ObservableList list;

    ListModificationImpl(
            int position,
            List removed,
            int addedSize,
            ObservableList list) {
        this.position = position;
        this.removed = removed;
        this.addedSize = addedSize;
        this.list = list;
    }

    @Override
    public int getFrom() {
        return position;
    }

    @Override
    public List getRemoved() {
        return removed;
    }

    @Override
    public List getAddedSubList() {
        return list.subList(position, position + addedSize);
    }

    @Override
    public int getAddedSize() {
        return addedSize;
    }

    @Override
    public String toString() {
        return "[modification at: " + getFrom() +
                ", removed: " + getRemoved() +
                ", added size: " + getAddedSize() + "]";
    }

    @Override
    public ListModification trim()  {
        return Lists.commonPrefixSuffixLengths(removed, getAddedSubList()).map((pref, suff) -> {
            if(pref == 0 && suff == 0) {
                return this;
            } else {
                return new ListModificationImpl<>(
                        position + pref,
                        removed.subList(pref, getRemovedSize() - suff),
                        addedSize - pref - suff,
                        list);
            }
        });
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy