Skip to content

Commit

Permalink
Add a isEquivalentTo method to correctly check equality
Browse files Browse the repository at this point in the history
  • Loading branch information
mori-atsushi authored and sjudd committed Aug 9, 2023
1 parent 0b62103 commit 8f6d645
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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).
*
* <p>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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions library/src/main/java/com/bumptech/glide/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 8f6d645

Please sign in to comment.