AbstractListDataView extends AbstractDataView<T> implements ListDataView<T, AbstractListDataView<T>> { @Override public AbstractListDataView<T> addItemsBefore(Collection<T> items, T before) { addItemCollectionOnTarget(items, before, "Item to insert before is not available in the data", (index, containsTarget) -> index); return this; } AbstractListDataView(
            SerializableSupplier<? extends DataProvider<T, ?>> dataProviderSupplier,
            Component component); @Override int getItemCount(); @Override T getItem(int index); @Override Optional<T> getNextItem(T item); @Override Optional<T> getPreviousItem(T item); @Override AbstractListDataView<T> addFilter(SerializablePredicate<T> filter); @Override AbstractListDataView<T> removeFilters(); @Override AbstractListDataView<T> setFilter(SerializablePredicate<T> filter); @Override AbstractListDataView<T> setSortComparator(
            SerializableComparator<T> sortComparator); @Override AbstractListDataView<T> addSortComparator(
            SerializableComparator<T> sortComparator); @Override AbstractListDataView<T> removeSorting(); @Override AbstractListDataView<T> setSortOrder(
            ValueProvider<T, V1> valueProvider, SortDirection sortDirection); @Override AbstractListDataView<T> addSortOrder(
            ValueProvider<T, V1> valueProvider, SortDirection sortDirection); @Override boolean contains(T item); @Override AbstractListDataView<T> addItem(T item); @Override AbstractListDataView<T> addItems(Collection<T> items); @Override AbstractListDataView<T> addItemAfter(T item, T after); @Override AbstractListDataView<T> addItemBefore(T item, T before); @Override AbstractListDataView<T> addItemsAfter(Collection<T> items, T after); @Override AbstractListDataView<T> addItemsBefore(Collection<T> items,
            T before); @Override AbstractListDataView<T> removeItem(T item); @Override AbstractListDataView<T> removeItems(Collection<T> items);  }
@Test public void addItemsBefore_allItemsAreAddedBeforeTargetItem() { dataView.addItemsBefore(Arrays.asList("newOne", "newTwo", "newThree"), "middle"); Assert.assertArrayEquals( new String[] { "first", "newOne", "newTwo", "newThree", "middle", "last" }, dataView.getItems().toArray(String[]::new)); }
@Test public void addItemsBefore_someItemsAlreadyInList_allItemsAreMovedBeforeTargetAndOrdered() { dataView.addItemsBefore(Arrays.asList("first", "newOne", "newTwo"), "last"); Assert.assertArrayEquals( new String[] { "middle", "first", "newOne", "newTwo", "last" }, dataView.getItems().toArray(String[]::new)); dataView.addItemsBefore(Arrays.asList("newThree", "last"), "last"); Assert.assertArrayEquals( new String[] { "middle", "first", "newOne", "newTwo", "newThree", "last" }, dataView.getItems().toArray(String[]::new)); dataView.addItemsBefore(Arrays.asList("newFour", "middle"), "middle"); Assert.assertArrayEquals( new String[] { "newFour", "middle", "first", "newOne", "newTwo", "newThree", "last" }, dataView.getItems().toArray(String[]::new)); dataView.addItemsBefore(Collections.singletonList("newFive"), "newFour"); Assert.assertArrayEquals( new String[] { "newFive", "newFour", "middle", "first", "newOne", "newTwo", "newThree", "last" }, dataView.getItems().toArray(String[]::new)); }
@Test public void addItemsBefore_targetItemNotInCollection_throwsException() { exceptionRule.expect(IllegalArgumentException.class); exceptionRule.expectMessage( "Item to insert before is not available in the data"); dataView.addItemsBefore(Collections.singletonList("newItem"), "notExistent"); }
@Test public void addItemsBefore_nullCollectionPassed_throwsException() { exceptionRule.expect(NullPointerException.class); exceptionRule.expectMessage("Items collection cannot be null"); dataView.addItemsBefore(null, "any"); }
@Test public void addItemsBefore_emptyCollectionPassed_dataNotChanged() { dataView.addItemsBefore(Collections.emptyList(), "any"); Assert.assertArrayEquals(new String[] { "first", "middle", "last" }, dataView.getItems().toArray(String[]::new)); }
@Test public void dataProviderOnSet_exceptionThrownForAddItemsBefore() { exceptionRule.expect(IllegalArgumentException.class); exceptionRule.expectMessage( "DataProvider collection 'HashSet' is not a list."); Set<String> items = new HashSet<>(); items.add("item1"); items.add("item2"); final ListDataProvider<String> stringListDataProvider = new ListDataProvider<>( items); dataView = new ListDataViewImpl(() -> stringListDataProvider, component); dataView.addItemsBefore(Collections.singleton("newItem"), "item1"); }