diff --git a/library/src/main/java/com/bumptech/glide/request/BaseRequestOptions.java b/library/src/main/java/com/bumptech/glide/request/BaseRequestOptions.java index 7e0ed46af5..072bc63cc8 100644 --- a/library/src/main/java/com/bumptech/glide/request/BaseRequestOptions.java +++ b/library/src/main/java/com/bumptech/glide/request/BaseRequestOptions.java @@ -1209,31 +1209,42 @@ public T apply(@NonNull BaseRequestOptions> o) { return selfOrThrowIfLocked(); } + /** + * Returns {@code true} if this {@link BaseRequestOptions} is equivalent to the given + * {@link BaseRequestOptions} (has all of the same options and sizes). + * + *
This method is identical to {@link #equals(Object)}, but this can not be overridden. We need + * to use this method instead of {@link #equals(Object)}, because child classes may have additional + * fields, such as listeners and models, that should not be considered when checking for equality. + */ + public final boolean isEquivalentTo(BaseRequestOptions> other) { + return Float.compare(other.sizeMultiplier, sizeMultiplier) == 0 + && errorId == other.errorId + && Util.bothNullOrEqual(errorPlaceholder, other.errorPlaceholder) + && placeholderId == other.placeholderId + && Util.bothNullOrEqual(placeholderDrawable, other.placeholderDrawable) + && fallbackId == other.fallbackId + && Util.bothNullOrEqual(fallbackDrawable, other.fallbackDrawable) + && isCacheable == other.isCacheable + && overrideHeight == other.overrideHeight + && overrideWidth == other.overrideWidth + && isTransformationRequired == other.isTransformationRequired + && isTransformationAllowed == other.isTransformationAllowed + && useUnlimitedSourceGeneratorsPool == other.useUnlimitedSourceGeneratorsPool + && onlyRetrieveFromCache == other.onlyRetrieveFromCache + && diskCacheStrategy.equals(other.diskCacheStrategy) + && priority == other.priority + && options.equals(other.options) + && transformations.equals(other.transformations) + && resourceClass.equals(other.resourceClass) + && Util.bothNullOrEqual(signature, other.signature) + && Util.bothNullOrEqual(theme, other.theme); + } + @Override public boolean equals(Object o) { if (o instanceof BaseRequestOptions>) { - BaseRequestOptions> other = (BaseRequestOptions>) o; - return Float.compare(other.sizeMultiplier, sizeMultiplier) == 0 - && errorId == other.errorId - && Util.bothNullOrEqual(errorPlaceholder, other.errorPlaceholder) - && placeholderId == other.placeholderId - && Util.bothNullOrEqual(placeholderDrawable, other.placeholderDrawable) - && fallbackId == other.fallbackId - && Util.bothNullOrEqual(fallbackDrawable, other.fallbackDrawable) - && isCacheable == other.isCacheable - && overrideHeight == other.overrideHeight - && overrideWidth == other.overrideWidth - && isTransformationRequired == other.isTransformationRequired - && isTransformationAllowed == other.isTransformationAllowed - && useUnlimitedSourceGeneratorsPool == other.useUnlimitedSourceGeneratorsPool - && onlyRetrieveFromCache == other.onlyRetrieveFromCache - && diskCacheStrategy.equals(other.diskCacheStrategy) - && priority == other.priority - && options.equals(other.options) - && transformations.equals(other.transformations) - && resourceClass.equals(other.resourceClass) - && Util.bothNullOrEqual(signature, other.signature) - && Util.bothNullOrEqual(theme, other.theme); + return isEquivalentTo((BaseRequestOptions>) o); } return false; } diff --git a/library/src/main/java/com/bumptech/glide/request/SingleRequest.java b/library/src/main/java/com/bumptech/glide/request/SingleRequest.java index 51a39a57c3..211936b20e 100644 --- a/library/src/main/java/com/bumptech/glide/request/SingleRequest.java +++ b/library/src/main/java/com/bumptech/glide/request/SingleRequest.java @@ -778,7 +778,7 @@ public boolean isEquivalentTo(Request o) { && localOverrideHeight == otherLocalOverrideHeight && Util.bothModelsNullEquivalentOrEquals(localModel, otherLocalModel) && localTranscodeClass.equals(otherLocalTranscodeClass) - && localRequestOptions.equals(otherLocalRequestOptions) + && Util.bothBaseRequestOptionsNullEquivalentOrEquals(localRequestOptions, otherLocalRequestOptions) && localPriority == otherLocalPriority // We do not want to require that RequestListeners implement equals/hashcode, so we // don't compare them using equals(). We can however, at least assert that the same diff --git a/library/src/main/java/com/bumptech/glide/util/Util.java b/library/src/main/java/com/bumptech/glide/util/Util.java index 1655b2e72d..286d483ca6 100644 --- a/library/src/main/java/com/bumptech/glide/util/Util.java +++ b/library/src/main/java/com/bumptech/glide/util/Util.java @@ -8,6 +8,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.bumptech.glide.load.model.Model; +import com.bumptech.glide.request.BaseRequestOptions; import com.bumptech.glide.request.target.Target; import java.util.ArrayDeque; import java.util.ArrayList; @@ -240,6 +241,16 @@ public static boolean bothModelsNullEquivalentOrEquals(@Nullable Object a, @Null return a.equals(b); } + public static boolean bothBaseRequestOptionsNullEquivalentOrEquals( + @Nullable BaseRequestOptions> a, + @Nullable BaseRequestOptions> b + ) { + if (a == null) { + return b == null; + } + return a.isEquivalentTo(b); + } + public static int hashCode(int value) { return hashCode(value, HASH_ACCUMULATOR); }