Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a isEquivalentTo method to correctly check equality #5232

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading