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