Skip to content

Commit

Permalink
Util nullability (#2729)
Browse files Browse the repository at this point in the history
* Add nullability annotations to the util package and its dependencies

Signed-off-by: Alex Saveau <[email protected]>
  • Loading branch information
SUPERCILEX authored and sjudd committed Dec 21, 2017
1 parent a6f1b1c commit bd2f215
Show file tree
Hide file tree
Showing 30 changed files with 264 additions and 187 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.os.Debug;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.test.InstrumentationRegistry;
import android.widget.ImageView;
Expand Down Expand Up @@ -101,7 +102,7 @@ private <T> void loadUntilFirstFinish(
final CountDownLatch latch = new CountDownLatch(1);
callOnMainThread(new Callable<Target<T>>() {
@Override
public Target<T> call() throws Exception {
public Target<T> call() {
builder.into(new Target<T>() {
@Override
public void onStart() {
Expand All @@ -119,7 +120,8 @@ public void onDestroy() {
}

@Override
public void onResourceReady(T resource, Transition<? super T> transition) {
public void onResourceReady(@NonNull T resource,
@Nullable Transition<? super T> transition) {
target.onResourceReady(resource, transition);
latch.countDown();
}
Expand All @@ -141,12 +143,12 @@ public void onLoadFailed(@Nullable Drawable errorDrawable) {
}

@Override
public void getSize(SizeReadyCallback cb) {
public void getSize(@NonNull SizeReadyCallback cb) {
target.getSize(cb);
}

@Override
public void removeCallback(SizeReadyCallback cb) {
public void removeCallback(@NonNull SizeReadyCallback cb) {
target.removeCallback(cb);
}

Expand All @@ -171,7 +173,7 @@ private <T> void loadOnMainThread(final RequestBuilder<T> builder, final Target<
final CountDownLatch latch = new CountDownLatch(1);
callOnMainThread(new Callable<Target<T>>() {
@Override
public Target<T> call() throws Exception {
public Target<T> call() {
builder.into(new Target<T>() {
@Override
public void onStart() {
Expand All @@ -189,7 +191,8 @@ public void onDestroy() {
}

@Override
public void onResourceReady(T resource, Transition<? super T> transition) {
public void onResourceReady(@NonNull T resource,
@Nullable Transition<? super T> transition) {
target.onResourceReady(resource, transition);
if (!Preconditions.checkNotNull(getRequest()).isRunning()) {
latch.countDown();
Expand All @@ -215,12 +218,12 @@ public void onLoadFailed(@Nullable Drawable errorDrawable) {
}

@Override
public void getSize(SizeReadyCallback cb) {
public void getSize(@NonNull SizeReadyCallback cb) {
target.getSize(cb);
}

@Override
public void removeCallback(SizeReadyCallback cb) {
public void removeCallback(@NonNull SizeReadyCallback cb) {
target.removeCallback(cb);
}

Expand Down Expand Up @@ -253,7 +256,7 @@ public void run() {
public void runOnMainThread(final Runnable runnable) {
callOnMainThread(new Callable<Void>() {
@Override
public Void call() throws Exception {
public Void call() {
runnable.run();
return null;
}
Expand Down Expand Up @@ -291,22 +294,22 @@ private interface Waiter {

private static void wait(Waiter waiter) {
boolean isFinished = false;
do {
try {
try {
isFinished = waiter.await(TIMEOUT_SECONDS, TIMEOUT_UNIT);
if (!isFinished) {
throw new RuntimeException("Timed out while waiting");
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
} catch (RuntimeException e) {
if (Debug.isDebuggerConnected()) {
continue;
}
throw e;
}
} while (Debug.isDebuggerConnected() && !isFinished);
do {
try {
try {
isFinished = waiter.await(TIMEOUT_SECONDS, TIMEOUT_UNIT);
if (!isFinished) {
throw new RuntimeException("Timed out while waiting");
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
} catch (RuntimeException e) {
if (Debug.isDebuggerConnected()) {
continue;
}
throw e;
}
} while (Debug.isDebuggerConnected() && !isFinished);
}
}
7 changes: 4 additions & 3 deletions library/src/main/java/com/bumptech/glide/ListPreloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,17 +249,18 @@ private static final class PreloadTarget extends BaseTarget<Object> {
PreloadTarget() { }

@Override
public void onResourceReady(Object resource, Transition<? super Object> transition) {
public void onResourceReady(@NonNull Object resource,
@Nullable Transition<? super Object> transition) {
// Do nothing.
}

@Override
public void getSize(SizeReadyCallback cb) {
public void getSize(@NonNull SizeReadyCallback cb) {
cb.onSizeReady(photoWidth, photoHeight);
}

@Override
public void removeCallback(SizeReadyCallback cb) {
public void removeCallback(@NonNull SizeReadyCallback cb) {
// Do nothing because we don't retain references to SizeReadyCallbacks.
}
}
Expand Down
3 changes: 2 additions & 1 deletion library/src/main/java/com/bumptech/glide/RequestManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,8 @@ private static class ClearTarget extends ViewTarget<View, Object> {
}

@Override
public void onResourceReady(Object resource, Transition<? super Object> transition) {
public void onResourceReady(@NonNull Object resource,
@Nullable Transition<? super Object> transition) {
// Do nothing.
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public LruResourceCache(long size) {
}

@Override
public void setResourceRemovedListener(ResourceRemovedListener listener) {
public void setResourceRemovedListener(@NonNull ResourceRemovedListener listener) {
this.listener = listener;
}

Expand All @@ -35,8 +35,12 @@ protected void onItemEvicted(@NonNull Key key, @Nullable Resource<?> item) {
}

@Override
protected int getSize(Resource<?> item) {
return item.getSize();
protected int getSize(@Nullable Resource<?> item) {
if (item == null) {
return super.getSize(null);
} else {
return item.getSize();
}
}

@SuppressLint("InlinedApi")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ interface ResourceRemovedListener {
* @param key The key.
*/
@Nullable
Resource<?> remove(Key key);
Resource<?> remove(@NonNull Key key);

/**
* Add bitmap to the cache with the given key.
Expand All @@ -53,14 +53,14 @@ interface ResourceRemovedListener {
* @return The old value of key (null if key is not in map).
*/
@Nullable
Resource<?> put(Key key, Resource<?> resource);
Resource<?> put(@NonNull Key key, @Nullable Resource<?> resource);

/**
* Set the listener to be called when a bitmap is removed from the cache.
*
* @param listener The listener.
*/
void setResourceRemovedListener(ResourceRemovedListener listener);
void setResourceRemovedListener(@NonNull ResourceRemovedListener listener);

/**
* Evict all items from the memory cache.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.bumptech.glide.load.engine.cache;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.bumptech.glide.load.Key;
import com.bumptech.glide.load.engine.Resource;

Expand All @@ -25,19 +27,23 @@ public void setSizeMultiplier(float multiplier) {
// Do nothing.
}

@Nullable
@Override
public Resource<?> remove(Key key) {
public Resource<?> remove(@NonNull Key key) {
return null;
}

@Nullable
@Override
public Resource<?> put(Key key, Resource<?> resource) {
listener.onResourceRemoved(resource);
public Resource<?> put(@NonNull Key key, @Nullable Resource<?> resource) {
if (resource != null) {
listener.onResourceRemoved(resource);
}
return null;
}

@Override
public void setResourceRemovedListener(ResourceRemovedListener listener) {
public void setResourceRemovedListener(@NonNull ResourceRemovedListener listener) {
this.listener = listener;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.bumptech.glide.load.model;

import android.support.annotation.Nullable;

/**
* An optional interface that models can implement to enhance control over Glide behaviors.
*/
Expand All @@ -24,5 +26,5 @@ public interface Model {
* the same image. However two requests made with the different models are not exactly the
* same because the way the image is loaded will differ.
*/
boolean isEquivalentTo(Object other);
boolean isEquivalentTo(@Nullable Object other);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import com.bumptech.glide.Glide;
Expand Down Expand Up @@ -322,7 +323,8 @@ Bitmap getResource() {
}

@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
public void onResourceReady(@NonNull Bitmap resource,
@Nullable Transition<? super Bitmap> transition) {
this.resource = resource;
Message msg = handler.obtainMessage(FrameLoaderCallback.MSG_DELAY, this);
handler.sendMessageAtTime(msg, targetTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ public R get(long time, @NonNull TimeUnit timeUnit)
* A callback that should never be invoked directly.
*/
@Override
public void getSize(SizeReadyCallback cb) {
public void getSize(@NonNull SizeReadyCallback cb) {
cb.onSizeReady(width, height);
}

@Override
public void removeCallback(SizeReadyCallback cb) {
public void removeCallback(@NonNull SizeReadyCallback cb) {
// Do nothing because we do not retain references to SizeReadyCallbacks.
}

Expand All @@ -154,31 +154,32 @@ public Request getRequest() {
* A callback that should never be invoked directly.
*/
@Override
public void onLoadCleared(Drawable placeholder) {
public void onLoadCleared(@Nullable Drawable placeholder) {
// Do nothing.
}

/**
* A callback that should never be invoked directly.
*/
@Override
public void onLoadStarted(Drawable placeholder) {
public void onLoadStarted(@Nullable Drawable placeholder) {
// Do nothing.
}

/**
* A callback that should never be invoked directly.
*/
@Override
public synchronized void onLoadFailed(Drawable errorDrawable) {
public synchronized void onLoadFailed(@Nullable Drawable errorDrawable) {
// Ignored, synchronized for backwards compatibility.
}

/**
* A callback that should never be invoked directly.
*/
@Override
public synchronized void onResourceReady(R resource, Transition<? super R> transition) {
public synchronized void onResourceReady(@NonNull R resource,
@Nullable Transition<? super R> transition) {
// Ignored, synchronized for backwards compatibility.
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.content.ComponentName;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.RemoteViews;
import com.bumptech.glide.request.transition.Transition;
import com.bumptech.glide.util.Preconditions;
Expand Down Expand Up @@ -118,7 +120,8 @@ private void update() {
}

@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
public void onResourceReady(@NonNull Bitmap resource,
@Nullable Transition<? super Bitmap> transition) {
this.remoteViews.setImageViewBitmap(this.viewId, resource);
this.update();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.ImageView;
import com.bumptech.glide.request.transition.Transition;
Expand Down Expand Up @@ -98,7 +99,7 @@ public void onLoadCleared(@Nullable Drawable placeholder) {
}

@Override
public void onResourceReady(Z resource, @Nullable Transition<? super Z> transition) {
public void onResourceReady(@NonNull Z resource, @Nullable Transition<? super Z> transition) {
if (transition == null || !transition.transition(resource, this)) {
setResourceInternal(resource);
} else {
Expand Down
Loading

0 comments on commit bd2f215

Please sign in to comment.