Skip to content

Commit 8f6d645

Browse files
mori-atsushisjudd
authored andcommitted
Add a isEquivalentTo method to correctly check equality
1 parent 0b62103 commit 8f6d645

File tree

3 files changed

+45
-23
lines changed

3 files changed

+45
-23
lines changed

library/src/main/java/com/bumptech/glide/request/BaseRequestOptions.java

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,31 +1209,42 @@ public T apply(@NonNull BaseRequestOptions<?> o) {
12091209
return selfOrThrowIfLocked();
12101210
}
12111211

1212+
/**
1213+
* Returns {@code true} if this {@link BaseRequestOptions} is equivalent to the given
1214+
* {@link BaseRequestOptions} (has all of the same options and sizes).
1215+
*
1216+
* <p>This method is identical to {@link #equals(Object)}, but this can not be overridden. We need
1217+
* to use this method instead of {@link #equals(Object)}, because child classes may have additional
1218+
* fields, such as listeners and models, that should not be considered when checking for equality.
1219+
*/
1220+
public final boolean isEquivalentTo(BaseRequestOptions<?> other) {
1221+
return Float.compare(other.sizeMultiplier, sizeMultiplier) == 0
1222+
&& errorId == other.errorId
1223+
&& Util.bothNullOrEqual(errorPlaceholder, other.errorPlaceholder)
1224+
&& placeholderId == other.placeholderId
1225+
&& Util.bothNullOrEqual(placeholderDrawable, other.placeholderDrawable)
1226+
&& fallbackId == other.fallbackId
1227+
&& Util.bothNullOrEqual(fallbackDrawable, other.fallbackDrawable)
1228+
&& isCacheable == other.isCacheable
1229+
&& overrideHeight == other.overrideHeight
1230+
&& overrideWidth == other.overrideWidth
1231+
&& isTransformationRequired == other.isTransformationRequired
1232+
&& isTransformationAllowed == other.isTransformationAllowed
1233+
&& useUnlimitedSourceGeneratorsPool == other.useUnlimitedSourceGeneratorsPool
1234+
&& onlyRetrieveFromCache == other.onlyRetrieveFromCache
1235+
&& diskCacheStrategy.equals(other.diskCacheStrategy)
1236+
&& priority == other.priority
1237+
&& options.equals(other.options)
1238+
&& transformations.equals(other.transformations)
1239+
&& resourceClass.equals(other.resourceClass)
1240+
&& Util.bothNullOrEqual(signature, other.signature)
1241+
&& Util.bothNullOrEqual(theme, other.theme);
1242+
}
1243+
12121244
@Override
12131245
public boolean equals(Object o) {
12141246
if (o instanceof BaseRequestOptions<?>) {
1215-
BaseRequestOptions<?> other = (BaseRequestOptions<?>) o;
1216-
return Float.compare(other.sizeMultiplier, sizeMultiplier) == 0
1217-
&& errorId == other.errorId
1218-
&& Util.bothNullOrEqual(errorPlaceholder, other.errorPlaceholder)
1219-
&& placeholderId == other.placeholderId
1220-
&& Util.bothNullOrEqual(placeholderDrawable, other.placeholderDrawable)
1221-
&& fallbackId == other.fallbackId
1222-
&& Util.bothNullOrEqual(fallbackDrawable, other.fallbackDrawable)
1223-
&& isCacheable == other.isCacheable
1224-
&& overrideHeight == other.overrideHeight
1225-
&& overrideWidth == other.overrideWidth
1226-
&& isTransformationRequired == other.isTransformationRequired
1227-
&& isTransformationAllowed == other.isTransformationAllowed
1228-
&& useUnlimitedSourceGeneratorsPool == other.useUnlimitedSourceGeneratorsPool
1229-
&& onlyRetrieveFromCache == other.onlyRetrieveFromCache
1230-
&& diskCacheStrategy.equals(other.diskCacheStrategy)
1231-
&& priority == other.priority
1232-
&& options.equals(other.options)
1233-
&& transformations.equals(other.transformations)
1234-
&& resourceClass.equals(other.resourceClass)
1235-
&& Util.bothNullOrEqual(signature, other.signature)
1236-
&& Util.bothNullOrEqual(theme, other.theme);
1247+
return isEquivalentTo((BaseRequestOptions<?>) o);
12371248
}
12381249
return false;
12391250
}

library/src/main/java/com/bumptech/glide/request/SingleRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ public boolean isEquivalentTo(Request o) {
778778
&& localOverrideHeight == otherLocalOverrideHeight
779779
&& Util.bothModelsNullEquivalentOrEquals(localModel, otherLocalModel)
780780
&& localTranscodeClass.equals(otherLocalTranscodeClass)
781-
&& localRequestOptions.equals(otherLocalRequestOptions)
781+
&& Util.bothBaseRequestOptionsNullEquivalentOrEquals(localRequestOptions, otherLocalRequestOptions)
782782
&& localPriority == otherLocalPriority
783783
// We do not want to require that RequestListeners implement equals/hashcode, so we
784784
// don't compare them using equals(). We can however, at least assert that the same

library/src/main/java/com/bumptech/glide/util/Util.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import androidx.annotation.NonNull;
99
import androidx.annotation.Nullable;
1010
import com.bumptech.glide.load.model.Model;
11+
import com.bumptech.glide.request.BaseRequestOptions;
1112
import com.bumptech.glide.request.target.Target;
1213
import java.util.ArrayDeque;
1314
import java.util.ArrayList;
@@ -240,6 +241,16 @@ public static boolean bothModelsNullEquivalentOrEquals(@Nullable Object a, @Null
240241
return a.equals(b);
241242
}
242243

244+
public static boolean bothBaseRequestOptionsNullEquivalentOrEquals(
245+
@Nullable BaseRequestOptions<?> a,
246+
@Nullable BaseRequestOptions<?> b
247+
) {
248+
if (a == null) {
249+
return b == null;
250+
}
251+
return a.isEquivalentTo(b);
252+
}
253+
243254
public static int hashCode(int value) {
244255
return hashCode(value, HASH_ACCUMULATOR);
245256
}

0 commit comments

Comments
 (0)