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));
}
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());
}
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());
}
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;
}
}
Aggregations