{@inheritDoc} @throws AmbiguousMatchException if the registry contains more than value mapped to a maximally-specific type from which key is derived.
(Object key)
| 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() ) { |
| 103 | cache.put(keyClass, Void.TYPE); |
| 104 | return null; |
| 105 | } |
| 106 | else if ( candidates.size()==1 ) { |
| 107 | cache.put(keyClass, candidates.get(0)); |
| 108 | return backingStore.get(candidates.get(0)); |
| 109 | } |
| 110 | else { |
| 111 | for (int i = 0; i<candidates.size()-1; i++) { |
| 112 | if ( candidates.get(i)==null ) { |
| 113 | continue; |
| 114 | } |
| 115 | for (int j = i+1; j<candidates.size(); j++) { |
| 116 | if ( candidates.get(i).isAssignableFrom(candidates.get(j)) ) { |
| 117 | candidates.set(i, null); |
| 118 | break; |
| 119 | } |
| 120 | else if ( candidates.get(j).isAssignableFrom(candidates.get(i)) ) { |
| 121 | candidates.set(j, null); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | int j = 0; |
| 127 | for (int i = 0; i<candidates.size(); i++) { |
| 128 | Class<?> current = candidates.get(i); |
| 129 | if ( current==null ) { |
| 130 | continue; |
| 131 | } |
| 132 | if ( i!= j ) { |
| 133 | candidates.set(j, current); |
| 134 | } |
| 135 | j++; |