Compare commits

...

9 Commits

Author SHA1 Message Date
Denis Karmyshakov fb79dc971a Gradle update (#52) 2017-11-20 19:20:18 +03:00
Arseniy Borisov eef6932a4a Update by payload fixed (#51) 2017-11-13 12:10:06 +03:00
Denis Karmyshakov 10fcb22358 Merge pull request #48 from TouchInstinct/versions_in_constants
Versions in constants
2017-10-04 13:12:32 +03:00
Denis Karmyshakov f20db0317e Versions in constants 2017-10-04 13:10:49 +03:00
Denis Karmyshakov deff9f7cdd Versions in constants 2017-10-04 13:05:39 +03:00
Ilia Kurtov 9dcf3fb6b2 Merge pull request #43 from TouchInstinct/lib_version
rxjava version
2017-09-15 16:19:22 +03:00
gorodeckii 00b13c3acb rxjava version 2017-09-15 15:18:05 +03:00
Gavriil 74e70be245 RxJava version update (#42) 2017-09-11 18:51:05 +03:00
Arseniy Borisov c7edf3c84d idea formatter (#41) 2017-09-04 17:36:48 +03:00
6 changed files with 56 additions and 41 deletions

View File

@ -1,9 +1,7 @@
apply plugin: 'com.android.library' apply plugin: 'com.android.library'
apply plugin: 'me.tatarka.retrolambda'
android { android {
compileSdkVersion 25 compileSdkVersion compileSdk
buildToolsVersion '25.0.3'
compileOptions { compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8 sourceCompatibility JavaVersion.VERSION_1_8
@ -16,7 +14,7 @@ android {
} }
dependencies { dependencies {
provided 'com.android.support:support-annotations:25.4.0' compileOnly "com.android.support:support-annotations:$supportLibraryVersion"
provided 'io.reactivex:rxandroid:1.2.1' compileOnly "io.reactivex:rxandroid:$rxAndroidVersion"
provided 'io.reactivex:rxjava:1.3.0' compileOnly "io.reactivex:rxjava:$rxJavaVersion"
} }

View File

@ -50,13 +50,13 @@ import java.util.List;
* and the cost of your comparison methods. Below are some average run times for reference: * and the cost of your comparison methods. Below are some average run times for reference:
* (The areSame list is composed of random UUID Strings and the tests are run on Nexus 5X with M) * (The areSame list is composed of random UUID Strings and the tests are run on Nexus 5X with M)
* <ul> * <ul>
* <li>100 items and 10 modifications: avg: 0.39 ms, median: 0.35 ms * <li>100 items and 10 modifications: avg: 0.39 ms, median: 0.35 ms
* <li>100 items and 100 modifications: 3.82 ms, median: 3.75 ms * <li>100 items and 100 modifications: 3.82 ms, median: 3.75 ms
* <li>100 items and 100 modifications without moves: 2.09 ms, median: 2.06 ms * <li>100 items and 100 modifications without moves: 2.09 ms, median: 2.06 ms
* <li>1000 items and 50 modifications: avg: 4.67 ms, median: 4.59 ms * <li>1000 items and 50 modifications: avg: 4.67 ms, median: 4.59 ms
* <li>1000 items and 50 modifications without moves: avg: 3.59 ms, median: 3.50 ms * <li>1000 items and 50 modifications without moves: avg: 3.59 ms, median: 3.50 ms
* <li>1000 items and 200 modifications: 27.07 ms, median: 26.92 ms * <li>1000 items and 200 modifications: 27.07 ms, median: 26.92 ms
* <li>1000 items and 200 modifications without moves: 13.54 ms, median: 13.36 ms * <li>1000 items and 200 modifications without moves: 13.54 ms, median: 13.36 ms
* </ul> * </ul>
* *
* <p>Due to implementation constraints, the max size of the list can be 2^26. * <p>Due to implementation constraints, the max size of the list can be 2^26.
@ -83,7 +83,6 @@ public class DiffUtil {
* Calculates the list of update operations that can covert one list into the other one. * Calculates the list of update operations that can covert one list into the other one.
* *
* @param cb The callback that acts as a gateway to the backing list data * @param cb The callback that acts as a gateway to the backing list data
*
* @return A DiffResult that contains the information about the edit sequence to convert the * @return A DiffResult that contains the information about the edit sequence to convert the
* old list into the new list. * old list into the new list.
*/ */
@ -98,9 +97,8 @@ public class DiffUtil {
* positions), you can disable move detection which takes <code>O(N^2)</code> time where * positions), you can disable move detection which takes <code>O(N^2)</code> time where
* N is the number of added, moved, removed items. * N is the number of added, moved, removed items.
* *
* @param cb The callback that acts as a gateway to the backing list data * @param cb The callback that acts as a gateway to the backing list data
* @param detectMoves True if DiffUtil should try to detect moved items, false otherwise. * @param detectMoves True if DiffUtil should try to detect moved items, false otherwise.
*
* @return A DiffResult that contains the information about the edit sequence to convert the * @return A DiffResult that contains the information about the edit sequence to convert the
* old list into the new list. * old list into the new list.
*/ */
@ -185,7 +183,7 @@ public class DiffUtil {
} }
private static Snake diffPartial(Callback cb, int startOld, int endOld, private static Snake diffPartial(Callback cb, int startOld, int endOld,
int startNew, int endNew, int[] forward, int[] backward, int kOffset) { int startNew, int endNew, int[] forward, int[] backward, int kOffset) {
final int oldSize = endOld - startOld; final int oldSize = endOld - startOld;
final int newSize = endNew - startNew; final int newSize = endNew - startNew;
@ -329,7 +327,6 @@ public class DiffUtil {
* *
* @param oldItemPosition The position of the item in the old list * @param oldItemPosition The position of the item in the old list
* @param newItemPosition The position of the item in the new list * @param newItemPosition The position of the item in the new list
*
* @return A payload object that represents the change between the two items. * @return A payload object that represents the change between the two items.
*/ */
@Nullable @Nullable
@ -453,14 +450,14 @@ public class DiffUtil {
private final boolean mDetectMoves; private final boolean mDetectMoves;
/** /**
* @param callback The callback that was used to calculate the diff * @param callback The callback that was used to calculate the diff
* @param snakes The list of Myers' snakes * @param snakes The list of Myers' snakes
* @param oldItemStatuses An int[] that can be re-purposed to keep metadata * @param oldItemStatuses An int[] that can be re-purposed to keep metadata
* @param newItemStatuses An int[] that can be re-purposed to keep metadata * @param newItemStatuses An int[] that can be re-purposed to keep metadata
* @param detectMoves True if this DiffResult will try to detect moved items * @param detectMoves True if this DiffResult will try to detect moved items
*/ */
DiffResult(Callback callback, List<Snake> snakes, int[] oldItemStatuses, DiffResult(Callback callback, List<Snake> snakes, int[] oldItemStatuses,
int[] newItemStatuses, boolean detectMoves) { int[] newItemStatuses, boolean detectMoves) {
mSnakes = snakes; mSnakes = snakes;
mOldItemStatuses = oldItemStatuses; mOldItemStatuses = oldItemStatuses;
mNewItemStatuses = newItemStatuses; mNewItemStatuses = newItemStatuses;
@ -556,15 +553,14 @@ public class DiffUtil {
* Finds a matching item that is before the given coordinates in the matrix * Finds a matching item that is before the given coordinates in the matrix
* (before : left and above). * (before : left and above).
* *
* @param x The x position in the matrix (position in the old list) * @param x The x position in the matrix (position in the old list)
* @param y The y position in the matrix (position in the new list) * @param y The y position in the matrix (position in the new list)
* @param snakeIndex The current snake index * @param snakeIndex The current snake index
* @param removal True if we are looking for a removal, false otherwise * @param removal True if we are looking for a removal, false otherwise
*
* @return True if such item is found. * @return True if such item is found.
*/ */
private boolean findMatchingItem(final int x, final int y, final int snakeIndex, private boolean findMatchingItem(final int x, final int y, final int snakeIndex,
final boolean removal) { final boolean removal) {
final int myItemPos; final int myItemPos;
int curX; int curX;
int curY; int curY;
@ -664,7 +660,7 @@ public class DiffUtil {
} }
private static PostponedUpdate removePostponedUpdate(List<PostponedUpdate> updates, private static PostponedUpdate removePostponedUpdate(List<PostponedUpdate> updates,
int pos, boolean removal) { int pos, boolean removal) {
for (int i = updates.size() - 1; i >= 0; i--) { for (int i = updates.size() - 1; i >= 0; i--) {
final PostponedUpdate update = updates.get(i); final PostponedUpdate update = updates.get(i);
if (update.posInOwnerList == pos && update.removal == removal) { if (update.posInOwnerList == pos && update.removal == removal) {
@ -680,7 +676,7 @@ public class DiffUtil {
} }
private void dispatchAdditions(List<PostponedUpdate> postponedUpdates, private void dispatchAdditions(List<PostponedUpdate> postponedUpdates,
ListUpdateCallback updateCallback, int start, int count, int globalIndex) { ListUpdateCallback updateCallback, int start, int count, int globalIndex) {
if (!mDetectMoves) { if (!mDetectMoves) {
updateCallback.onInserted(start, count); updateCallback.onInserted(start, count);
return; return;
@ -720,7 +716,7 @@ public class DiffUtil {
} }
private void dispatchRemovals(List<PostponedUpdate> postponedUpdates, private void dispatchRemovals(List<PostponedUpdate> postponedUpdates,
ListUpdateCallback updateCallback, int start, int count, int globalIndex) { ListUpdateCallback updateCallback, int start, int count, int globalIndex) {
if (!mDetectMoves) { if (!mDetectMoves) {
updateCallback.onRemoved(start, count); updateCallback.onRemoved(start, count);
return; return;

View File

@ -98,8 +98,8 @@ public abstract class ObservableCollection<TItem> {
* Method to notify that collection have changed. * Method to notify that collection have changed.
* *
* @param insertedItems Collection of inserted items; * @param insertedItems Collection of inserted items;
* @param removedItems Collection of removed items; * @param removedItems Collection of removed items;
* @param changes Changes of collection. * @param changes Changes of collection.
*/ */
protected void notifyAboutChanges(@NonNull final List<TItem> insertedItems, protected void notifyAboutChanges(@NonNull final List<TItem> insertedItems,
@NonNull final List<TItem> removedItems, @NonNull final List<TItem> removedItems,

View File

@ -58,6 +58,8 @@ public class ObservableList<TItem> extends ObservableCollection<TItem> implement
private SameItemsPredicate<TItem> sameItemsPredicate; private SameItemsPredicate<TItem> sameItemsPredicate;
@Nullable @Nullable
private ChangePayloadProducer<TItem> changePayloadProducer; private ChangePayloadProducer<TItem> changePayloadProducer;
@Nullable
private ObservableList<TItem> diffUtilsSource;
public ObservableList() { public ObservableList() {
super(); super();
@ -227,9 +229,19 @@ public class ObservableList<TItem> extends ObservableCollection<TItem> implement
synchronized (this) { synchronized (this) {
final List<TItem> oldList = new ArrayList<>(items); final List<TItem> oldList = new ArrayList<>(items);
final List<TItem> newList = new ArrayList<>(newItems); final List<TItem> newList = new ArrayList<>(newItems);
final CollectionsChangesCalculator<TItem> calculator = sameItemsPredicate != null final CollectionsChangesCalculator<TItem> calculator;
? new DiffCollectionsChangesCalculator<>(oldList, newList, detectMoves, sameItemsPredicate, changePayloadProducer) if (diffUtilsSource != null) {
: new DefaultCollectionsChangesCalculator<>(oldList, newList, false); if (diffUtilsSource.sameItemsPredicate != null) {
calculator = new DiffCollectionsChangesCalculator<>(oldList, newList,
diffUtilsSource.detectMoves, diffUtilsSource.sameItemsPredicate, diffUtilsSource.changePayloadProducer);
} else {
calculator = new DefaultCollectionsChangesCalculator<>(oldList, newList, false);
}
} else if (sameItemsPredicate != null) {
calculator = new DiffCollectionsChangesCalculator<>(oldList, newList, detectMoves, sameItemsPredicate, changePayloadProducer);
} else {
calculator = new DefaultCollectionsChangesCalculator<>(oldList, newList, false);
}
items.clear(); items.clear();
items.addAll(newItems); items.addAll(newItems);
notifyAboutChanges(calculator.calculateInsertedItems(), calculator.calculateRemovedItems(), calculator.calculateChanges()); notifyAboutChanges(calculator.calculateInsertedItems(), calculator.calculateRemovedItems(), calculator.calculateChanges());
@ -246,8 +258,8 @@ public class ObservableList<TItem> extends ObservableCollection<TItem> implement
/** /**
* Enable diff utils algorithm in collection changes. * Enable diff utils algorithm in collection changes.
* *
* @param detectMoves The flag that determines whether the {@link Change.Moved} changes will be generated or not; * @param detectMoves The flag that determines whether the {@link Change.Moved} changes will be generated or not;
* @param sameItemsPredicate Predicate for the determination of the same elements; * @param sameItemsPredicate Predicate for the determination of the same elements;
* @param changePayloadProducer Function that calculate change payload when items the same but contents are different. * @param changePayloadProducer Function that calculate change payload when items the same but contents are different.
*/ */
public void enableDiffUtils(final boolean detectMoves, public void enableDiffUtils(final boolean detectMoves,
@ -271,7 +283,16 @@ public class ObservableList<TItem> extends ObservableCollection<TItem> implement
* @return true if diff utils is enabled. * @return true if diff utils is enabled.
*/ */
public boolean diffUtilsIsEnabled() { public boolean diffUtilsIsEnabled() {
return sameItemsPredicate != null; return diffUtilsSource != null ? diffUtilsSource.diffUtilsIsEnabled() : sameItemsPredicate != null;
}
/**
* Sets observableCollection as a source of diff utils parameters;
*
* @param diffUtilsSource Source of diff utils parameters.
*/
public void setDiffUtilsSource(@Nullable final ObservableList<TItem> diffUtilsSource) {
this.diffUtilsSource = diffUtilsSource;
} }
/** /**

View File

@ -38,7 +38,7 @@ public interface Converter<TObject, TStoreObject> {
* *
* @param objectType Type of object; * @param objectType Type of object;
* @param storeObjectType Type of store object allowed to store; * @param storeObjectType Type of store object allowed to store;
* @param object Object to be converted to store object; * @param object Object to be converted to store object;
* @return Object that is allowed to store into specific {@link Store}; * @return Object that is allowed to store into specific {@link Store};
* @throws ConversionException Exception during conversion. Usually it indicates illegal state. * @throws ConversionException Exception during conversion. Usually it indicates illegal state.
*/ */
@ -51,7 +51,7 @@ public interface Converter<TObject, TStoreObject> {
* *
* @param objectType Type of object; * @param objectType Type of object;
* @param storeObjectType Type of store object allowed to store; * @param storeObjectType Type of store object allowed to store;
* @param storeObject Object from specific {@link Store}; * @param storeObject Object from specific {@link Store};
* @return Object converted from store object; * @return Object converted from store object;
* @throws ConversionException Exception during conversion. Usually it indicates illegal state. * @throws ConversionException Exception during conversion. Usually it indicates illegal state.
*/ */

View File

@ -32,7 +32,7 @@ import ru.touchin.roboswag.core.utils.ObjectUtils;
* Both arguments are not null. * Both arguments are not null.
* Note that if you want to save this pair in state, you need make TFirst and TSecond Serializable too. * Note that if you want to save this pair in state, you need make TFirst and TSecond Serializable too.
* *
* @param <TFirst> type of the first nonnull argument. * @param <TFirst> type of the first nonnull argument.
* @param <TSecond> type of the second nonnull argument. * @param <TSecond> type of the second nonnull argument.
*/ */
public class NonNullPair<TFirst, TSecond> implements Serializable { public class NonNullPair<TFirst, TSecond> implements Serializable {