Search in sources :

Example 11 with MapPutChange

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

the class MapPutChangeTest method testJson.

@Test
public void testJson() {
    MapPutChange change = new MapPutChange(feature, "some", "string");
    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_PUT, json.getString(JsonConstants.CHANGE_TYPE));
    Assert.assertEquals("some", json.getString(JsonConstants.CHANGE_MAP_KEY));
    Assert.assertEquals("string", json.getString(JsonConstants.CHANGE_PUT_VALUE));
}
Also used : MapPutChange(com.vaadin.flow.internal.change.MapPutChange) JsonObject(elemental.json.JsonObject) Test(org.junit.Test) StateNodeTest(com.vaadin.flow.internal.StateNodeTest) AbstractNodeFeatureTest(com.vaadin.flow.internal.nodefeature.AbstractNodeFeatureTest)

Example 12 with MapPutChange

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

the class MapPutChangeTest method testNodeValueType.

@Test
public void testNodeValueType() {
    StateNode value = StateNodeTest.createEmptyNode("value");
    MapPutChange change = new MapPutChange(feature, "myKey", value);
    JsonObject json = change.toJson(null);
    Assert.assertFalse(json.hasKey(JsonConstants.CHANGE_PUT_VALUE));
    JsonValue nodeValue = json.get(JsonConstants.CHANGE_PUT_NODE_VALUE);
    Assert.assertSame(JsonType.NUMBER, nodeValue.getType());
    Assert.assertEquals(value.getId(), (int) nodeValue.asNumber());
}
Also used : MapPutChange(com.vaadin.flow.internal.change.MapPutChange) StateNode(com.vaadin.flow.internal.StateNode) JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject) Test(org.junit.Test) StateNodeTest(com.vaadin.flow.internal.StateNodeTest) AbstractNodeFeatureTest(com.vaadin.flow.internal.nodefeature.AbstractNodeFeatureTest)

Example 13 with MapPutChange

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

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

MapPutChange (com.vaadin.flow.internal.change.MapPutChange)14 NodeChange (com.vaadin.flow.internal.change.NodeChange)8 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)6 JsonObject (elemental.json.JsonObject)5 StateNodeTest (com.vaadin.flow.internal.StateNodeTest)3 EmptyChange (com.vaadin.flow.internal.change.EmptyChange)3 NodeAttachChange (com.vaadin.flow.internal.change.NodeAttachChange)3 Serializable (java.io.Serializable)3 MapRemoveChange (com.vaadin.flow.internal.change.MapRemoveChange)2 AbstractNodeFeatureTest (com.vaadin.flow.internal.nodefeature.AbstractNodeFeatureTest)2 ElementData (com.vaadin.flow.internal.nodefeature.ElementData)2 VisibilityData (com.vaadin.flow.internal.nodefeature.VisibilityData)2 UniqueSerializable (com.vaadin.flow.shared.util.UniqueSerializable)2 UI (com.vaadin.flow.component.UI)1 Element (com.vaadin.flow.dom.Element)1 ElementFactory (com.vaadin.flow.dom.ElementFactory)1 StateNode (com.vaadin.flow.internal.StateNode)1 NodeDetachChange (com.vaadin.flow.internal.change.NodeDetachChange)1 ElementAttributeMap (com.vaadin.flow.internal.nodefeature.ElementAttributeMap)1