Skip to content

Commit

Permalink
Override default Encoders (#2349)
Browse files Browse the repository at this point in the history
* Add prepend methods for EncoderRegistry and ResourceEncoderRegistry classes
  • Loading branch information
MistaGreen authored and sjudd committed Sep 8, 2017
1 parent 73a8054 commit 03f5bd4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
44 changes: 38 additions & 6 deletions library/src/main/java/com/bumptech/glide/Registry.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand Down Expand Up @@ -65,11 +66,24 @@ public 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;
}

Expand Down Expand Up @@ -139,12 +153,30 @@ public <Data, TResource> Registry prepend(
* {@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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
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.
Expand All @@ -24,10 +24,14 @@ public synchronized <T> Encoder<T> getEncoder(Class<T> dataClass) {
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
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) {
Expand Down

0 comments on commit 03f5bd4

Please sign in to comment.