Skip to content
This repository has been archived by the owner on Mar 16, 2021. It is now read-only.

DistinctUntilChanged with different comparisons #24

Merged
merged 14 commits into from
Nov 6, 2016

Conversation

passsy
Copy link
Contributor

@passsy passsy commented Oct 11, 2016

As discussed in #19 the current implementation of @DistinctUntilChanged using the hashCode() of the arguments could cause problems.

The new default implementation uses equals() instead of hashcode(). A DistinctComparator can be set as argument of the annotation @DistinctUntilChanged(comparator = EqualsComparator.class). Three different implementations exist:

  • EqualsComparator (default) - uses equals() for comparison and holds a strong reference to the previous arguements
  • WeakEqualsComparator - uses equals() for comparison and holds a weak reference to the previous arguments. Could call a method twice when gc() kicks in.
  • HashComparator - old implementation using hashCode() for comparison. Doesn't hold references to the arguments

When writing tests I detected that the DistinctUntilChangedInvocationHandler swallows exceptions thrown by the TiView. The same problem was already discovered when using the @CallOnMainThread annotation 23034a0. I fixed it here accordingly

These proguard rules are required:

# ThirtyInch
-keep class net.grandcentrix.thirtyinch.distinctuntilchanged.**
-keep class net.grandcentrix.thirtyinch.callonmainthread.**

Filip and others added 12 commits October 5, 2016 10:55
rename DistinctComparator#isEquals() to compareWith(other) analog to Comparable#compareTo. This change should reflect the fact that not a equals comparison is required

The equals comparator has to use strong references to work properly (default implementation)
Alternatives: *new* WeakEqualsComparator, HashComparator

Added missing copyright
first check for backpressure before positiong the "calculating..." state
DistinctUntilChanged works now when hammering the button
@passsy passsy added this to the 0.8.0 milestone Oct 21, 2016

private interface TestViewBadHash extends TiView {

@DistinctUntilChanged(comparator = EqualsComparator.class)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name and comparator isn't equal. You say BadHash (and argument is BadHash) but using EqualsComparator


private interface TestViewBadEquals extends TiView {

@DistinctUntilChanged(comparator = HashComparator.class)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name and comparator isn't equal. You say BadEquals (and argument is BadEquals) but using HashComparator

@StefMa
Copy link
Contributor

StefMa commented Nov 4, 2016

Rest LGTM

@passsy
Copy link
Contributor Author

passsy commented Nov 4, 2016

@StefMa Thanks for pointing out the mistake in the tests!

@StefMa StefMa merged commit f56dcbc into master Nov 6, 2016
@StefMa StefMa deleted the feature/distinctuntilchanged_with_custom_comparator branch November 6, 2016 12:09
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

Successfully merging this pull request may close these issues.

3 participants