Search in sources :

Example 1 with EmptyChange

use of com.vaadin.flow.internal.change.EmptyChange in project flow by vaadin.

the class ElementData method collectChanges.

@Override
public void collectChanges(Consumer<NodeChange> collector) {
    List<NodeChange> changes = new ArrayList<>(1);
    super.collectChanges(changes::add);
    Serializable[] previousValue;
    Serializable tracker = getNode().getChangeTracker(this, () -> null);
    if (tracker instanceof Serializable[]) {
        previousValue = (Serializable[]) tracker;
    } else {
        previousValue = new Serializable[2];
    }
    NodeChange change = changes.get(0);
    if (change instanceof MapPutChange) {
        if (!Objects.equals(previousValue[0], getTag())) {
            collector.accept(new MapPutChange(this, getKey(), getTag()));
        }
        if (!Objects.equals(previousValue[1], getPayload())) {
            collector.accept(new MapPutChange(this, NodeProperties.PAYLOAD, getPayload()));
        }
    } else if (change instanceof EmptyChange) {
        collector.accept(change);
    }
}
Also used : MapPutChange(com.vaadin.flow.internal.change.MapPutChange) NodeChange(com.vaadin.flow.internal.change.NodeChange) Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) EmptyChange(com.vaadin.flow.internal.change.EmptyChange)

Example 2 with EmptyChange

use of com.vaadin.flow.internal.change.EmptyChange in project flow by vaadin.

the class NodeValue method collectChanges.

@Override
public void collectChanges(Consumer<NodeChange> collector) {
    Serializable originalValue = getNode().getChangeTracker(this, () -> null);
    assert originalValue != null;
    if (originalValue == NULL_MARKER) {
        originalValue = null;
    }
    if (!Objects.equals(originalValue, this.value)) {
        collector.accept(new MapPutChange(this, getKey(), value));
    } else if (!isPopulated) {
        collector.accept(new EmptyChange(this));
    }
    isPopulated = true;
}
Also used : MapPutChange(com.vaadin.flow.internal.change.MapPutChange) UniqueSerializable(com.vaadin.flow.shared.util.UniqueSerializable) Serializable(java.io.Serializable) EmptyChange(com.vaadin.flow.internal.change.EmptyChange)

Example 3 with EmptyChange

use of com.vaadin.flow.internal.change.EmptyChange in project flow by vaadin.

the class NodeList method collectChanges.

@Override
public void collectChanges(Consumer<NodeChange> collector) {
    // This map contains items wrapped by AbstractListChanges as keys and
    // index in the following allChanges list as a value (it allows to get
    // AbstractListChange by the index)
    Map<Object, Integer> indices = new IdentityHashMap<>();
    // This list contains all changes in the tracker. These changes will be
    // modified: each "remove" change following by a corresponding "add"
    // will be replaced by null and "add" will be adjusted. Indeces in
    // changes in between will be adjusted
    List<AbstractListChange<T>> allChanges = new ArrayList<>();
    int index = 0;
    for (AbstractListChange<T> change : getChangeTracker()) {
        if (change instanceof ListRemoveChange<?>) {
            // the remove change => find an appropriate "add" event, adjust
            // it and adjust everything in between
            adjustChanges(index, (ListRemoveChange<T>) change, indices, allChanges);
        } else if (change instanceof ListAddChange<?>) {
            allChanges.add(change);
            int i = index;
            // put all items into "indeces" as keys and "index" as a value
            // so that the "change" can be retrieved from the "allChanges"
            // by the index
            ((ListAddChange<T>) change).getNewItems().forEach(item -> indices.put(item, i));
        } else if (change instanceof ListClearChange<?>) {
            allChanges.add(change);
        } else {
            assert false : "AbstractListChange has only three subtypes: add, remove and clear";
        }
        index++;
    }
    List<AbstractListChange<T>> changes = allChanges.stream().filter(this::acceptChange).collect(Collectors.toList());
    if (isPopulated) {
        changes.forEach(collector);
    } else {
        if (changes.isEmpty()) {
            collector.accept(new EmptyChange(this));
        } else {
            changes.forEach(collector);
        }
        isPopulated = true;
    }
}
Also used : IdentityHashMap(java.util.IdentityHashMap) StateNode(com.vaadin.flow.internal.StateNode) Iterator(java.util.Iterator) ListAddChange(com.vaadin.flow.internal.change.ListAddChange) Collection(java.util.Collection) AbstractSet(java.util.AbstractSet) Set(java.util.Set) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) ListClearChange(com.vaadin.flow.internal.change.ListClearChange) ListRemoveChange(com.vaadin.flow.internal.change.ListRemoveChange) Consumer(java.util.function.Consumer) NodeChange(com.vaadin.flow.internal.change.NodeChange) List(java.util.List) Map(java.util.Map) AbstractListChange(com.vaadin.flow.internal.change.AbstractListChange) EmptyChange(com.vaadin.flow.internal.change.EmptyChange) Collections(java.util.Collections) AbstractListChange(com.vaadin.flow.internal.change.AbstractListChange) IdentityHashMap(java.util.IdentityHashMap) ArrayList(java.util.ArrayList) ListRemoveChange(com.vaadin.flow.internal.change.ListRemoveChange) EmptyChange(com.vaadin.flow.internal.change.EmptyChange) ListAddChange(com.vaadin.flow.internal.change.ListAddChange) ListClearChange(com.vaadin.flow.internal.change.ListClearChange)

