Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class KeyValueLazySingleton {
- private final static String KEY = "FUCKING_KEY_FOR_INSTANCE";
- private static KeyValueLazySingleton INSTANCE;
- private final Map<Object, Object> kv = new HashMap<>();
- private KeyValueLazySingleton() {
- init();
- }
- public static KeyValueLazySingleton getInstance() {
- if (kv.containsKey(KEY))
- return kv.get(KEY);
- synchronized (KeyValueLazySingleton.class) {
- if (kv.containsKey(KEY))
- return kv.get(KEY);
- INSTANCE = new KeyValueLazySingleton();
- return INSTANCE;
- }
- }
- private void init() {
- kv.put("one", "a");
- kv.put(BigInteger.valueOf(100), new Object());
- kv.put(KEY, this);
- }
- public Object getValueByKey(Object key) {
- return kv.get(key);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement