提交 03f5bd40 编写于 作者: A Alex Kalyukin 提交者: Sam

Override default Encoders (#2349)

* Add prepend methods for EncoderRegistry and ResourceEncoderRegistry classes
上级 73a8054f
......@@ -23,6 +23,7 @@ import com.bumptech.glide.provider.ModelToResourceClassCache;
import com.bumptech.glide.provider.ResourceDecoderRegistry;
import com.bumptech.glide.provider.ResourceEncoderRegistry;
import com.bumptech.glide.util.pool.FactoryPools;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
......@@ -65,11 +66,24 @@ public class Registry {
* {@link java.io.FileInputStream} and any other subclass.
*
* <p>If multiple {@link Encoder}s are registered for the same type or super type, the
* {@link Encoder} that is registered first will be used. As a result, it's not currently possible
* to replace Glide's default {@link Encoder}s.
* {@link Encoder} that is registered first will be used.
*/
public <Data> Registry register(Class<Data> dataClass, Encoder<Data> encoder) {
encoderRegistry.add(dataClass, encoder);
encoderRegistry.append(dataClass, encoder);
return this;
}
/**
* Prepends the given {@link Encoder} into the list of available {@link Encoder}s
* so that it is attempted before all later and default {@link Encoder}s for the given
* data class.
*
* <p>This method allows you to replace the default {@link Encoder} because it ensures
* the registered {@link Encoder} will run first. If multiple {@link Encoder}s are registered for
* the same type or super type, the {@link Encoder} that is registered first will be used.
*/
public <Data> Registry prepend(Class<Data> dataClass, Encoder<Data> encoder) {
encoderRegistry.prepend(dataClass, encoder);
return this;
}
......@@ -139,12 +153,30 @@ public class Registry {
* {@link com.bumptech.glide.load.resource.gif.GifDrawable} and any other subclass.
*
* <p>If multiple {@link ResourceEncoder}s are registered for the same type or super type, the
* {@link ResourceEncoder} that is registered first will be used. As a result, it's not currently
* possible to replace Glide's default {@link ResourceEncoder}s.
* {@link ResourceEncoder} that is registered first will be used.
*/
public <TResource> Registry register(Class<TResource> resourceClass,
ResourceEncoder<TResource> encoder) {
resourceEncoderRegistry.add(resourceClass, encoder);
resourceEncoderRegistry.append(resourceClass, encoder);
return this;
}
/**
* 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.
*
* Prepends the given {@link ResourceEncoder} into the list of available {@link ResourceEncoder}s
* so that it is attempted before all later and default {@link ResourceEncoder}s for the given
* data type.
*
* <p>This method allows you to replace the default {@link ResourceEncoder} because it ensures
* the registered {@link ResourceEncoder} will run first. If multiple {@link ResourceEncoder}s are
* registered for the same type or super type, the {@link ResourceEncoder} that is registered
* first will be used.
*/
public <TResource> Registry prepend(Class<TResource> resourceClass,
ResourceEncoder<TResource> encoder) {
resourceEncoderRegistry.prepend(resourceClass, encoder);
return this;
}
......
......@@ -7,7 +7,7 @@ import java.util.ArrayList;
import java.util.List;
/**
* Contains an unordered list of {@link Encoder}s capable of encoding arbitrary data types.
* Contains an ordered list of {@link Encoder}s capable of encoding arbitrary data types.
*/
public class EncoderRegistry {
// TODO: This registry should probably contain a put, rather than a list.
......@@ -24,10 +24,14 @@ public class EncoderRegistry {
return null;
}
public synchronized <T> void add(Class<T> dataClass, Encoder<T> encoder) {
public synchronized <T> void append(Class<T> dataClass, Encoder<T> encoder) {
encoders.add(new Entry<>(dataClass, encoder));
}
public synchronized <T> void prepend(Class<T> dataClass, Encoder<T> encoder) {
encoders.add(0, new Entry<>(dataClass, encoder));
}
private static final class Entry<T> {
private final Class<T> dataClass;
@Synthetic final Encoder<T> encoder;
......
package com.bumptech.glide.provider;
import android.support.annotation.Nullable;
import com.bumptech.glide.load.ResourceEncoder;
import com.bumptech.glide.util.Synthetic;
import java.util.ArrayList;
import java.util.List;
/**
* Contains an unordered list of {@link ResourceEncoder}s capable of encoding arbitrary resource
* Contains an ordered list of {@link ResourceEncoder}s capable of encoding arbitrary resource
* types.
*/
public class ResourceEncoderRegistry {
// TODO: this should probably be a put.
final List<Entry<?>> encoders = new ArrayList<>();
public synchronized <Z> void add(Class<Z> resourceClass, ResourceEncoder<Z> encoder) {
public synchronized <Z> void append(Class<Z> resourceClass, ResourceEncoder<Z> encoder) {
encoders.add(new Entry<>(resourceClass, encoder));
}
public synchronized <Z> void prepend(Class<Z> resourceClass, ResourceEncoder<Z> encoder) {
encoders.add(0, new Entry<>(resourceClass, encoder));
}
@SuppressWarnings("unchecked")
@Nullable
public synchronized <Z> ResourceEncoder<Z> get(Class<Z> resourceClass) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册