Skip to content

Commit

Permalink
Start adding nullability annotations to Registry class and its depend…
Browse files Browse the repository at this point in the history
…encies

Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
  • Loading branch information
SUPERCILEX authored and sjudd committed Dec 20, 2017
1 parent 04b9c11 commit 650bb77
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 73 deletions.
98 changes: 63 additions & 35 deletions library/src/main/java/com/bumptech/glide/Registry.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}

Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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);
Expand 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);
}

Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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)) {
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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);
}
}
}
Expand All @@ -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) {
Expand All @@ -533,27 +557,31 @@ 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;
}
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);
}
return result;
}

@NonNull
public List<ImageHeaderParser> getImageHeaderParsers() {
List<ImageHeaderParser> result = imageHeaderParserRegistry.getParsers();
if (result.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bumptech.glide.load.data;

import android.support.annotation.NonNull;
import java.io.IOException;

/**
Expand All @@ -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();
}

Expand All @@ -35,6 +38,7 @@ interface Factory<T> {
*
* @return An object pointing to the wrapped data.
*/
@NonNull
T rewindAndGet() throws IOException;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand All @@ -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;
Expand Down
Loading

0 comments on commit 650bb77

Please sign in to comment.