• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.bumptech.glide.provider;
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.provider.DataLoadProvider}s to be registered and retrieved by the
10  * data and resource classes they provide encoders and decoders for.
11  */
12 public class DataLoadProviderRegistry {
13     private static final MultiClassKey GET_KEY = new MultiClassKey();
14 
15     private final Map<MultiClassKey, DataLoadProvider<?, ?>> providers =
16             new HashMap<MultiClassKey, DataLoadProvider<?, ?>>();
17 
18     /**
19      * Registers the given {@link com.bumptech.glide.provider.DataLoadProvider} using the given classes so it can later
20      * be retrieved using the given classes.
21      *
22      * @param dataClass The class of the data that the provider provides encoders and decoders for.
23      * @param resourceClass The class of the resource that the provider provides encoders and decoders for.
24      * @param provider The provider.
25      * @param <T> The type of the data that the provider provides encoders and decoders for.
26      * @param <Z> The type of the resource that the provider provides encoders and decoders for.
27      */
register(Class<T> dataClass, Class<Z> resourceClass, DataLoadProvider<T, Z> provider)28     public <T, Z> void register(Class<T> dataClass, Class<Z> resourceClass, DataLoadProvider<T, Z> provider) {
29         //TODO: maybe something like DataLoadProvider<? super T, ? extends Z> may work here
30         providers.put(new MultiClassKey(dataClass, resourceClass), provider);
31     }
32 
33     /**
34      * Returns the currently registered {@link com.bumptech.glide.provider.DataLoadProvider} for the given classes.
35      *
36      * @param dataClass The class of the data that the provider provides encoders and decoders for.
37      * @param resourceClass The class of the resource that the provider provides encoders and decoders for.
38      * @param <T> The type of the data that the provider provides encoders and decoders for.
39      * @param <Z> The type of the resource that the provider provides encoders and decoders for.
40      */
41     @SuppressWarnings("unchecked")
get(Class<T> dataClass, Class<Z> resourceClass)42     public <T, Z> DataLoadProvider<T, Z> get(Class<T> dataClass, Class<Z> resourceClass) {
43         DataLoadProvider<?, ?> result;
44         synchronized (GET_KEY) {
45             GET_KEY.set(dataClass, resourceClass);
46             result = providers.get(GET_KEY);
47         }
48         if (result == null) {
49             result = EmptyDataLoadProvider.get();
50         }
51         return (DataLoadProvider<T, Z>) result;
52     }
53 }
54