Optionally cache hashcode: ImageRequest
@@ -41,6 +41,10 @@ import javax.annotation.concurrent.Immutable;
@Immutable
public class ImageRequest {
+ private static boolean sUseCachedHashcodeInEquals;
+ private static boolean sCacheHashcode;
+ private int mHashcode;
+
/** Cache choice */
private final CacheChoice mCacheChoice;
@@ -238,6 +242,13 @@ public class ImageRequest {
return false;
}
ImageRequest request = (ImageRequest) o;
+ if (sUseCachedHashcodeInEquals) {
+ int a = mHashcode;
+ int b = request.mHashcode;
+ if (a != 0 && b != 0 && a != b) {
+ return false;
+ }
+ }
if (mLocalThumbnailPreviewsEnabled != request.mLocalThumbnailPreviewsEnabled) return false;
if (mIsDiskCacheEnabled != request.mIsDiskCacheEnabled) return false;
if (mIsMemoryCacheEnabled != request.mIsMemoryCacheEnabled) return false;
@@ -263,9 +274,16 @@ public class ImageRequest {
@Override
public int hashCode() {
+ final boolean cacheHashcode = sCacheHashcode;
+ int result = 0;
+ if (cacheHashcode) {
+ result = mHashcode;
+ }
+ if (result == 0) {
final CacheKey postprocessorCacheKey =
mPostprocessor != null ? mPostprocessor.getPostprocessorCacheKey() : null;
- return Objects.hashCode(
+ result =
+ Objects.hashCode(
mCacheChoice,
mSourceUri,
mLocalThumbnailPreviewsEnabled,
@@ -280,6 +298,11 @@ public class ImageRequest {
mRotationOptions,
postprocessorCacheKey,
mResizingAllowedOverride);
+ if (cacheHashcode) {
+ mHashcode = result;
+ }
+ }
+ return result;
}
@Override
@@ -385,4 +408,12 @@ public class ImageRequest {
return arg != null ? arg.getSourceUri() : null;
}
};
+
+ public static void setUseCachedHashcodeInEquals(boolean useCachedHashcodeInEquals) {
+ sUseCachedHashcodeInEquals = useCachedHashcodeInEquals;
+ }
+
+ public static void setCacheHashcode(boolean cacheHashcode) {
+ sCacheHashcode = cacheHashcode;
+ }
}
