@Test public void testRemoveItems() { final SimpleAdapterDataSet<String> dataSet = new SimpleAdapterDataSet<>(); dataSet.swapItems(createItems(10)); final SimpleAdapterDataSet.ItemsCallback mockCallback = mock(SimpleAdapterDataSet.ItemsCallback.class); dataSet.setItemsCallback(mockCallback); final List<String> removedItems = dataSet.removeItems(4, 5); assertThat(removedItems, is(notNullValue())); assertThat(removedItems.size(), is(5)); for (int i = 0; i < removedItems.size(); i++) { assertThat(removedItems.get(i), is("Test item no. " + (i + 5))); } assertThat(dataSet.getItemCount(), is(5)); verify(mockCallback).onItemRangeRemoved(4, 5); verify(mockCallback, times(0)).onItemRangeInserted(anyInt(), anyInt()); verify(mockCallback, times(0)).onItemRangeChanged(anyInt(), anyInt()); verify(mockCallback, times(0)).onItemMoved(anyInt(), anyInt()); }
@Override @NonNull public List<I> removeItems(final int positionStart, final int itemCount) { final int itemsSize = items.size(); if (positionStart < 0 || positionStart + itemCount > itemsSize) { throw new IndexOutOfBoundsException( "Starting position(" + positionStart + ") and/or item count(" + itemCount + ") are out of range (positionStart < 0 || positionStart + itemCount > " + itemsSize + ")!" ); } final List<I> items = new ArrayList<>(itemCount); for (int i = positionStart + itemCount - 1; i >= positionStart; i--) { items.add(0, this.items.remove(i)); } AdaptersLogging.d(TAG, "Removed items from start position(" + positionStart + ") in count(" + itemCount + ")."); this.notifyItemRangeRemoved(positionStart, itemCount); return items; }
SimpleAdapterDataSet implements SimpleDataSet<I> { @Override @NonNull public List<I> removeItems(final int positionStart, final int itemCount) { final int itemsSize = items.size(); if (positionStart < 0 || positionStart + itemCount > itemsSize) { throw new IndexOutOfBoundsException( "Starting position(" + positionStart + ") and/or item count(" + itemCount + ") are out of range (positionStart < 0 || positionStart + itemCount > " + itemsSize + ")!" ); } final List<I> items = new ArrayList<>(itemCount); for (int i = positionStart + itemCount - 1; i >= positionStart; i--) { items.add(0, this.items.remove(i)); } AdaptersLogging.d(TAG, "Removed items from start position(" + positionStart + ") in count(" + itemCount + ")."); this.notifyItemRangeRemoved(positionStart, itemCount); return items; } }
SimpleAdapterDataSet implements SimpleDataSet<I> { @Override @NonNull public List<I> removeItems(final int positionStart, final int itemCount) { final int itemsSize = items.size(); if (positionStart < 0 || positionStart + itemCount > itemsSize) { throw new IndexOutOfBoundsException( "Starting position(" + positionStart + ") and/or item count(" + itemCount + ") are out of range (positionStart < 0 || positionStart + itemCount > " + itemsSize + ")!" ); } final List<I> items = new ArrayList<>(itemCount); for (int i = positionStart + itemCount - 1; i >= positionStart; i--) { items.add(0, this.items.remove(i)); } AdaptersLogging.d(TAG, "Removed items from start position(" + positionStart + ") in count(" + itemCount + ")."); this.notifyItemRangeRemoved(positionStart, itemCount); return items; } SimpleAdapterDataSet();   SimpleAdapterDataSet(@Nullable final List<I> items); }
SimpleAdapterDataSet implements SimpleDataSet<I> { @Override @NonNull public List<I> removeItems(final int positionStart, final int itemCount) { final int itemsSize = items.size(); if (positionStart < 0 || positionStart + itemCount > itemsSize) { throw new IndexOutOfBoundsException( "Starting position(" + positionStart + ") and/or item count(" + itemCount + ") are out of range (positionStart < 0 || positionStart + itemCount > " + itemsSize + ")!" ); } final List<I> items = new ArrayList<>(itemCount); for (int i = positionStart + itemCount - 1; i >= positionStart; i--) { items.add(0, this.items.remove(i)); } AdaptersLogging.d(TAG, "Removed items from start position(" + positionStart + ") in count(" + itemCount + ")."); this.notifyItemRangeRemoved(positionStart, itemCount); return items; } SimpleAdapterDataSet();   SimpleAdapterDataSet(@Nullable final List<I> items); @Override boolean isEmpty(); @Override int getItemCount(); @Override void insertItem(@NonNull final I item); @Override void insertItem(final int position, @NonNull final I item); @Override void insertItems(@NonNull final List<I> items); @Override void insertItems(final int positionStart, @NonNull final List<I> items); @Override @NonNull I swapItemById(final long itemId, @NonNull final I item); @Override @NonNull I swapItem(final int position, @NonNull final I item); @Override void moveItem(final int fromPosition, final int toPosition); @Override int removeItem(@NonNull final I item); @Override @NonNull I removeItemById(final long itemId); @Override @NonNull I removeItem(final int position); @Override @NonNull List<I> removeItems(final int positionStart, final int itemCount); @Override boolean hasItemAt(final int position); @Override boolean isEnabled(final int position); @Override boolean hasStableIds(); @Override long getItemId(final int position); @SuppressWarnings({"unchecked", "ConstantConditions"}) @Override @NonNull I getItem(int position); }
SimpleAdapterDataSet implements SimpleDataSet<I> { @Override @NonNull public List<I> removeItems(final int positionStart, final int itemCount) { final int itemsSize = items.size(); if (positionStart < 0 || positionStart + itemCount > itemsSize) { throw new IndexOutOfBoundsException( "Starting position(" + positionStart + ") and/or item count(" + itemCount + ") are out of range (positionStart < 0 || positionStart + itemCount > " + itemsSize + ")!" ); } final List<I> items = new ArrayList<>(itemCount); for (int i = positionStart + itemCount - 1; i >= positionStart; i--) { items.add(0, this.items.remove(i)); } AdaptersLogging.d(TAG, "Removed items from start position(" + positionStart + ") in count(" + itemCount + ")."); this.notifyItemRangeRemoved(positionStart, itemCount); return items; } SimpleAdapterDataSet();   SimpleAdapterDataSet(@Nullable final List<I> items); @Override boolean isEmpty(); @Override int getItemCount(); @Override void insertItem(@NonNull final I item); @Override void insertItem(final int position, @NonNull final I item); @Override void insertItems(@NonNull final List<I> items); @Override void insertItems(final int positionStart, @NonNull final List<I> items); @Override @NonNull I swapItemById(final long itemId, @NonNull final I item); @Override @NonNull I swapItem(final int position, @NonNull final I item); @Override void moveItem(final int fromPosition, final int toPosition); @Override int removeItem(@NonNull final I item); @Override @NonNull I removeItemById(final long itemId); @Override @NonNull I removeItem(final int position); @Override @NonNull List<I> removeItems(final int positionStart, final int itemCount); @Override boolean hasItemAt(final int position); @Override boolean isEnabled(final int position); @Override boolean hasStableIds(); @Override long getItemId(final int position); @SuppressWarnings({"unchecked", "ConstantConditions"}) @Override @NonNull I getItem(int position);  }