Skip to content

Commit

Permalink
Add nullability annotations to pool package and its dependencies (#2747)
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
  • Loading branch information
SUPERCILEX authored and sjudd committed Dec 23, 2017
1 parent e35a73b commit f37ced1
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ private void logWithTimeAndKey(String message, long startTime, String extraArgs)
+ Thread.currentThread().getName());
}

@NonNull
@Override
public StateVerifier getVerifier() {
return stateVerifier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;
import android.support.v4.util.Pools;
import com.bumptech.glide.load.DataSource;
Expand Down Expand Up @@ -296,6 +297,7 @@ void handleExceptionOnMainThread() {
release(false /*isRemovedFromQueue*/);
}

@NonNull
@Override
public StateVerifier getVerifier() {
return stateVerifier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public synchronized void recycle() {
}
}

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

import android.support.annotation.NonNull;
import android.support.v4.util.Pools;
import com.bumptech.glide.load.Key;
import com.bumptech.glide.util.LruCache;
Expand Down Expand Up @@ -65,6 +66,7 @@ private static final class PoolableDigestContainer implements FactoryPools.Poola
this.messageDigest = messageDigest;
}

@NonNull
@Override
public StateVerifier getVerifier() {
return stateVerifier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.res.Resources.Theme;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.util.Pools;
import android.util.Log;
Expand Down Expand Up @@ -191,6 +192,7 @@ private void init(
status = Status.PENDING;
}

@NonNull
@Override
public StateVerifier getVerifier() {
return stateVerifier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public final class FactoryPools {
private static final int DEFAULT_POOL_SIZE = 20;
private static final Resetter<Object> EMPTY_RESETTER = new Resetter<Object>() {
@Override
public void reset(Object object) {
public void reset(@NonNull Object object) {
// Do nothing.
}
};
Expand All @@ -35,7 +35,8 @@ private FactoryPools() { }
*
* @param <T> The type of object the pool will contains.
*/
public static <T extends Poolable> Pool<T> simple(int size, Factory<T> factory) {
@NonNull
public static <T extends Poolable> Pool<T> simple(int size, @NonNull Factory<T> factory) {
return build(new SimplePool<T>(size), factory);
}

Expand All @@ -49,7 +50,8 @@ public static <T extends Poolable> Pool<T> simple(int size, Factory<T> factory)
*
* @param <T> The type of object the pool will contains.
*/
public static <T extends Poolable> Pool<T> threadSafe(int size, Factory<T> factory) {
@NonNull
public static <T extends Poolable> Pool<T> threadSafe(int size, @NonNull Factory<T> factory) {
return build(new SynchronizedPool<T>(size), factory);
}

Expand All @@ -62,6 +64,7 @@ public static <T extends Poolable> Pool<T> threadSafe(int size, Factory<T> facto
*
* @param <T> The type of object that the {@link List Lists} will contain.
*/
@NonNull
public static <T> Pool<List<T>> threadSafeList() {
return threadSafeList(DEFAULT_POOL_SIZE);
}
Expand All @@ -77,29 +80,35 @@ public static <T> Pool<List<T>> threadSafeList() {
*/
// Public API.
@SuppressWarnings("WeakerAccess")
@NonNull
public static <T> Pool<List<T>> threadSafeList(int size) {
return build(new SynchronizedPool<List<T>>(size), new Factory<List<T>>() {
@NonNull
@Override
public List<T> create() {
return new ArrayList<>();
}
}, new Resetter<List<T>>() {
@Override
public void reset(List<T> object) {
public void reset(@NonNull List<T> object) {
object.clear();
}
});
}

private static <T extends Poolable> Pool<T> build(Pool<T> pool, Factory<T> factory) {
@NonNull
private static <T extends Poolable> Pool<T> build(@NonNull Pool<T> pool,
@NonNull Factory<T> factory) {
return build(pool, factory, FactoryPools.<T>emptyResetter());
}

private static <T> Pool<T> build(Pool<T> pool, Factory<T> factory,
Resetter<T> resetter) {
@NonNull
private static <T> Pool<T> build(@NonNull Pool<T> pool, @NonNull Factory<T> factory,
@NonNull Resetter<T> resetter) {
return new FactoryPool<>(pool, factory, resetter);
}

@NonNull
@SuppressWarnings("unchecked")
private static <T> Resetter<T> emptyResetter() {
return (Resetter<T>) EMPTY_RESETTER;
Expand All @@ -120,14 +129,15 @@ public interface Factory<T> {
* @param <T> The type of Object that will be reset.
*/
public interface Resetter<T> {
void reset(T object);
void reset(@NonNull T object);
}

/**
* Allows additional verification to catch errors caused by using objects while they are in
* an object pool.
*/
public interface Poolable {
@NonNull
StateVerifier getVerifier();
}

Expand All @@ -136,7 +146,7 @@ private static final class FactoryPool<T> implements Pool<T> {
private final Resetter<T> resetter;
private final Pool<T> pool;

FactoryPool(Pool<T> pool, Factory<T> factory, Resetter<T> resetter) {
FactoryPool(@NonNull Pool<T> pool, @NonNull Factory<T> factory, @NonNull Resetter<T> resetter) {
this.pool = pool;
this.factory = factory;
this.resetter = resetter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bumptech.glide.util.pool;

import android.support.annotation.NonNull;
import com.bumptech.glide.util.Synthetic;

/**
Expand All @@ -11,6 +12,7 @@ public abstract class StateVerifier {
/**
* Creates a new {@link StateVerifier} instance.
*/
@NonNull
public static StateVerifier newInstance() {
if (DEBUG) {
return new DebugStateVerifier();
Expand Down Expand Up @@ -68,9 +70,9 @@ public void throwIfRecycled() {
@Override
void setRecycled(boolean isRecycled) {
if (isRecycled) {
this.recycledAtStackTraceException = new RuntimeException("Released");
recycledAtStackTraceException = new RuntimeException("Released");
} else {
this.recycledAtStackTraceException = null;
recycledAtStackTraceException = null;
}
}
}
Expand Down

0 comments on commit f37ced1

Please sign in to comment.