162 } 163 164 void reset() { 165 AppContext.getAppContext().remove(this); 166 } 167 168 abstract T getInstance(); 169 } 170 171 static class RecyclableSingletonFromDefaultConstructor<T> extends RecyclableSingleton<T> { 172 private final Class<T> clazz; 173 174 RecyclableSingletonFromDefaultConstructor(final Class<T> clazz) { 175 this.clazz = clazz; 176 } 177 178 @Override 179 T getInstance() { 180 try { 181 ReflectUtil.checkPackageAccess(clazz); 182 return clazz.newInstance(); 183 } catch (InstantiationException | IllegalAccessException ignored) { 184 } 185 return null; 186 } 187 } 188 189 abstract static class LazyKeyedSingleton<K, V> { 190 private Map<K, V> refs; 191 192 V get(final K key) { 193 if (refs == null) refs = new HashMap<>(); 194 195 final V cachedValue = refs.get(key); 196 if (cachedValue != null) return cachedValue; 197 198 final V value = getInstance(key); 199 refs.put(key, value); 200 return value; 201 } 202 203 protected abstract V getInstance(K key); | 162 } 163 164 void reset() { 165 AppContext.getAppContext().remove(this); 166 } 167 168 abstract T getInstance(); 169 } 170 171 static class RecyclableSingletonFromDefaultConstructor<T> extends RecyclableSingleton<T> { 172 private final Class<T> clazz; 173 174 RecyclableSingletonFromDefaultConstructor(final Class<T> clazz) { 175 this.clazz = clazz; 176 } 177 178 @Override 179 T getInstance() { 180 try { 181 ReflectUtil.checkPackageAccess(clazz); 182 return clazz.getDeclaredConstructor().newInstance(); 183 } catch (ReflectiveOperationException ignored) { 184 } 185 return null; 186 } 187 } 188 189 abstract static class LazyKeyedSingleton<K, V> { 190 private Map<K, V> refs; 191 192 V get(final K key) { 193 if (refs == null) refs = new HashMap<>(); 194 195 final V cachedValue = refs.get(key); 196 if (cachedValue != null) return cachedValue; 197 198 final V value = getInstance(key); 199 refs.put(key, value); 200 return value; 201 } 202 203 protected abstract V getInstance(K key); |