180 abstract static class RecyclableSingleton<T> {
181 final T get() {
182 return AppContext.getSoftReferenceValue(this, () -> getInstance());
183 }
184
185 void reset() {
186 AppContext.getAppContext().remove(this);
187 }
188
189 abstract T getInstance();
190 }
191
192 static class RecyclableSingletonFromDefaultConstructor<T> extends RecyclableSingleton<T> {
193 private final Class<T> clazz;
194
195 RecyclableSingletonFromDefaultConstructor(final Class<T> clazz) {
196 this.clazz = clazz;
197 }
198
199 @Override
200 T getInstance() {
201 try {
202 ReflectUtil.checkPackageAccess(clazz);
203 return clazz.newInstance();
204 } catch (InstantiationException | IllegalAccessException ignored) {
205 }
206 return null;
207 }
208 }
209
210 abstract static class LazyKeyedSingleton<K, V> {
211 private Map<K, V> refs;
212
213 V get(final K key) {
214 if (refs == null) refs = new HashMap<>();
215
216 final V cachedValue = refs.get(key);
217 if (cachedValue != null) return cachedValue;
218
219 final V value = getInstance(key);
|
180 abstract static class RecyclableSingleton<T> {
181 final T get() {
182 return AppContext.getSoftReferenceValue(this, () -> getInstance());
183 }
184
185 void reset() {
186 AppContext.getAppContext().remove(this);
187 }
188
189 abstract T getInstance();
190 }
191
192 static class RecyclableSingletonFromDefaultConstructor<T> extends RecyclableSingleton<T> {
193 private final Class<T> clazz;
194
195 RecyclableSingletonFromDefaultConstructor(final Class<T> clazz) {
196 this.clazz = clazz;
197 }
198
199 @Override
200 @SuppressWarnings("deprecation") // Class.newInstance
201 T getInstance() {
202 try {
203 ReflectUtil.checkPackageAccess(clazz);
204 return clazz.newInstance();
205 } catch (InstantiationException | IllegalAccessException ignored) {
206 }
207 return null;
208 }
209 }
210
211 abstract static class LazyKeyedSingleton<K, V> {
212 private Map<K, V> refs;
213
214 V get(final K key) {
215 if (refs == null) refs = new HashMap<>();
216
217 final V cachedValue = refs.get(key);
218 if (cachedValue != null) return cachedValue;
219
220 final V value = getInstance(key);
|