@author Sam Harwell
| 43 | |
| 44 | |
| 45 | public class TypeRegistry<V> implements Map<Class<?>, V> { |
| 46 | private final Map<Class<?>, V> backingStore = new HashMap<Class<?>, V>(); |
| 47 | private final Map<Class<?>, Class<?>> cache = new HashMap<Class<?>, Class<?>>(); |
| 48 | public int size() { |
| 49 | return backingStore.size(); |
| 50 | } |
| 51 | |
| 52 | public boolean isEmpty() { |
| 53 | return backingStore.isEmpty(); |
| 54 | } |
| 55 | |
| 56 | public boolean containsKey(Object key) { |
| 57 | if ( cache.containsKey(key) ) { |
| 58 | return true; |
| 59 | } |
| 60 | if ( !(key instanceof Class) ) { |
| 61 | return false; |
| 62 | } |
| 63 | return get(key)!=null; |
| 64 | } |
| 65 | |
| 66 | @SuppressWarnings("unchecked") |
| 67 | public boolean containsValue(Object value) { |
| 68 | return values().contains(value); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * {@inheritDoc} |
| 73 | * |
| 74 | * @throws AmbiguousMatchException if the registry contains more than value |
| 75 | * mapped to a maximally-specific type from which {@code key} is derived. |
| 76 | */ |
| 77 | |
| 78 | public V get(Object key) { |
| 79 | V value = backingStore.get(key); |
| 80 | if ( value!=null ) { |
| 81 | return value; |
| 82 | } |
| 83 | Class<?> redirect = cache.get(key); |
| 84 | if ( redirect!=null ) { |
| 85 | if ( redirect==Void.TYPE ) { |
| 86 | return null; |
| 87 | } |
| 88 | else { |
| 89 | return backingStore.get(redirect); |
| 90 | } |
| 91 | } |
| 92 | if ( !(key instanceof Class) ) { |
| 93 | return null; |
| 94 | } |
| 95 | Class<?> keyClass = (Class<?>)key; |
| 96 | List<Class<?>> candidates = new ArrayList<Class<?>>(); |
| 97 | for (Class<?> clazz : backingStore.keySet()) { |
| 98 | if ( clazz.isAssignableFrom(keyClass) ) { |
| 99 | candidates.add(clazz); |
| 100 | } |
| 101 | } |
| 102 | if ( candidates.isEmpty() ) { |
nothing calls this directly
no outgoing calls
no test coverage detected