File |
Line |
com/irurueta/hermes/AbstractListItemChangeDetector.java |
29 |
com/irurueta/hermes/AbstractSequentialListItemChangeDetector.java |
29 |
public abstract class AbstractListItemChangeDetector<T> {
/**
* Detects changes between two lists of items.
*
* @param newItems new items.
* @param oldItems old items.
* @return a list of found changes.
* @throws IllegalArgumentException if any of the provided lists is null.
*/
public List<ListItemChange> detectChanges(final List<T> newItems, final List<T> oldItems) {
if (oldItems == null || newItems == null) {
throw new IllegalArgumentException();
}
final var changes = new ArrayList<ListItemChange>();
final var newCopy = new ArrayList<>(newItems);
final var oldCopy = new ArrayList<>(oldItems);
// removes
changes.addAll(buildRemoves(newCopy, oldCopy));
// inserts
changes.addAll(buildInserts(newCopy, oldCopy));
// moves
changes.addAll(buildMoves(newCopy, oldCopy));
// changed items
changes.addAll(buildChanges(newCopy, oldCopy));
return changes;
}
/**
* Determines whether the content of an item has changed.
*
* @param newItem new item.
* @param oldItem old item.
* @return true if the content of the item has changed, false otherwise.
*/
protected abstract boolean hasContentChanged(final T newItem, final T oldItem);
/**
* Determines whether an item is not contained in a list.
*
* @param items list of items.
* @param item item to be checked.
* @return true if the item is not contained in the list, false otherwise.
*/
protected abstract boolean notContains(final List<T> items, final T item);
/**
* Determines the index of an item in a list.
*
* @param items list of items.
* @param item item to be checked.
* @return index of the item in the list, or -1 if the item is not contained in the list.
*/
protected abstract int indexOf(final List<T> items, final T item);
/**
* Finds removed items and build a list of detected remove changes.
*
* @param newItemsCopy new items copy.
* @param oldItemsCopy old items copy.
* @return a list of detected remove changes.
*/
private List<RemovedListItemChange<T>> buildRemoves(final List<T> newItemsCopy, final List<T> oldItemsCopy) {
final var removes = new ArrayList<RemovedListItemChange<T>>();
for (var i = 0; i < oldItemsCopy.size(); i++) {
final var oldItem = oldItemsCopy.get(i);
if (notContains(newItemsCopy, oldItem)) { |
File |
Line |
com/irurueta/hermes/ListItemChangeDetector.java |
48 |
com/irurueta/hermes/SequentialListItemChangeDetector.java |
48 |
public ListItemChangeDetector(final ItemComparator<T> itemComparator,
final ItemContentComparator<T> itemContentComparator) {
if (itemComparator == null || itemContentComparator == null) {
throw new IllegalArgumentException();
}
this.itemComparator = itemComparator;
this.itemContentComparator = itemContentComparator;
}
/**
* Determines whether the content of an item has changed.
*
* @param newItem new item.
* @param oldItem old item.
* @return true if the content of the item has changed, false otherwise.
*/
@Override
protected boolean hasContentChanged(final T newItem, final T oldItem) {
return !itemContentComparator.equalContent(newItem, oldItem);
}
/**
* Determines whether an item is not contained in a list.
*
* @param items list of items.
* @param item item to be checked.
* @return true if the item is not contained in the list, false otherwise.
*/
@Override
protected boolean notContains(final List<T> items, final T item) {
return items.stream().noneMatch(newItem -> itemComparator.equals(newItem, item));
}
/**
* Determines the index of an item in a list.
*
* @param items list of items.
* @param item item to be checked.
* @return index of the item in the list, or -1 if the item is not contained in the list.
*/
@Override
protected int indexOf(final List<T> items, final T item) {
return IntStream.range(0, items.size()).filter(index -> {
final var otherItem = items.get(index);
return itemComparator.equals(item, otherItem);
}).findFirst().orElse(-1);
}
} |
File |
Line |
com/irurueta/hermes/AbstractListItemChangeDetector.java |
153 |
com/irurueta/hermes/AbstractSequentialListItemChangeDetector.java |
176 |
}
}
return moves;
}
/**
* Finds changed items and build a list of detected update changes.
*
* @param newItemsCopy new items copy.
* @param oldItemsCopy old items copy.
* @return a list of detected update changes.
*/
private List<UpdatedListItemChange<T>> buildChanges(final List<T> newItemsCopy, final List<T> oldItemsCopy) {
final var changes = new ArrayList<UpdatedListItemChange<T>>();
var pos = 0;
for (final var newItem : newItemsCopy) {
final var oldPos = indexOf(oldItemsCopy, newItem);
if (oldPos >= 0) {
final var oldItem = oldItemsCopy.get(oldPos);
// compare both items
if (hasContentChanged(newItem, oldItem)) {
// item has changed
changes.add(new UpdatedListItemChange<>(oldItem, newItem, pos));
}
}
pos++;
}
return changes;
}
} |
File |
Line |
com/irurueta/hermes/CollectionItemChangeDetector.java |
78 |
com/irurueta/hermes/ListItemChangeDetector.java |
79 |
com/irurueta/hermes/SequentialListItemChangeDetector.java |
79 |
protected boolean notContains(final Collection<T> items, final T item) {
return items.stream().noneMatch(newItem -> itemComparator.equals(newItem, item));
}
/**
* Determines the index of an item in a list.
*
* @param items list of items.
* @param item item to be checked.
* @return index of the item in the list, or -1 if the item is not contained in the list.
*/
@Override
protected int indexOf(final List<T> items, final T item) {
return IntStream.range(0, items.size()).filter(index -> {
final var otherItem = items.get(index);
return itemComparator.equals(item, otherItem);
}).findFirst().orElse(-1);
}
} |