Skip to content

Commit

Permalink
Add nullability annotations to gif modules (#2712)
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 Dec 14, 2017
1 parent 0cffd1d commit f541b65
Show file tree
Hide file tree
Showing 32 changed files with 164 additions and 93 deletions.
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.engine.Resource;
import java.io.IOException;
Expand All @@ -24,7 +25,7 @@ public interface ResourceDecoder<T, Z> {
* <p>Decoders that return {@code true} from {@code handles} may still return {@code null} from
* {@link #decode(Object, int, int, Options)} if the data is partial or formatted incorrectly.
*/
boolean handles(T source, Options options) throws IOException;
boolean handles(@NonNull T source, @NonNull Options options) throws IOException;

/**
* Returns a decoded resource from the given data or null if no resource could be decoded.
Expand All @@ -51,5 +52,6 @@ public interface ResourceDecoder<T, Z> {
* expected type.
*/
@Nullable
Resource<Z> decode(T source, int width, int height, Options options) throws IOException;
Resource<Z> decode(@NonNull T source, int width, int height, @NonNull Options options)
throws IOException;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bumptech.glide.load.engine;

import android.os.Looper;
import android.support.annotation.NonNull;
import com.bumptech.glide.load.Key;
import com.bumptech.glide.util.Preconditions;

Expand Down Expand Up @@ -42,11 +43,13 @@ boolean isCacheable() {
return isCacheable;
}

@NonNull
@Override
public Class<Z> getResourceClass() {
return resource.getResourceClass();
}

@NonNull
@Override
public Z get() {
return resource.get();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bumptech.glide.load.engine;

import android.support.annotation.NonNull;
import android.support.v4.util.Pools;
import com.bumptech.glide.util.Preconditions;
import com.bumptech.glide.util.Synthetic;
Expand Down Expand Up @@ -61,11 +62,13 @@ synchronized void unlock() {
}
}

@NonNull
@Override
public Class<Z> getResourceClass() {
return toWrap.getResourceClass();
}

@NonNull
@Override
public Z get() {
return toWrap.get();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bumptech.glide.load.engine;

import android.support.annotation.NonNull;

/**
* A resource interface that wraps a particular type so that it can be pooled and reused.
Expand All @@ -11,6 +12,7 @@ public interface Resource<Z> {
/**
* Returns the {@link Class} of the wrapped resource.
*/
@NonNull
Class<Z> getResourceClass();

/**
Expand All @@ -23,6 +25,7 @@ public interface Resource<Z> {
* {@link android.graphics.drawable.Drawable Drawable}s should always return a new
* {@link android.graphics.drawable.Drawable Drawable}. </p>
*/
@NonNull
Z get();

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bumptech.glide.load.resource;

import android.support.annotation.NonNull;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.util.Preconditions;

Expand All @@ -18,12 +19,14 @@ public SimpleResource(T data) {
this.data = Preconditions.checkNotNull(data);
}

@NonNull
@SuppressWarnings("unchecked")
@Override
public Class<T> getResourceClass() {
return (Class<T>) data.getClass();
}

@NonNull
@Override
public final T get() {
return data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ public BitmapDrawableDecoder(
}

@Override
public boolean handles(DataType source, Options options) throws IOException {
public boolean handles(@NonNull DataType source, @NonNull Options options) throws IOException {
return decoder.handles(source, options);
}

@Override
public Resource<BitmapDrawable> decode(DataType source, int width, int height, Options options)
public Resource<BitmapDrawable> decode(@NonNull DataType source, int width, int height,
@NonNull Options options)
throws IOException {
Resource<Bitmap> bitmapResource = decoder.decode(source, width, height, options);
return LazyBitmapDrawableResource.obtain(resources, bitmapResource);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bumptech.glide.load.resource.bitmap;

import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.NonNull;
import com.bumptech.glide.load.engine.Initializable;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.drawable.DrawableResource;
Expand All @@ -27,6 +28,7 @@ public BitmapDrawableResource(BitmapDrawable drawable, BitmapPool bitmapPool) {
this.bitmapPool = bitmapPool;
}

@NonNull
@Override
public Class<BitmapDrawable> getResourceClass() {
return BitmapDrawable.class;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bumptech.glide.load.resource.bitmap;

import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.bumptech.glide.load.engine.Initializable;
import com.bumptech.glide.load.engine.Resource;
Expand All @@ -24,24 +25,26 @@ public class BitmapResource implements Resource<Bitmap>,
* @param bitmapPool A non-null {@link com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool}.
*/
@Nullable
public static BitmapResource obtain(@Nullable Bitmap bitmap, BitmapPool bitmapPool) {
public static BitmapResource obtain(@Nullable Bitmap bitmap, @NonNull BitmapPool bitmapPool) {
if (bitmap == null) {
return null;
} else {
return new BitmapResource(bitmap, bitmapPool);
}
}

public BitmapResource(Bitmap bitmap, BitmapPool bitmapPool) {
public BitmapResource(@NonNull Bitmap bitmap, @NonNull BitmapPool bitmapPool) {
this.bitmap = Preconditions.checkNotNull(bitmap, "Bitmap must not be null");
this.bitmapPool = Preconditions.checkNotNull(bitmapPool, "BitmapPool must not be null");
}

@NonNull
@Override
public Class<Bitmap> getResourceClass() {
return Bitmap.class;
}

@NonNull
@Override
public Bitmap get() {
return bitmap;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bumptech.glide.load.resource.bitmap;

import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.ResourceDecoder;
import com.bumptech.glide.load.engine.Resource;
Expand All @@ -20,12 +21,13 @@ public ByteBufferBitmapDecoder(Downsampler downsampler) {
}

@Override
public boolean handles(ByteBuffer source, Options options) throws IOException {
public boolean handles(@NonNull ByteBuffer source, @NonNull Options options) {
return downsampler.handles(source);
}

@Override
public Resource<Bitmap> decode(ByteBuffer source, int width, int height, Options options)
public Resource<Bitmap> decode(@NonNull ByteBuffer source, int width, int height,
@NonNull Options options)
throws IOException {
InputStream is = ByteBufferUtil.toStream(source);
return downsampler.decode(is, width, height, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ private LazyBitmapDrawableResource(Resources resources, Resource<Bitmap> bitmapR
this.bitmapResource = Preconditions.checkNotNull(bitmapResource);
}

@NonNull
@Override
public Class<BitmapDrawable> getResourceClass() {
return BitmapDrawable.class;
}

@NonNull
@Override
public BitmapDrawable get() {
return new BitmapDrawable(resources, bitmapResource.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.ResourceDecoder;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.drawable.ResourceDrawableDecoder;
import com.bumptech.glide.request.target.Target;
import java.io.IOException;

/**
* Decodes {@link Bitmap}s from resource ids.
Expand All @@ -39,14 +39,14 @@ public ResourceBitmapDecoder(ResourceDrawableDecoder drawableDecoder, BitmapPool
}

@Override
public boolean handles(Uri source, Options options) throws IOException {
public boolean handles(@NonNull Uri source, @NonNull Options options) {
return ContentResolver.SCHEME_ANDROID_RESOURCE.equals(source.getScheme());
}

@Nullable
@Override
public Resource<Bitmap> decode(Uri source, int width, int height, Options options)
throws IOException {
public Resource<Bitmap> decode(@NonNull Uri source, int width, int height,
@NonNull Options options) {
Resource<Drawable> drawableResource = drawableDecoder.decode(source, width, height, options);
Drawable drawable = drawableResource.get();
return DrawableToBitmapConverter.convert(bitmapPool, drawable, width, height);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bumptech.glide.load.resource.bitmap;

import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.ResourceDecoder;
import com.bumptech.glide.load.engine.Resource;
Expand All @@ -25,12 +26,13 @@ public StreamBitmapDecoder(Downsampler downsampler, ArrayPool byteArrayPool) {
}

@Override
public boolean handles(InputStream source, Options options) throws IOException {
public boolean handles(@NonNull InputStream source, @NonNull Options options) {
return downsampler.handles(source);
}

@Override
public Resource<Bitmap> decode(InputStream source, int width, int height, Options options)
public Resource<Bitmap> decode(@NonNull InputStream source, int width, int height,
@NonNull Options options)
throws IOException {

// Use to fix the mark limit to avoid allocating buffers that fit entire images.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.bumptech.glide.load.resource.bitmap;

import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.ResourceDecoder;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.util.Util;
import java.io.IOException;

/**
* Passes through a (hopefully) non-owned {@link Bitmap} as a {@link Bitmap} based {@link Resource}
Expand All @@ -14,29 +14,31 @@
public final class UnitBitmapDecoder implements ResourceDecoder<Bitmap, Bitmap> {

@Override
public boolean handles(Bitmap source, Options options) throws IOException {
public boolean handles(@NonNull Bitmap source, @NonNull Options options) {
return true;
}

@Override
public Resource<Bitmap> decode(Bitmap source, int width, int height, Options options)
throws IOException {
public Resource<Bitmap> decode(@NonNull Bitmap source, int width, int height,
@NonNull Options options) {
return new NonOwnedBitmapResource(source);
}

private static final class NonOwnedBitmapResource implements Resource<Bitmap> {

private final Bitmap bitmap;

NonOwnedBitmapResource(Bitmap bitmap) {
NonOwnedBitmapResource(@NonNull Bitmap bitmap) {
this.bitmap = bitmap;
}

@NonNull
@Override
public Class<Bitmap> getResourceClass() {
return Bitmap.class;
}

@NonNull
@Override
public Bitmap get() {
return bitmap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.os.ParcelFileDescriptor;
import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.Option;
Expand Down Expand Up @@ -43,6 +44,7 @@ public class VideoBitmapDecoder implements ResourceDecoder<ParcelFileDescriptor,
"com.bumptech.glide.load.resource.bitmap.VideoBitmapDecode.TargetFrame", DEFAULT_FRAME,
new Option.CacheKeyUpdater<Long>() {
private final ByteBuffer buffer = ByteBuffer.allocate(Long.SIZE / Byte.SIZE);

@Override
public void update(byte[] keyBytes, Long value, MessageDigest messageDigest) {
messageDigest.update(keyBytes);
Expand All @@ -68,6 +70,7 @@ public void update(byte[] keyBytes, Long value, MessageDigest messageDigest) {
null /*defaultValue*/,
new Option.CacheKeyUpdater<Integer>() {
private final ByteBuffer buffer = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE);

@Override
public void update(byte[] keyBytes, Integer value, MessageDigest messageDigest) {
if (value == null) {
Expand Down Expand Up @@ -105,16 +108,16 @@ public VideoBitmapDecoder(BitmapPool bitmapPool) {
}

@Override
public boolean handles(ParcelFileDescriptor data, Options options) {
public boolean handles(@NonNull ParcelFileDescriptor data, @NonNull Options options) {
// Calling setDataSource is expensive so avoid doing so unless we're actually called.
// For non-videos this isn't any cheaper, but for videos it safes the redundant call and
// For non-videos this isn't any cheaper, but for videos it saves the redundant call and
// 50-100ms.
return true;
}

@Override
public Resource<Bitmap> decode(ParcelFileDescriptor resource, int outWidth, int outHeight,
Options options) throws IOException {
public Resource<Bitmap> decode(@NonNull ParcelFileDescriptor resource, int outWidth,
int outHeight, @NonNull Options options) throws IOException {
long frameTimeMicros = options.get(TARGET_FRAME);
if (frameTimeMicros < 0 && frameTimeMicros != DEFAULT_FRAME) {
throw new IllegalArgumentException(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bumptech.glide.load.resource.bytes;

import android.support.annotation.NonNull;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.util.Preconditions;

Expand All @@ -13,11 +14,13 @@ public BytesResource(byte[] bytes) {
this.bytes = Preconditions.checkNotNull(bytes);
}

@NonNull
@Override
public Class<byte[]> getResourceClass() {
return byte[].class;
}

@NonNull
@Override
public byte[] get() {
return bytes;
Expand Down
Loading

0 comments on commit f541b65

Please sign in to comment.