Search in sources :

Example 1 with MapRemoveChange

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

the class MapRemoveChangeTest method testJson.

@Test
public void testJson() {
    MapRemoveChange change = new MapRemoveChange(feature, "some");
    JsonObject json = change.toJson(null);
    Assert.assertEquals(change.getNode().getId(), (int) json.getNumber(JsonConstants.CHANGE_NODE));
    Assert.assertEquals(NodeFeatureRegistry.getId(feature.getClass()), (int) json.getNumber(JsonConstants.CHANGE_FEATURE));
    Assert.assertEquals(JsonConstants.CHANGE_TYPE_REMOVE, json.getString(JsonConstants.CHANGE_TYPE));
    Assert.assertEquals("some", json.getString(JsonConstants.CHANGE_MAP_KEY));
}
Also used : JsonObject(elemental.json.JsonObject) MapRemoveChange(com.vaadin.flow.internal.change.MapRemoveChange) AbstractNodeFeatureTest(com.vaadin.flow.internal.nodefeature.AbstractNodeFeatureTest) Test(org.junit.Test)

Example 2 with MapRemoveChange

use of com.vaadin.flow.internal.change.MapRemoveChange 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 3 with MapRemoveChange

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

MapRemoveChange (com.vaadin.flow.internal.change.MapRemoveChange)3 MapPutChange (com.vaadin.flow.internal.change.MapPutChange)2 Test (org.junit.Test)2 StateNodeTest (com.vaadin.flow.internal.StateNodeTest)1 EmptyChange (com.vaadin.flow.internal.change.EmptyChange)1 NodeChange (com.vaadin.flow.internal.change.NodeChange)1 AbstractNodeFeatureTest (com.vaadin.flow.internal.nodefeature.AbstractNodeFeatureTest)1 UniqueSerializable (com.vaadin.flow.shared.util.UniqueSerializable)1 JsonObject (elemental.json.JsonObject)1 Serializable (java.io.Serializable)1