use of com.vaadin.flow.internal.change.ListRemoveChange in project flow by vaadin.
the class NodeListAddRemoveTest method verifyRemoved.
private void verifyRemoved(List<NodeChange> changes, List<String> items, Integer... indexes) {
Assert.assertTrue(changes.size() > 0);
for (int i = 0; i < indexes.length; i++) {
NodeChange nodeChange = changes.get(i);
Assert.assertThat(nodeChange, CoreMatchers.instanceOf(ListRemoveChange.class));
ListRemoveChange<?> change = (ListRemoveChange<?>) nodeChange;
Assert.assertEquals(indexes[i].intValue(), change.getIndex());
Assert.assertEquals(items.get(i), change.getRemovedItem());
}
}
use of com.vaadin.flow.internal.change.ListRemoveChange 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());
}
use of com.vaadin.flow.internal.change.ListRemoveChange 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;
}
}
Aggregations