From 650bb7770d08b279c2a7f00e25337618da54f7f3 Mon Sep 17 00:00:00 2001 From: Alex Saveau <saveau.alexandre@gmail.com> Date: Sat, 16 Dec 2017 14:23:20 -0800 Subject: [PATCH] Start adding nullability annotations to Registry class and its dependencies Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> --- .../java/com/bumptech/glide/Registry.java | 98 ++++++++++++------- .../glide/load/data/DataRewinder.java | 6 +- .../glide/load/data/DataRewinderRegistry.java | 13 ++- .../glide/load/data/InputStreamRewinder.java | 4 + .../resource/bytes/ByteBufferRewinder.java | 7 +- .../transcode/TranscoderRegistry.java | 21 ++-- .../glide/provider/EncoderRegistry.java | 11 ++- .../provider/ImageHeaderParserRegistry.java | 4 +- .../provider/ResourceDecoderRegistry.java | 31 +++--- .../provider/ResourceEncoderRegistry.java | 13 ++- 10 files changed, 135 insertions(+), 73 deletions(-) diff --git a/library/src/main/java/com/bumptech/glide/Registry.java b/library/src/main/java/com/bumptech/glide/Registry.java index d531f09713..41e4c84327 100644 --- a/library/src/main/java/com/bumptech/glide/Registry.java +++ b/library/src/main/java/com/bumptech/glide/Registry.java @@ -1,5 +1,6 @@ package com.bumptech.glide; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.util.Pools.Pool; import com.bumptech.glide.load.Encoder; @@ -82,8 +83,9 @@ public Registry() { * @deprecated Use the equivalent {@link #append(Class, Class, ModelLoaderFactory)} method * instead. */ + @NonNull @Deprecated - public <Data> Registry register(Class<Data> dataClass, Encoder<Data> encoder) { + public <Data> Registry register(@NonNull Class<Data> dataClass, @NonNull Encoder<Data> encoder) { return append(dataClass, encoder); } @@ -102,7 +104,8 @@ public <Data> Registry register(Class<Data> dataClass, Encoder<Data> encoder) { * * @see #prepend(Class, Encoder) */ - public <Data> Registry append(Class<Data> dataClass, Encoder<Data> encoder) { + @NonNull + public <Data> Registry append(@NonNull Class<Data> dataClass, @NonNull Encoder<Data> encoder) { encoderRegistry.append(dataClass, encoder); return this; } @@ -118,7 +121,8 @@ public <Data> Registry append(Class<Data> dataClass, Encoder<Data> encoder) { * * @see #append(Class, Encoder) */ - public <Data> Registry prepend(Class<Data> dataClass, Encoder<Data> encoder) { + @NonNull + public <Data> Registry prepend(@NonNull Class<Data> dataClass, @NonNull Encoder<Data> encoder) { encoderRegistry.prepend(dataClass, encoder); return this; } @@ -143,10 +147,11 @@ public <Data> Registry prepend(Class<Data> dataClass, Encoder<Data> encoder) { * {@link com.bumptech.glide.load.resource.gif.GifDrawable} etc). * @param decoder The {@link ResourceDecoder} to register. */ + @NonNull public <Data, TResource> Registry append( - Class<Data> dataClass, - Class<TResource> resourceClass, - ResourceDecoder<Data, TResource> decoder) { + @NonNull Class<Data> dataClass, + @NonNull Class<TResource> resourceClass, + @NonNull ResourceDecoder<Data, TResource> decoder) { append(BUCKET_APPEND_ALL, dataClass, resourceClass, decoder); return this; } @@ -172,11 +177,12 @@ public <Data, TResource> Registry append( * {@link com.bumptech.glide.load.resource.gif.GifDrawable} etc). * @param decoder The {@link ResourceDecoder} to register. */ + @NonNull public <Data, TResource> Registry append( - String bucket, - Class<Data> dataClass, - Class<TResource> resourceClass, - ResourceDecoder<Data, TResource> decoder) { + @NonNull String bucket, + @NonNull Class<Data> dataClass, + @NonNull Class<TResource> resourceClass, + @NonNull ResourceDecoder<Data, TResource> decoder) { decoderRegistry.append(bucket, decoder, dataClass, resourceClass); return this; } @@ -201,10 +207,11 @@ public <Data, TResource> Registry append( * {@link com.bumptech.glide.load.resource.gif.GifDrawable} etc). * @param decoder The {@link ResourceDecoder} to register. */ + @NonNull public <Data, TResource> Registry prepend( - Class<Data> dataClass, - Class<TResource> resourceClass, - ResourceDecoder<Data, TResource> decoder) { + @NonNull Class<Data> dataClass, + @NonNull Class<TResource> resourceClass, + @NonNull ResourceDecoder<Data, TResource> decoder) { prepend(BUCKET_PREPEND_ALL, dataClass, resourceClass, decoder); return this; } @@ -230,11 +237,12 @@ public <Data, TResource> Registry prepend( * {@link com.bumptech.glide.load.resource.gif.GifDrawable} etc). * @param decoder The {@link ResourceDecoder} to register. */ + @NonNull public <Data, TResource> Registry prepend( - String bucket, - Class<Data> dataClass, - Class<TResource> resourceClass, - ResourceDecoder<Data, TResource> decoder) { + @NonNull String bucket, + @NonNull Class<Data> dataClass, + @NonNull Class<TResource> resourceClass, + @NonNull ResourceDecoder<Data, TResource> decoder) { decoderRegistry.prepend(bucket, decoder, dataClass, resourceClass); return this; } @@ -255,7 +263,8 @@ public <Data, TResource> Registry prepend( * @param buckets The list of bucket identifiers in order from highest priority to least priority. */ // Final to avoid a PMD error. - public final Registry setResourceDecoderBucketPriorityList(List<String> buckets) { + @NonNull + public final Registry setResourceDecoderBucketPriorityList(@NonNull List<String> buckets) { List<String> modifiedBuckets = new ArrayList<>(buckets); modifiedBuckets.add(0, BUCKET_PREPEND_ALL); modifiedBuckets.add(BUCKET_APPEND_ALL); @@ -279,9 +288,10 @@ public final Registry setResourceDecoderBucketPriorityList(List<String> buckets) * * @deprecated Use the equivalent {@link #append(Class, ResourceEncoder)} method instead. */ + @NonNull @Deprecated public <TResource> Registry register( - Class<TResource> resourceClass, ResourceEncoder<TResource> encoder) { + @NonNull Class<TResource> resourceClass, @NonNull ResourceEncoder<TResource> encoder) { return append(resourceClass, encoder); } @@ -301,8 +311,9 @@ public <TResource> Registry register( * * @see #prepend(Class, ResourceEncoder) */ + @NonNull public <TResource> Registry append( - Class<TResource> resourceClass, ResourceEncoder<TResource> encoder) { + @NonNull Class<TResource> resourceClass, @NonNull ResourceEncoder<TResource> encoder) { resourceEncoderRegistry.append(resourceClass, encoder); return this; } @@ -319,8 +330,9 @@ public <TResource> Registry append( * * @see #append(Class, ResourceEncoder) */ + @NonNull public <TResource> Registry prepend( - Class<TResource> resourceClass, ResourceEncoder<TResource> encoder) { + @NonNull Class<TResource> resourceClass, @NonNull ResourceEncoder<TResource> encoder) { resourceEncoderRegistry.prepend(resourceClass, encoder); return this; } @@ -329,7 +341,8 @@ public <TResource> Registry prepend( * Registers a new {@link com.bumptech.glide.load.data.DataRewinder.Factory} to handle a * non-default data type that can be rewind to allow for efficient reads of file headers. */ - public Registry register(DataRewinder.Factory<?> factory) { + @NonNull + public Registry register(@NonNull DataRewinder.Factory<?> factory) { dataRewinderRegistry.register(factory); return this; } @@ -344,8 +357,10 @@ public Registry register(DataRewinder.Factory<?> factory) { * {@link android.graphics.drawable.BitmapDrawable}). * @param transcoder The {@link ResourceTranscoder} to register. */ - public <TResource, Transcode> Registry register(Class<TResource> resourceClass, - Class<Transcode> transcodeClass, ResourceTranscoder<TResource, Transcode> transcoder) { + @NonNull + public <TResource, Transcode> Registry register( + @NonNull Class<TResource> resourceClass, @NonNull Class<Transcode> transcodeClass, + @NonNull ResourceTranscoder<TResource, Transcode> transcoder) { transcoderRegistry.register(resourceClass, transcodeClass, transcoder); return this; } @@ -354,7 +369,8 @@ public <TResource, Transcode> Registry register(Class<TResource> resourceClass, * Registers a new {@link ImageHeaderParser} that can obtain some basic metadata from an image * header (orientation, type etc). */ - public Registry register(ImageHeaderParser parser) { + @NonNull + public Registry register(@NonNull ImageHeaderParser parser) { imageHeaderParserRegistry.add(parser); return this; } @@ -381,6 +397,7 @@ public Registry register(ImageHeaderParser parser) { * @param dataClass the data class (e.g. {@link java.io.InputStream}, * {@link java.io.FileDescriptor}). */ + @NonNull public <Model, Data> Registry append(Class<Model> modelClass, Class<Data> dataClass, ModelLoaderFactory<Model, Data> factory) { modelLoaderRegistry.append(modelClass, dataClass, factory); @@ -410,6 +427,7 @@ public <Model, Data> Registry append(Class<Model> modelClass, Class<Data> dataCl * @param dataClass the data class (e.g. {@link java.io.InputStream}, * {@link java.io.FileDescriptor}). */ + @NonNull public <Model, Data> Registry prepend(Class<Model> modelClass, Class<Data> dataClass, ModelLoaderFactory<Model, Data> factory) { modelLoaderRegistry.prepend(modelClass, dataClass, factory); @@ -439,6 +457,7 @@ public <Model, Data> Registry prepend(Class<Model> modelClass, Class<Data> dataC * @param dataClass the data class (e.g. {@link java.io.InputStream}, * {@link java.io.FileDescriptor}). */ + @NonNull public <Model, Data> Registry replace( Class<Model> modelClass, Class<Data> dataClass, @@ -449,7 +468,8 @@ public <Model, Data> Registry replace( @Nullable public <Data, TResource, Transcode> LoadPath<Data, TResource, Transcode> getLoadPath( - Class<Data> dataClass, Class<TResource> resourceClass, Class<Transcode> transcodeClass) { + @NonNull Class<Data> dataClass, @NonNull Class<TResource> resourceClass, + @NonNull Class<Transcode> transcodeClass) { LoadPath<Data, TResource, Transcode> result = loadPathCache.get(dataClass, resourceClass, transcodeClass); if (loadPathCache.isEmptyLoadPath(result)) { @@ -471,9 +491,10 @@ public <Data, TResource, Transcode> LoadPath<Data, TResource, Transcode> getLoad return result; } + @NonNull private <Data, TResource, Transcode> List<DecodePath<Data, TResource, Transcode>> getDecodePaths( - Class<Data> dataClass, Class<TResource> resourceClass, Class<Transcode> transcodeClass) { - + @NonNull Class<Data> dataClass, @NonNull Class<TResource> resourceClass, + @NonNull Class<Transcode> transcodeClass) { List<DecodePath<Data, TResource, Transcode>> decodePaths = new ArrayList<>(); List<Class<TResource>> registeredResourceClasses = decoderRegistry.getResourceClasses(dataClass, resourceClass); @@ -495,8 +516,10 @@ private <Data, TResource, Transcode> List<DecodePath<Data, TResource, Transcode> return decodePaths; } + @Nullable public <Model, TResource, Transcode> List<Class<?>> getRegisteredResourceClasses( - Class<Model> modelClass, Class<TResource> resourceClass, Class<Transcode> transcodeClass) { + Class<Model> modelClass, @NonNull Class<TResource> resourceClass, + @NonNull Class<Transcode> transcodeClass) { List<Class<?>> result = modelToResourceClassCache.get(modelClass, resourceClass); if (result == null) { @@ -509,7 +532,7 @@ public <Model, TResource, Transcode> List<Class<?>> getRegisteredResourceClasses List<Class<Transcode>> registeredTranscodeClasses = transcoderRegistry .getTranscodeClasses(registeredResourceClass, transcodeClass); if (!registeredTranscodeClasses.isEmpty() && !result.contains(registeredResourceClass)) { - result.add(registeredResourceClass); + result.add(registeredResourceClass); } } } @@ -520,11 +543,12 @@ public <Model, TResource, Transcode> List<Class<?>> getRegisteredResourceClasses return result; } - public boolean isResourceEncoderAvailable(Resource<?> resource) { + public boolean isResourceEncoderAvailable(@NonNull Resource<?> resource) { return resourceEncoderRegistry.get(resource.getResourceClass()) != null; } - public <X> ResourceEncoder<X> getResultEncoder(Resource<X> resource) + @Nullable + public <X> ResourceEncoder<X> getResultEncoder(@NonNull Resource<X> resource) throws NoResultEncoderAvailableException { ResourceEncoder<X> resourceEncoder = resourceEncoderRegistry.get(resource.getResourceClass()); if (resourceEncoder != null) { @@ -533,8 +557,9 @@ public <X> ResourceEncoder<X> getResultEncoder(Resource<X> resource) throw new NoResultEncoderAvailableException(resource.getResourceClass()); } + @Nullable @SuppressWarnings("unchecked") - public <X> Encoder<X> getSourceEncoder(X data) throws NoSourceEncoderAvailableException { + public <X> Encoder<X> getSourceEncoder(@NonNull X data) throws NoSourceEncoderAvailableException { Encoder<X> encoder = encoderRegistry.getEncoder((Class<X>) data.getClass()); if (encoder != null) { return encoder; @@ -542,11 +567,13 @@ public <X> Encoder<X> getSourceEncoder(X data) throws NoSourceEncoderAvailableEx throw new NoSourceEncoderAvailableException(data.getClass()); } - public <X> DataRewinder<X> getRewinder(X data) { + @NonNull + public <X> DataRewinder<X> getRewinder(@NonNull X data) { return dataRewinderRegistry.build(data); } - public <Model> List<ModelLoader<Model, ?>> getModelLoaders(Model model) { + @NonNull + public <Model> List<ModelLoader<Model, ?>> getModelLoaders(@NonNull Model model) { List<ModelLoader<Model, ?>> result = modelLoaderRegistry.getModelLoaders(model); if (result.isEmpty()) { throw new NoModelLoaderAvailableException(model); @@ -554,6 +581,7 @@ public <X> DataRewinder<X> getRewinder(X data) { return result; } + @NonNull public List<ImageHeaderParser> getImageHeaderParsers() { List<ImageHeaderParser> result = imageHeaderParserRegistry.getParsers(); if (result.isEmpty()) { diff --git a/library/src/main/java/com/bumptech/glide/load/data/DataRewinder.java b/library/src/main/java/com/bumptech/glide/load/data/DataRewinder.java index 65d64706f2..805198ca6e 100644 --- a/library/src/main/java/com/bumptech/glide/load/data/DataRewinder.java +++ b/library/src/main/java/com/bumptech/glide/load/data/DataRewinder.java @@ -1,5 +1,6 @@ package com.bumptech.glide.load.data; +import android.support.annotation.NonNull; import java.io.IOException; /** @@ -20,12 +21,14 @@ interface Factory<T> { /** * Returns a new {@link com.bumptech.glide.load.data.DataRewinder} wrapping the given data. */ - DataRewinder<T> build(T data); + @NonNull + DataRewinder<T> build(@NonNull T data); /** * Returns the class of data this factory can produce * {@link com.bumptech.glide.load.data.DataRewinder}s for. */ + @NonNull Class<T> getDataClass(); } @@ -35,6 +38,7 @@ interface Factory<T> { * * @return An object pointing to the wrapped data. */ + @NonNull T rewindAndGet() throws IOException; /** diff --git a/library/src/main/java/com/bumptech/glide/load/data/DataRewinderRegistry.java b/library/src/main/java/com/bumptech/glide/load/data/DataRewinderRegistry.java index 6949c486b9..9aa20bc8f8 100644 --- a/library/src/main/java/com/bumptech/glide/load/data/DataRewinderRegistry.java +++ b/library/src/main/java/com/bumptech/glide/load/data/DataRewinderRegistry.java @@ -1,5 +1,6 @@ package com.bumptech.glide.load.data; +import android.support.annotation.NonNull; import com.bumptech.glide.util.Preconditions; import java.util.HashMap; import java.util.Map; @@ -12,23 +13,26 @@ public class DataRewinderRegistry { private final Map<Class<?>, DataRewinder.Factory<?>> rewinders = new HashMap<>(); private static final DataRewinder.Factory<?> DEFAULT_FACTORY = new DataRewinder.Factory<Object>() { + @NonNull @Override - public DataRewinder<Object> build(Object data) { + public DataRewinder<Object> build(@NonNull Object data) { return new DefaultRewinder(data); } + @NonNull @Override public Class<Object> getDataClass() { throw new UnsupportedOperationException("Not implemented"); } }; - public synchronized void register(DataRewinder.Factory<?> factory) { + public synchronized void register(@NonNull DataRewinder.Factory<?> factory) { rewinders.put(factory.getDataClass(), factory); } + @NonNull @SuppressWarnings("unchecked") - public synchronized <T> DataRewinder<T> build(T data) { + public synchronized <T> DataRewinder<T> build(@NonNull T data) { Preconditions.checkNotNull(data); DataRewinder.Factory<T> result = (DataRewinder.Factory<T>) rewinders.get(data.getClass()); if (result == null) { @@ -49,10 +53,11 @@ public synchronized <T> DataRewinder<T> build(T data) { private static final class DefaultRewinder implements DataRewinder<Object> { private final Object data; - DefaultRewinder(Object data) { + DefaultRewinder(@NonNull Object data) { this.data = data; } + @NonNull @Override public Object rewindAndGet() { return data; diff --git a/library/src/main/java/com/bumptech/glide/load/data/InputStreamRewinder.java b/library/src/main/java/com/bumptech/glide/load/data/InputStreamRewinder.java index dc7ee528fe..9a0766ce7f 100644 --- a/library/src/main/java/com/bumptech/glide/load/data/InputStreamRewinder.java +++ b/library/src/main/java/com/bumptech/glide/load/data/InputStreamRewinder.java @@ -1,5 +1,6 @@ package com.bumptech.glide.load.data; +import android.support.annotation.NonNull; import com.bumptech.glide.load.engine.bitmap_recycle.ArrayPool; import com.bumptech.glide.load.resource.bitmap.RecyclableBufferedInputStream; import com.bumptech.glide.util.Synthetic; @@ -22,6 +23,7 @@ public final class InputStreamRewinder implements DataRewinder<InputStream> { bufferedStream.mark(MARK_LIMIT); } + @NonNull @Override public InputStream rewindAndGet() throws IOException { bufferedStream.reset(); @@ -44,11 +46,13 @@ public Factory(ArrayPool byteArrayPool) { this.byteArrayPool = byteArrayPool; } + @NonNull @Override public DataRewinder<InputStream> build(InputStream data) { return new InputStreamRewinder(data, byteArrayPool); } + @NonNull @Override public Class<InputStream> getDataClass() { return InputStream.class; diff --git a/library/src/main/java/com/bumptech/glide/load/resource/bytes/ByteBufferRewinder.java b/library/src/main/java/com/bumptech/glide/load/resource/bytes/ByteBufferRewinder.java index 7bf3faa215..a154faafb7 100644 --- a/library/src/main/java/com/bumptech/glide/load/resource/bytes/ByteBufferRewinder.java +++ b/library/src/main/java/com/bumptech/glide/load/resource/bytes/ByteBufferRewinder.java @@ -1,7 +1,7 @@ package com.bumptech.glide.load.resource.bytes; +import android.support.annotation.NonNull; import com.bumptech.glide.load.data.DataRewinder; -import java.io.IOException; import java.nio.ByteBuffer; /** @@ -16,8 +16,9 @@ public ByteBufferRewinder(ByteBuffer buffer) { this.buffer = buffer; } + @NonNull @Override - public ByteBuffer rewindAndGet() throws IOException { + public ByteBuffer rewindAndGet() { buffer.position(0); return buffer; } @@ -32,11 +33,13 @@ public void cleanup() { */ public static class Factory implements DataRewinder.Factory<ByteBuffer> { + @NonNull @Override public DataRewinder<ByteBuffer> build(ByteBuffer data) { return new ByteBufferRewinder(data); } + @NonNull @Override public Class<ByteBuffer> getDataClass() { return ByteBuffer.class; diff --git a/library/src/main/java/com/bumptech/glide/load/resource/transcode/TranscoderRegistry.java b/library/src/main/java/com/bumptech/glide/load/resource/transcode/TranscoderRegistry.java index b632c35ad8..9c907b65f9 100644 --- a/library/src/main/java/com/bumptech/glide/load/resource/transcode/TranscoderRegistry.java +++ b/library/src/main/java/com/bumptech/glide/load/resource/transcode/TranscoderRegistry.java @@ -1,5 +1,6 @@ package com.bumptech.glide.load.resource.transcode; +import android.support.annotation.NonNull; import com.bumptech.glide.util.Synthetic; import java.util.ArrayList; import java.util.List; @@ -21,8 +22,9 @@ public class TranscoderRegistry { * @param <Z> The type of the resource that the transcoder transcodes from. * @param <R> The type of the resource that the transcoder transcodes to. */ - public synchronized <Z, R> void register(Class<Z> decodedClass, Class<R> transcodedClass, - ResourceTranscoder<Z, R> transcoder) { + public synchronized <Z, R> void register( + @NonNull Class<Z> decodedClass, @NonNull Class<R> transcodedClass, + @NonNull ResourceTranscoder<Z, R> transcoder) { transcoders.add(new Entry<>(decodedClass, transcodedClass, transcoder)); } @@ -35,9 +37,10 @@ public synchronized <Z, R> void register(Class<Z> decodedClass, Class<R> transco * @param <Z> The type of the resource that the transcoder transcodes from. * @param <R> The type of the resource that the transcoder transcodes to. */ + @NonNull @SuppressWarnings("unchecked") - public synchronized <Z, R> ResourceTranscoder<Z, R> get(Class<Z> resourceClass, - Class<R> transcodedClass) { + public synchronized <Z, R> ResourceTranscoder<Z, R> get( + @NonNull Class<Z> resourceClass, @NonNull Class<R> transcodedClass) { // For example, there may be a transcoder that can convert a GifDrawable to a Drawable, which // will be caught above. However, if there is no registered transcoder, we can still just use // the UnitTranscoder to return the Drawable because the transcode class (Drawable) is @@ -55,8 +58,9 @@ public synchronized <Z, R> ResourceTranscoder<Z, R> get(Class<Z> resourceClass, "No transcoder registered to transcode from " + resourceClass + " to " + transcodedClass); } - public synchronized <Z, R> List<Class<R>> getTranscodeClasses(Class<Z> resourceClass, - Class<R> transcodeClass) { + @NonNull + public synchronized <Z, R> List<Class<R>> getTranscodeClasses( + @NonNull Class<Z> resourceClass, @NonNull Class<R> transcodeClass) { List<Class<R>> transcodeClasses = new ArrayList<>(); // GifDrawable -> Drawable is just the UnitTranscoder, as is GifDrawable -> GifDrawable. if (transcodeClass.isAssignableFrom(resourceClass)) { @@ -78,7 +82,8 @@ private static final class Entry<Z, R> { private final Class<R> toClass; @Synthetic final ResourceTranscoder<Z, R> transcoder; - Entry(Class<Z> fromClass, Class<R> toClass, ResourceTranscoder<Z, R> transcoder) { + Entry(@NonNull Class<Z> fromClass, @NonNull Class<R> toClass, + @NonNull ResourceTranscoder<Z, R> transcoder) { this.fromClass = fromClass; this.toClass = toClass; this.transcoder = transcoder; @@ -90,7 +95,7 @@ private static final class Entry<Z, R> { * we can fulfill requests for a more generic parent class (like Drawable). As a result, we * check fromClass and toClass in different orders. */ - public boolean handles(Class<?> fromClass, Class<?> toClass) { + public boolean handles(@NonNull Class<?> fromClass, @NonNull Class<?> toClass) { return this.fromClass.isAssignableFrom(fromClass) && toClass.isAssignableFrom(this.toClass); } } diff --git a/library/src/main/java/com/bumptech/glide/provider/EncoderRegistry.java b/library/src/main/java/com/bumptech/glide/provider/EncoderRegistry.java index 97a5061808..ca5148a0f5 100644 --- a/library/src/main/java/com/bumptech/glide/provider/EncoderRegistry.java +++ b/library/src/main/java/com/bumptech/glide/provider/EncoderRegistry.java @@ -1,5 +1,6 @@ package com.bumptech.glide.provider; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import com.bumptech.glide.load.Encoder; import com.bumptech.glide.util.Synthetic; @@ -15,7 +16,7 @@ public class EncoderRegistry { @SuppressWarnings("unchecked") @Nullable - public synchronized <T> Encoder<T> getEncoder(Class<T> dataClass) { + public synchronized <T> Encoder<T> getEncoder(@NonNull Class<T> dataClass) { for (Entry<?> entry : encoders) { if (entry.handles(dataClass)) { return (Encoder<T>) entry.encoder; @@ -24,11 +25,11 @@ public synchronized <T> Encoder<T> getEncoder(Class<T> dataClass) { return null; } - public synchronized <T> void append(Class<T> dataClass, Encoder<T> encoder) { + public synchronized <T> void append(@NonNull Class<T> dataClass, @NonNull Encoder<T> encoder) { encoders.add(new Entry<>(dataClass, encoder)); } - public synchronized <T> void prepend(Class<T> dataClass, Encoder<T> encoder) { + public synchronized <T> void prepend(@NonNull Class<T> dataClass, @NonNull Encoder<T> encoder) { encoders.add(0, new Entry<>(dataClass, encoder)); } @@ -36,12 +37,12 @@ private static final class Entry<T> { private final Class<T> dataClass; @Synthetic @SuppressWarnings("WeakerAccess") final Encoder<T> encoder; - Entry(Class<T> dataClass, Encoder<T> encoder) { + Entry(@NonNull Class<T> dataClass, @NonNull Encoder<T> encoder) { this.dataClass = dataClass; this.encoder = encoder; } - boolean handles(Class<?> dataClass) { + boolean handles(@NonNull Class<?> dataClass) { return this.dataClass.isAssignableFrom(dataClass); } } diff --git a/library/src/main/java/com/bumptech/glide/provider/ImageHeaderParserRegistry.java b/library/src/main/java/com/bumptech/glide/provider/ImageHeaderParserRegistry.java index af329320ef..902fc1bd49 100644 --- a/library/src/main/java/com/bumptech/glide/provider/ImageHeaderParserRegistry.java +++ b/library/src/main/java/com/bumptech/glide/provider/ImageHeaderParserRegistry.java @@ -1,5 +1,6 @@ package com.bumptech.glide.provider; +import android.support.annotation.NonNull; import com.bumptech.glide.load.ImageHeaderParser; import java.util.ArrayList; import java.util.List; @@ -10,11 +11,12 @@ public final class ImageHeaderParserRegistry { private final List<ImageHeaderParser> parsers = new ArrayList<>(); + @NonNull public synchronized List<ImageHeaderParser> getParsers() { return parsers; } - public synchronized void add(ImageHeaderParser parser) { + public synchronized void add(@NonNull ImageHeaderParser parser) { parsers.add(parser); } } diff --git a/library/src/main/java/com/bumptech/glide/provider/ResourceDecoderRegistry.java b/library/src/main/java/com/bumptech/glide/provider/ResourceDecoderRegistry.java index b7e95fd406..831453779a 100644 --- a/library/src/main/java/com/bumptech/glide/provider/ResourceDecoderRegistry.java +++ b/library/src/main/java/com/bumptech/glide/provider/ResourceDecoderRegistry.java @@ -1,5 +1,6 @@ package com.bumptech.glide.provider; +import android.support.annotation.NonNull; import com.bumptech.glide.load.ResourceDecoder; import com.bumptech.glide.util.Synthetic; import java.util.ArrayList; @@ -16,7 +17,7 @@ public class ResourceDecoderRegistry { private final List<String> bucketPriorityList = new ArrayList<>(); private final Map<String, List<Entry<?, ?>>> decoders = new HashMap<>(); - public synchronized void setBucketPriorityList(List<String> buckets) { + public synchronized void setBucketPriorityList(@NonNull List<String> buckets) { List<String> previousBuckets = new ArrayList<>(bucketPriorityList); bucketPriorityList.clear(); bucketPriorityList.addAll(buckets); @@ -29,9 +30,10 @@ public synchronized void setBucketPriorityList(List<String> buckets) { } } + @NonNull @SuppressWarnings("unchecked") - public synchronized <T, R> List<ResourceDecoder<T, R>> getDecoders(Class<T> dataClass, - Class<R> resourceClass) { + public synchronized <T, R> List<ResourceDecoder<T, R>> getDecoders(@NonNull Class<T> dataClass, + @NonNull Class<R> resourceClass) { List<ResourceDecoder<T, R>> result = new ArrayList<>(); for (String bucket : bucketPriorityList) { List<Entry<?, ?>> entries = decoders.get(bucket); @@ -49,9 +51,10 @@ public synchronized <T, R> List<ResourceDecoder<T, R>> getDecoders(Class<T> data return result; } + @NonNull @SuppressWarnings("unchecked") - public synchronized <T, R> List<Class<R>> getResourceClasses(Class<T> dataClass, - Class<R> resourceClass) { + public synchronized <T, R> List<Class<R>> getResourceClasses(@NonNull Class<T> dataClass, + @NonNull Class<R> resourceClass) { List<Class<R>> result = new ArrayList<>(); for (String bucket : bucketPriorityList) { List<Entry<?, ?>> entries = decoders.get(bucket); @@ -67,17 +70,20 @@ public synchronized <T, R> List<Class<R>> getResourceClasses(Class<T> dataClass, return result; } - public synchronized <T, R> void append( - String bucket, ResourceDecoder<T, R> decoder, Class<T> dataClass, Class<R> resourceClass) { + public synchronized <T, R> void append(@NonNull String bucket, + @NonNull ResourceDecoder<T, R> decoder, + @NonNull Class<T> dataClass, @NonNull Class<R> resourceClass) { getOrAddEntryList(bucket).add(new Entry<>(dataClass, resourceClass, decoder)); } - public synchronized <T, R> void prepend( - String bucket, ResourceDecoder<T, R> decoder, Class<T> dataClass, Class<R> resourceClass) { + public synchronized <T, R> void prepend(@NonNull String bucket, + @NonNull ResourceDecoder<T, R> decoder, + @NonNull Class<T> dataClass, @NonNull Class<R> resourceClass) { getOrAddEntryList(bucket).add(0, new Entry<>(dataClass, resourceClass, decoder)); } - private synchronized List<Entry<?, ?>> getOrAddEntryList(String bucket) { + @NonNull + private synchronized List<Entry<?, ?>> getOrAddEntryList(@NonNull String bucket) { if (!bucketPriorityList.contains(bucket)) { // Add this unspecified bucket as a low priority bucket. bucketPriorityList.add(bucket); @@ -95,13 +101,14 @@ private static class Entry<T, R> { @Synthetic final Class<R> resourceClass; @Synthetic final ResourceDecoder<T, R> decoder; - public Entry(Class<T> dataClass, Class<R> resourceClass, ResourceDecoder<T, R> decoder) { + public Entry(@NonNull Class<T> dataClass, @NonNull Class<R> resourceClass, + ResourceDecoder<T, R> decoder) { this.dataClass = dataClass; this.resourceClass = resourceClass; this.decoder = decoder; } - public boolean handles(Class<?> dataClass, Class<?> resourceClass) { + public boolean handles(@NonNull Class<?> dataClass, @NonNull Class<?> resourceClass) { return this.dataClass.isAssignableFrom(dataClass) && resourceClass .isAssignableFrom(this.resourceClass); } diff --git a/library/src/main/java/com/bumptech/glide/provider/ResourceEncoderRegistry.java b/library/src/main/java/com/bumptech/glide/provider/ResourceEncoderRegistry.java index 54430423f9..b1bd2977a4 100644 --- a/library/src/main/java/com/bumptech/glide/provider/ResourceEncoderRegistry.java +++ b/library/src/main/java/com/bumptech/glide/provider/ResourceEncoderRegistry.java @@ -1,5 +1,6 @@ package com.bumptech.glide.provider; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import com.bumptech.glide.load.ResourceEncoder; import com.bumptech.glide.util.Synthetic; @@ -14,17 +15,19 @@ public class ResourceEncoderRegistry { // TODO: this should probably be a put. private final List<Entry<?>> encoders = new ArrayList<>(); - public synchronized <Z> void append(Class<Z> resourceClass, ResourceEncoder<Z> encoder) { + public synchronized <Z> void append(@NonNull Class<Z> resourceClass, + @NonNull ResourceEncoder<Z> encoder) { encoders.add(new Entry<>(resourceClass, encoder)); } - public synchronized <Z> void prepend(Class<Z> resourceClass, ResourceEncoder<Z> encoder) { + public synchronized <Z> void prepend(@NonNull Class<Z> resourceClass, + @NonNull ResourceEncoder<Z> encoder) { encoders.add(0, new Entry<>(resourceClass, encoder)); } @SuppressWarnings("unchecked") @Nullable - public synchronized <Z> ResourceEncoder<Z> get(Class<Z> resourceClass) { + public synchronized <Z> ResourceEncoder<Z> get(@NonNull Class<Z> resourceClass) { //noinspection ForLoopReplaceableByForEach to improve perf for (int i = 0, size = encoders.size(); i < size; i++) { Entry<?> entry = encoders.get(i); @@ -40,13 +43,13 @@ private static final class Entry<T> { private final Class<T> resourceClass; @Synthetic final ResourceEncoder<T> encoder; - Entry(Class<T> resourceClass, ResourceEncoder<T> encoder) { + Entry(@NonNull Class<T> resourceClass, @NonNull ResourceEncoder<T> encoder) { this.resourceClass = resourceClass; this.encoder = encoder; } @Synthetic - boolean handles(Class<?> resourceClass) { + boolean handles(@NonNull Class<?> resourceClass) { return this.resourceClass.isAssignableFrom(resourceClass); } }