Example 4 with EmptyChange

use of com.vaadin.flow.internal.change.EmptyChange in project flow by vaadin.

the class NodeMap method collectChanges.

@Override
public void collectChanges(Consumer<NodeChange> collector) {
    boolean hasChanges = false;
    for (Entry<String, Serializable> entry : getChangeTracker().entrySet()) {
        String key = entry.getKey();
        Serializable value = entry.getValue();
        boolean containsNow = values != null && values.containsKey(key);
        boolean containedEarlier = value != REMOVED_MARKER;
        if (containedEarlier && !containsNow) {
            collector.accept(new MapRemoveChange(this, key));
            hasChanges = true;
        } else if (containsNow) {
            Object currentValue = values.get(key);
            if (!containedEarlier || !Objects.equals(value, currentValue)) {
                // New or changed value
                collector.accept(new MapPutChange(this, key, currentValue));
                hasChanges = true;
            }
        }
    }
    if (!isPopulated) {
        if (!hasChanges) {
            collector.accept(new EmptyChange(this));
        }
        isPopulated = true;
    }
}
Also used : MapPutChange(com.vaadin.flow.internal.change.MapPutChange) Serializable(java.io.Serializable) UniqueSerializable(com.vaadin.flow.shared.util.UniqueSerializable) MapRemoveChange(com.vaadin.flow.internal.change.MapRemoveChange) EmptyChange(com.vaadin.flow.internal.change.EmptyChange)

Aggregations

EmptyChange (com.vaadin.flow.internal.change.EmptyChange)4 Serializable (java.io.Serializable)4 MapPutChange (com.vaadin.flow.internal.change.MapPutChange)3 NodeChange (com.vaadin.flow.internal.change.NodeChange)2 UniqueSerializable (com.vaadin.flow.shared.util.UniqueSerializable)2 ArrayList (java.util.ArrayList)2 StateNode (com.vaadin.flow.internal.StateNode)1 AbstractListChange (com.vaadin.flow.internal.change.AbstractListChange)1 ListAddChange (com.vaadin.flow.internal.change.ListAddChange)1 ListClearChange (com.vaadin.flow.internal.change.ListClearChange)1 ListRemoveChange (com.vaadin.flow.internal.change.ListRemoveChange)1 MapRemoveChange (com.vaadin.flow.internal.change.MapRemoveChange)1 AbstractSet (java.util.AbstractSet)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 IdentityHashMap (java.util.IdentityHashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1