Home | History | Annotate | Download | only in transcode
      1 package com.bumptech.glide.load.resource.transcode;
      2 
      3 import com.bumptech.glide.util.MultiClassKey;
      4 
      5 import java.util.HashMap;
      6 import java.util.Map;
      7 
      8 /**
      9  * A class that allows {@link com.bumptech.glide.load.resource.transcode.ResourceTranscoder}s to be registered and
     10  * retrieved by the classes they convert between.
     11  */
     12 public class TranscoderRegistry {
     13     private static final MultiClassKey GET_KEY = new MultiClassKey();
     14 
     15     private final Map<MultiClassKey, ResourceTranscoder<?, ?>> factories =
     16             new HashMap<MultiClassKey, ResourceTranscoder<?, ?>>();
     17 
     18     /**
     19      * Registers the given {@link com.bumptech.glide.load.resource.transcode.ResourceTranscoder} using the given
     20      * classes so it can later be retrieved using the given classes.
     21      *
     22      * @param decodedClass The class of the resource that the transcoder transcodes from.
     23      * @param transcodedClass The class of the resource that the transcoder transcodes to.
     24      * @param transcoder The transcoder.
     25      * @param <Z> The type of the resource that the transcoder transcodes from.
     26      * @param <R> The type of the resource that the transcoder transcodes to.
     27      */
     28     public <Z, R> void register(Class<Z> decodedClass, Class<R> transcodedClass, ResourceTranscoder<Z, R> transcoder) {
     29         factories.put(new MultiClassKey(decodedClass, transcodedClass), transcoder);
     30     }
     31 
     32     /**
     33      * Returns the currently registered {@link com.bumptech.glide.load.resource.transcode.ResourceTranscoder} for the
     34      * given classes.
     35      *
     36      * @param decodedClass The class of the resource that the transcoder transcodes from.
     37      * @param transcodedClass The class of the resource that the transcoder transcodes to.
     38      * @param <Z> The type of the resource that the transcoder transcodes from.
     39      * @param <R> The type of the resource that the transcoder transcodes to.
     40      */
     41     @SuppressWarnings("unchecked")
     42     public <Z, R> ResourceTranscoder<Z, R> get(Class<Z> decodedClass, Class<R> transcodedClass) {
     43         if (decodedClass.equals(transcodedClass)) {
     44             // we know they're the same type (Z and R)
     45             return (ResourceTranscoder<Z, R>) UnitTranscoder.get();
     46         }
     47         final ResourceTranscoder<?, ?> result;
     48         synchronized (GET_KEY) {
     49             GET_KEY.set(decodedClass, transcodedClass);
     50             result = factories.get(GET_KEY);
     51         }
     52         if (result == null) {
     53             throw new IllegalArgumentException("No transcoder registered for " + decodedClass + " and "
     54                     + transcodedClass);
     55         }
     56         return (ResourceTranscoder<Z, R>) result;
     57     }
     58 }
     59