Skip to content

Commit

Permalink
Start adding nullability annotations to load package (#2797)
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Saveau <[email protected]>
  • Loading branch information
SUPERCILEX authored and sjudd committed Jan 23, 2018
1 parent 17e0943 commit e9f682f
Show file tree
Hide file tree
Showing 35 changed files with 197 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class ErrorHandlingTest {
private Context context;

@Before
public void setUp() throws InterruptedException {
public void setUp() {
MockitoAnnotations.initMocks(this);
context = InstrumentationRegistry.getTargetContext();
}
Expand Down Expand Up @@ -82,8 +82,7 @@ public void load_whenEncoderFails_callsUncaughtThrowableStrategy() {
}

@Test
public void load_whenLoadSucceeds_butEncoderFails_doesNotCallOnLoadFailed()
throws InterruptedException {
public void load_whenLoadSucceeds_butEncoderFails_doesNotCallOnLoadFailed() {
WaitForErrorStrategy strategy = new WaitForErrorStrategy();
Glide.init(context,
new GlideBuilder()
Expand All @@ -110,8 +109,7 @@ public void load_whenLoadSucceeds_butEncoderFails_doesNotCallOnLoadFailed()
}

@Test
public void clearRequest_withError_afterPrimaryFails_clearsErrorRequest()
throws InterruptedException {
public void clearRequest_withError_afterPrimaryFails_clearsErrorRequest() {
WaitModel<Integer> errorModel = WaitModelLoader.Factory.waitOn(ResourceIds.raw.canonical);

FutureTarget<Drawable> target =
Expand Down Expand Up @@ -151,6 +149,7 @@ private static final class FailEncoder implements ResourceEncoder<Bitmap> {

static final RuntimeException TO_THROW = new RuntimeException();

@NonNull
@Override
public EncodeStrategy getEncodeStrategy(@NonNull Options options) {
return EncodeStrategy.TRANSFORMED;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public class ReEncodingGifResourceEncoder implements ResourceEncoder<GifDrawable
public static final Option<Boolean> ENCODE_TRANSFORMATION =
Option.disk(KEY_ENCODE_TRANSFORMATION, false, new Option.CacheKeyUpdater<Boolean>() {
@Override
public void update(byte[] keyBytes, Boolean value, MessageDigest messageDigest) {
public void update(@NonNull byte[] keyBytes, @NonNull Boolean value,
@NonNull MessageDigest messageDigest) {
if (value) {
messageDigest.update(keyBytes);
}
Expand Down Expand Up @@ -81,6 +82,7 @@ public ReEncodingGifResourceEncoder(@NonNull Context context, @NonNull BitmapPoo
this.factory = factory;
}

@NonNull
@Override
public EncodeStrategy getEncodeStrategy(@NonNull Options options) {
Boolean encodeTransformation = options.get(ENCODE_TRANSFORMATION);
Expand Down Expand Up @@ -213,7 +215,8 @@ AnimatedGifEncoder buildEncoder() {
return new AnimatedGifEncoder();
}

Resource<Bitmap> buildFrameResource(Bitmap bitmap, BitmapPool bitmapPool) {
@NonNull
Resource<Bitmap> buildFrameResource(@NonNull Bitmap bitmap, @NonNull BitmapPool bitmapPool) {
return new BitmapResource(bitmap, bitmapPool);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bumptech.glide.load;

import android.support.annotation.NonNull;
import com.bumptech.glide.load.engine.bitmap_recycle.ArrayPool;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -10,9 +11,9 @@
*/
public interface ImageHeaderParser {
/**
* A constant indicating we were unable to parse the orientation from the image either because
* no exif segment containing orientation data existed, or because of an I/O error attempting to
* read the exif segment.
* A constant indicating we were unable to parse the orientation from the image either because no
* exif segment containing orientation data existed, or because of an I/O error attempting to read
* the exif segment.
*/
int UNKNOWN_ORIENTATION = -1;

Expand All @@ -32,10 +33,9 @@ enum ImageType {
WEBP_A(true),
/** WebP type without alpha. */
WEBP(false),
/**
* Unrecognized type.
*/
/** Unrecognized type. */
UNKNOWN(false);

private final boolean hasAlpha;

ImageType(boolean hasAlpha) {
Expand All @@ -47,8 +47,11 @@ public boolean hasAlpha() {
}
}

ImageType getType(InputStream is) throws IOException;
ImageType getType(ByteBuffer byteBuffer) throws IOException;
@NonNull
ImageType getType(@NonNull InputStream is) throws IOException;

@NonNull
ImageType getType(@NonNull ByteBuffer byteBuffer) throws IOException;

/**
* Parse the orientation from the image header. If it doesn't handle this image type (or this is
Expand All @@ -57,6 +60,8 @@ public boolean hasAlpha() {
* @return The exif orientation if present or -1 if the header couldn't be parsed or doesn't
* contain an orientation
*/
int getOrientation(InputStream is, ArrayPool byteArrayPool) throws IOException;
int getOrientation(ByteBuffer byteBuffer, ArrayPool byteArrayPool) throws IOException;
int getOrientation(@NonNull InputStream is, @NonNull ArrayPool byteArrayPool) throws IOException;

int getOrientation(@NonNull ByteBuffer byteBuffer, @NonNull ArrayPool byteArrayPool)
throws IOException;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bumptech.glide.load;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.bumptech.glide.load.ImageHeaderParser.ImageType;
import com.bumptech.glide.load.engine.bitmap_recycle.ArrayPool;
Expand All @@ -20,8 +21,9 @@ public final class ImageHeaderParserUtils {
private ImageHeaderParserUtils() { }

/** Returns the ImageType for the given InputStream. */
public static ImageType getType(List<ImageHeaderParser> parsers, @Nullable InputStream is,
ArrayPool byteArrayPool) throws IOException {
@NonNull
public static ImageType getType(@NonNull List<ImageHeaderParser> parsers,
@Nullable InputStream is, @NonNull ArrayPool byteArrayPool) throws IOException {
if (is == null) {
return ImageType.UNKNOWN;
}
Expand All @@ -48,7 +50,9 @@ public static ImageType getType(List<ImageHeaderParser> parsers, @Nullable Input
}

/** Returns the ImageType for the given ByteBuffer. */
public static ImageType getType(List<ImageHeaderParser> parsers, @Nullable ByteBuffer buffer)
@NonNull
public static ImageType getType(@NonNull List<ImageHeaderParser> parsers,
@Nullable ByteBuffer buffer)
throws IOException {
if (buffer == null) {
return ImageType.UNKNOWN;
Expand All @@ -66,9 +70,11 @@ public static ImageType getType(List<ImageHeaderParser> parsers, @Nullable ByteB
return ImageType.UNKNOWN;
}

/** Returns the orientation for the given InputStream. */
public static int getOrientation(List<ImageHeaderParser> parsers, @Nullable InputStream is,
ArrayPool byteArrayPool) throws IOException {
/**
* Returns the orientation for the given InputStream.
*/
public static int getOrientation(@NonNull List<ImageHeaderParser> parsers,
@Nullable InputStream is, @NonNull ArrayPool byteArrayPool) throws IOException {
if (is == null) {
return ImageHeaderParser.UNKNOWN_ORIENTATION;
}
Expand Down
8 changes: 8 additions & 0 deletions library/src/main/java/com/bumptech/glide/load/Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,17 @@ public interface Key {
*/
void updateDiskCacheKey(@NonNull MessageDigest messageDigest);

/**
* For caching to work correctly, implementations <em>must</em> implement this method and
* {@link #hashCode()}.
*/
@Override
boolean equals(Object o);

/**
* For caching to work correctly, implementations <em>must</em> implement this method and
* {@link #equals(Object)}.
*/
@Override
int hashCode();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ public class MultiTransformation<T> implements Transformation<T> {

@SafeVarargs
@SuppressWarnings("varargs")
public MultiTransformation(Transformation<T>... transformations) {
public MultiTransformation(@NonNull Transformation<T>... transformations) {
if (transformations.length == 0) {
throw new IllegalArgumentException(
"MultiTransformation must contain at least one Transformation");
}
this.transformations = Arrays.asList(transformations);
}

public MultiTransformation(Collection<? extends Transformation<T>> transformationList) {
public MultiTransformation(@NonNull Collection<? extends Transformation<T>> transformationList) {
if (transformationList.isEmpty()) {
throw new IllegalArgumentException(
"MultiTransformation must contain at least one Transformation");
Expand Down
31 changes: 21 additions & 10 deletions library/src/main/java/com/bumptech/glide/load/Option.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bumptech.glide.load;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.bumptech.glide.util.Preconditions;
import java.security.MessageDigest;
Expand All @@ -26,7 +27,8 @@
public final class Option<T> {
private static final CacheKeyUpdater<Object> EMPTY_UPDATER = new CacheKeyUpdater<Object>() {
@Override
public void update(byte[] keyBytes, Object value, MessageDigest messageDigest) {
public void update(@NonNull byte[] keyBytes, @NonNull Object value,
@NonNull MessageDigest messageDigest) {
// Do nothing.
}
};
Expand All @@ -43,8 +45,9 @@ public void update(byte[] keyBytes, Object value, MessageDigest messageDigest) {
* @param key A unique package prefixed {@link String} that identifies this option (must be
* stable across builds, so {@link Class#getName()} should <em>not</em> be used).
*/
public static <T> Option<T> memory(String key) {
return new Option<>(key, null /*defaultValue*/, Option.<T>emptyUpdater());
@NonNull
public static <T> Option<T> memory(@NonNull String key) {
return new Option<>(key, null, Option.<T>emptyUpdater());
}

/**
Expand All @@ -54,7 +57,8 @@ public static <T> Option<T> memory(String key) {
* @param key A unique package prefixed {@link String} that identifies this option (must be
* stable across builds, so {@link Class#getName()} should <em>not</em> be used).
*/
public static <T> Option<T> memory(String key, T defaultValue) {
@NonNull
public static <T> Option<T> memory(@NonNull String key, @NonNull T defaultValue) {
return new Option<>(key, defaultValue, Option.<T>emptyUpdater());
}

Expand All @@ -65,8 +69,10 @@ public static <T> Option<T> memory(String key, T defaultValue) {
* @param key A unique package prefixed {@link String} that identifies this option (must be
* stable across builds, so {@link Class#getName()} should <em>not</em> be used).
*/
public static <T> Option<T> disk(String key, CacheKeyUpdater<T> cacheKeyUpdater) {
return new Option<>(key, null /*defaultValue*/, cacheKeyUpdater);
@NonNull
public static <T> Option<T> disk(@NonNull String key,
@NonNull CacheKeyUpdater<T> cacheKeyUpdater) {
return new Option<>(key, null, cacheKeyUpdater);
}

/**
Expand All @@ -77,11 +83,14 @@ public static <T> Option<T> disk(String key, CacheKeyUpdater<T> cacheKeyUpdater)
* @param key A unique package prefixed {@link String} that identifies this option (must be
* stable across builds, so {@link Class#getName()} should <em>not</em> be used).
*/
public static <T> Option<T> disk(String key, T defaultValue, CacheKeyUpdater<T> cacheKeyUpdater) {
@NonNull
public static <T> Option<T> disk(@NonNull String key, @Nullable T defaultValue,
@NonNull CacheKeyUpdater<T> cacheKeyUpdater) {
return new Option<>(key, defaultValue, cacheKeyUpdater);
}

private Option(String key, T defaultValue, CacheKeyUpdater<T> cacheKeyUpdater) {
private Option(@NonNull String key, @Nullable T defaultValue,
@NonNull CacheKeyUpdater<T> cacheKeyUpdater) {
this.key = Preconditions.checkNotEmpty(key);
this.defaultValue = defaultValue;
this.cacheKeyUpdater = Preconditions.checkNotNull(cacheKeyUpdater);
Expand All @@ -102,10 +111,11 @@ public T getDefaultValue() {
* value using the {@link com.bumptech.glide.load.Option.CacheKeyUpdater} optionally provided in
* the constructor.
*/
public void update(T value, MessageDigest messageDigest) {
public void update(@NonNull T value, @NonNull MessageDigest messageDigest) {
cacheKeyUpdater.update(getKeyBytes(), value, messageDigest);
}

@NonNull
private byte[] getKeyBytes() {
if (keyBytes == null) {
keyBytes = key.getBytes(Key.CHARSET);
Expand All @@ -127,6 +137,7 @@ public int hashCode() {
return key.hashCode();
}

@NonNull
@SuppressWarnings("unchecked")
private static <T> CacheKeyUpdater<T> emptyUpdater() {
return (CacheKeyUpdater<T>) EMPTY_UPDATER;
Expand Down Expand Up @@ -162,6 +173,6 @@ public interface CacheKeyUpdater<T> {
* to a byte array using some stable mechanism and then call
* {@link MessageDigest#update(byte[])} to update the given digest.
*/
void update(byte[] keyBytes, T value, MessageDigest messageDigest);
void update(@NonNull byte[] keyBytes, @NonNull T value, @NonNull MessageDigest messageDigest);
}
}
12 changes: 8 additions & 4 deletions library/src/main/java/com/bumptech/glide/load/Options.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bumptech.glide.load;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.util.ArrayMap;
import android.support.v4.util.SimpleArrayMap;
import java.security.MessageDigest;
Expand All @@ -11,17 +12,19 @@
public final class Options implements Key {
private final ArrayMap<Option<?>, Object> values = new ArrayMap<>();

public void putAll(Options other) {
public void putAll(@NonNull Options other) {
values.putAll((SimpleArrayMap<Option<?>, Object>) other.values);
}

public <T> Options set(Option<T> option, T value) {
@NonNull
public <T> Options set(@NonNull Option<T> option, @NonNull T value) {
values.put(option, value);
return this;
}

@Nullable
@SuppressWarnings("unchecked")
public <T> T get(Option<T> option) {
public <T> T get(@NonNull Option<T> option) {
return values.containsKey(option) ? (T) values.get(option) : option.getDefaultValue();
}

Expand Down Expand Up @@ -56,7 +59,8 @@ public String toString() {
}

@SuppressWarnings("unchecked")
private static <T> void updateDiskCacheKey(Option<T> option, Object value, MessageDigest md) {
private static <T> void updateDiskCacheKey(@NonNull Option<T> option, @NonNull Object value,
@NonNull MessageDigest md) {
option.update((T) value, md);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
*/
public interface ResourceEncoder<T> extends Encoder<Resource<T>> {
// specializing the generic arguments
@NonNull
EncodeStrategy getEncodeStrategy(@NonNull Options options);
}
14 changes: 0 additions & 14 deletions library/src/main/java/com/bumptech/glide/load/Transformation.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,4 @@ public interface Transformation<T> extends Key {
@NonNull
Resource<T> transform(@NonNull Context context, @NonNull Resource<T> resource,
int outWidth, int outHeight);

/**
* For caching to work correctly, implementations <em>must</em> implement this method and
* {@link #hashCode()}.
*/
@Override
boolean equals(Object o);

/**
* For caching to work correctly, implementations <em>must</em> implement this method and
* {@link #equals(Object)}.
*/
@Override
int hashCode();
}
Loading

0 comments on commit e9f682f

Please sign in to comment.