From 6ce1fa96df42a69969f5a4997171524fee267d26 Mon Sep 17 00:00:00 2001 From: Yuriy Budiyev Date: Tue, 6 Mar 2018 22:20:36 +0300 Subject: [PATCH] Dev --- .../budiyev/android/imageloader/ImageLoader.java | 5 +++-- .../android/imageloader/ImageLoaderBuilder.java | 9 +++++---- .../budiyev/android/imageloader/ImageRequest.java | 15 +++------------ .../android/imageloader/InternalUtils.java | 8 ++++++++ .../android/imageloader/MemoryImageCache.java | 3 +++ .../android/imageloader/StorageImageCache.java | 7 +++++-- 6 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/budiyev/android/imageloader/ImageLoader.java b/src/main/java/com/budiyev/android/imageloader/ImageLoader.java index c029316..e4c4bf2 100644 --- a/src/main/java/com/budiyev/android/imageloader/ImageLoader.java +++ b/src/main/java/com/budiyev/android/imageloader/ImageLoader.java @@ -131,8 +131,9 @@ public void invalidate(@NonNull Object data) { public void registerDataType(@NonNull Class dataClass, @NonNull DataDescriptorFactory descriptorFactory, @NonNull BitmapLoader bitmapLoader) { String dataClassName = dataClass.getName(); - mDescriptorFactories.put(dataClassName, (DataDescriptorFactory) descriptorFactory); - mBitmapLoaders.put(dataClassName, (BitmapLoader) bitmapLoader); + mDescriptorFactories + .put(dataClassName, (DataDescriptorFactory) InternalUtils.requireNonNull(descriptorFactory)); + mBitmapLoaders.put(dataClassName, (BitmapLoader) InternalUtils.requireNonNull(bitmapLoader)); } /** diff --git a/src/main/java/com/budiyev/android/imageloader/ImageLoaderBuilder.java b/src/main/java/com/budiyev/android/imageloader/ImageLoaderBuilder.java index 3ca9071..fc8d83d 100644 --- a/src/main/java/com/budiyev/android/imageloader/ImageLoaderBuilder.java +++ b/src/main/java/com/budiyev/android/imageloader/ImageLoaderBuilder.java @@ -29,6 +29,7 @@ import android.content.Context; import android.net.Uri; +import android.support.annotation.IntRange; import android.support.annotation.NonNull; import android.support.annotation.Nullable; @@ -59,7 +60,7 @@ public ImageLoaderBuilder memoryCache() { * Memory cache with specified maximum size */ @NonNull - public ImageLoaderBuilder memoryCache(int maxSize) { + public ImageLoaderBuilder memoryCache(@IntRange(from = 0) int maxSize) { mMemoryCache = new MemoryImageCache(maxSize); return this; } @@ -88,7 +89,7 @@ public ImageLoaderBuilder storageCache() { * located in subdirectory of {@link Context#getExternalCacheDir} */ @NonNull - public ImageLoaderBuilder storageCache(long maxSize) { + public ImageLoaderBuilder storageCache(@IntRange(from = 0L) long maxSize) { mStorageCache = new StorageImageCache(mContext, maxSize); return this; } @@ -100,7 +101,7 @@ public ImageLoaderBuilder storageCache(long maxSize) { * @see CompressMode */ @NonNull - public ImageLoaderBuilder storageCache(@NonNull CompressMode compressMode, long maxSize) { + public ImageLoaderBuilder storageCache(@NonNull CompressMode compressMode, @IntRange(from = 0L) long maxSize) { mStorageCache = new StorageImageCache(mContext, compressMode, maxSize); return this; } @@ -118,7 +119,7 @@ public ImageLoaderBuilder storageCache(@NonNull File directory) { * Storage cache with specified directory and maximum size */ @NonNull - public ImageLoaderBuilder storageCache(@NonNull File directory, long maxSize) { + public ImageLoaderBuilder storageCache(@NonNull File directory, @IntRange(from = 0L) long maxSize) { mStorageCache = new StorageImageCache(directory, maxSize); return this; } diff --git a/src/main/java/com/budiyev/android/imageloader/ImageRequest.java b/src/main/java/com/budiyev/android/imageloader/ImageRequest.java index e853ade..86de645 100644 --- a/src/main/java/com/budiyev/android/imageloader/ImageRequest.java +++ b/src/main/java/com/budiyev/android/imageloader/ImageRequest.java @@ -183,8 +183,7 @@ public ImageRequest errorDrawable(@DrawableRes int resId) { */ @NonNull public ImageRequest transform(@NonNull BitmapTransformation transformation) { - checkNonNull(transformation); - transformations().add(transformation); + transformations().add(InternalUtils.requireNonNull(transformation)); return this; } @@ -196,8 +195,7 @@ public ImageRequest transform(@NonNull BitmapTransformation transformation) { */ @NonNull public ImageRequest transform(@NonNull Collection transformations) { - checkNonNull(transformations); - transformations().addAll(transformations); + transformations().addAll(InternalUtils.requireNonNull(transformations)); return this; } @@ -209,8 +207,7 @@ public ImageRequest transform(@NonNull Collection trans */ @NonNull public ImageRequest transform(@NonNull BitmapTransformation... transformations) { - checkNonNull(transformations); - Collections.addAll(transformations(), transformations); + Collections.addAll(transformations(), InternalUtils.requireNonNull(transformations)); return this; } @@ -445,10 +442,4 @@ private void checkSize(int width, int height) { throw new IllegalArgumentException("Width and height should be greater than zero"); } } - - private void checkNonNull(Object value) { - if (value == null) { - throw new NullPointerException(); - } - } } diff --git a/src/main/java/com/budiyev/android/imageloader/InternalUtils.java b/src/main/java/com/budiyev/android/imageloader/InternalUtils.java index 0ccde93..8779e00 100644 --- a/src/main/java/com/budiyev/android/imageloader/InternalUtils.java +++ b/src/main/java/com/budiyev/android/imageloader/InternalUtils.java @@ -247,4 +247,12 @@ public static Bitmap rotateAndRecycle(@NonNull Bitmap bitmap, int rotation) { } return rotated; } + + @NonNull + public static T requireNonNull(@Nullable T value) { + if (value == null) { + throw new NullPointerException(); + } + return value; + } } diff --git a/src/main/java/com/budiyev/android/imageloader/MemoryImageCache.java b/src/main/java/com/budiyev/android/imageloader/MemoryImageCache.java index a833fb6..69d3ee0 100644 --- a/src/main/java/com/budiyev/android/imageloader/MemoryImageCache.java +++ b/src/main/java/com/budiyev/android/imageloader/MemoryImageCache.java @@ -46,6 +46,9 @@ public MemoryImageCache() { } public MemoryImageCache(int maxSize) { + if (maxSize < 0) { + throw new IllegalArgumentException("Cache size should be greater than or equal to zero"); + } mImages = new LinkedHashMap<>(0, 0.75f, true); mLock = new ReentrantLock(); mMaxSize = maxSize; diff --git a/src/main/java/com/budiyev/android/imageloader/StorageImageCache.java b/src/main/java/com/budiyev/android/imageloader/StorageImageCache.java index c36ee80..a4fbb39 100644 --- a/src/main/java/com/budiyev/android/imageloader/StorageImageCache.java +++ b/src/main/java/com/budiyev/android/imageloader/StorageImageCache.java @@ -78,8 +78,11 @@ public StorageImageCache(@NonNull File directory, long maxSize) { } public StorageImageCache(@NonNull File directory, @NonNull CompressMode compressMode, long maxSize) { - mDirectory = directory; - mCompressMode = compressMode; + mDirectory = InternalUtils.requireNonNull(directory); + mCompressMode = InternalUtils.requireNonNull(compressMode); + if (maxSize < 0L) { + throw new IllegalArgumentException("Cache size should be greater than or equal to zero"); + } mMaxSize = maxSize; }