Search in sources :

Example 16 with NodeChange

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

the class NodeMapTest method testCollectChange.

@Test
public void testCollectChange() {
    List<NodeChange> initialChanges = collectChanges(nodeMap);
    Assert.assertEquals(0, initialChanges.size());
    nodeMap.put(KEY, "value");
    List<NodeChange> putChanges = collectChanges(nodeMap);
    Assert.assertEquals(1, putChanges.size());
    MapPutChange putChange = (MapPutChange) putChanges.get(0);
    Assert.assertEquals(KEY, putChange.getKey());
    Assert.assertEquals("value", putChange.getValue());
    nodeMap.put(KEY, null);
    List<NodeChange> putNullChanges = collectChanges(nodeMap);
    Assert.assertEquals(1, putNullChanges.size());
    MapPutChange putNullChange = (MapPutChange) putNullChanges.get(0);
    Assert.assertEquals(KEY, putNullChange.getKey());
    Assert.assertEquals(null, putNullChange.getValue());
    nodeMap.remove(KEY);
    List<NodeChange> removeChanges = collectChanges(nodeMap);
    Assert.assertEquals(1, removeChanges.size());
    MapRemoveChange removeChange = (MapRemoveChange) removeChanges.get(0);
    Assert.assertEquals(KEY, removeChange.getKey());
}
Also used : MapPutChange(com.vaadin.flow.internal.change.MapPutChange) NodeChange(com.vaadin.flow.internal.change.NodeChange) MapRemoveChange(com.vaadin.flow.internal.change.MapRemoveChange) Test(org.junit.Test) StateNodeTest(com.vaadin.flow.internal.StateNodeTest)

Example 17 with NodeChange

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

the class StateNodeNodeListTest method testAddingAndRemoving.

@Test
public void testAddingAndRemoving() {
    StateNode value1 = StateNodeTest.createEmptyNode("value1");
    StateNode value2 = StateNodeTest.createEmptyNode("value2");
    nodeList.add(value1);
    Assert.assertEquals(1, nodeList.size());
    Assert.assertSame(value1, nodeList.get(0));
    List<NodeChange> firstAddChanges = collectChanges(nodeList);
    Assert.assertEquals(1, firstAddChanges.size());
    ListAddChange<?> firstAddChange = (ListAddChange<?>) firstAddChanges.get(0);
    Assert.assertEquals(0, firstAddChange.getIndex());
    Assert.assertEquals(Arrays.asList(value1), firstAddChange.getNewItems());
    nodeList.add(0, value2);
    Assert.assertEquals(2, nodeList.size());
    Assert.assertSame(value2, nodeList.get(0));
    Assert.assertSame(value1, nodeList.get(1));
    List<NodeChange> secondAddChanges = collectChanges(nodeList);
    Assert.assertEquals(1, secondAddChanges.size());
    ListAddChange<?> secondAddChange = (ListAddChange<?>) secondAddChanges.get(0);
    Assert.assertEquals(0, secondAddChange.getIndex());
    Assert.assertEquals(Arrays.asList(value2), secondAddChange.getNewItems());
    StateNode removedItem = nodeList.remove(0);
    Assert.assertEquals(1, nodeList.size());
    Assert.assertSame(value1, nodeList.get(0));
    Assert.assertSame(value2, removedItem);
    List<NodeChange> removeChanges = collectChanges(nodeList);
    Assert.assertEquals(1, removeChanges.size());
    ListRemoveChange<?> removeChange = (ListRemoveChange<?>) removeChanges.get(0);
    Assert.assertEquals(0, removeChange.getIndex());
}
Also used : NodeChange(com.vaadin.flow.internal.change.NodeChange) ListAddChange(com.vaadin.flow.internal.change.ListAddChange) StateNode(com.vaadin.flow.internal.StateNode) ListRemoveChange(com.vaadin.flow.internal.change.ListRemoveChange) Test(org.junit.Test) StateNodeTest(com.vaadin.flow.internal.StateNodeTest)

Example 18 with NodeChange

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

the class StateNodeNodeListTest method testChangesAfterReset.

@Test
public void testChangesAfterReset() {
    StateNode value1 = StateNodeTest.createEmptyNode("value1");
    StateNode value2 = StateNodeTest.createEmptyNode("value2");
    nodeList.add(value1);
    nodeList.add(value2);
    nodeList.getNode().clearChanges();
    nodeList.generateChangesFromEmpty();
    List<NodeChange> changes = collectChanges(nodeList);
    Assert.assertEquals(1, changes.size());
    ListAddChange<?> change = (ListAddChange<?>) changes.get(0);
    Assert.assertEquals(0, change.getIndex());
    Assert.assertEquals(Arrays.asList(value1, value2), change.getNewItems());
}
Also used : NodeChange(com.vaadin.flow.internal.change.NodeChange) ListAddChange(com.vaadin.flow.internal.change.ListAddChange) StateNode(com.vaadin.flow.internal.StateNode) Test(org.junit.Test) StateNodeTest(com.vaadin.flow.internal.StateNodeTest)

Example 19 with NodeChange

use of com.vaadin.flow.internal.change.NodeChange 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)

Aggregations

NodeChange (com.vaadin.flow.internal.change.NodeChange)19 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)11 MapPutChange (com.vaadin.flow.internal.change.MapPutChange)8 StateNode (com.vaadin.flow.internal.StateNode)5 ListAddChange (com.vaadin.flow.internal.change.ListAddChange)4 NodeAttachChange (com.vaadin.flow.internal.change.NodeAttachChange)4 StateNodeTest (com.vaadin.flow.internal.StateNodeTest)3 ListRemoveChange (com.vaadin.flow.internal.change.ListRemoveChange)3 EmptyChange (com.vaadin.flow.internal.change.EmptyChange)2 ListClearChange (com.vaadin.flow.internal.change.ListClearChange)2 ElementData (com.vaadin.flow.internal.nodefeature.ElementData)2 ElementPropertyMap (com.vaadin.flow.internal.nodefeature.ElementPropertyMap)2 VisibilityData (com.vaadin.flow.internal.nodefeature.VisibilityData)2 JsonObject (elemental.json.JsonObject)2 Serializable (java.io.Serializable)2 List (java.util.List)2 Set (java.util.Set)2 Consumer (java.util.function.Consumer)2 Collectors (java.util.stream.Collectors)2