Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.85 KB | None | 0 0
  1. public class KeyValueLazySingleton {
  2.     private final static String KEY = "FUCKING_KEY_FOR_INSTANCE";
  3.  
  4.     private static KeyValueLazySingleton INSTANCE;
  5.  
  6.     private final Map<Object, Object> kv = new HashMap<>();
  7.  
  8.     private KeyValueLazySingleton() {
  9.         init();
  10.     }
  11.  
  12.     public static KeyValueLazySingleton getInstance() {
  13.         if (kv.containsKey(KEY))
  14.             return kv.get(KEY);
  15.  
  16.         synchronized (KeyValueLazySingleton.class) {
  17.             if (kv.containsKey(KEY))
  18.                 return kv.get(KEY);
  19.  
  20.             INSTANCE = new KeyValueLazySingleton();
  21.             return INSTANCE;
  22.         }
  23.     }
  24.  
  25.     private void init() {
  26.         kv.put("one", "a");
  27.     kv.put(BigInteger.valueOf(100), new Object());
  28.     kv.put(KEY, this);
  29.     }
  30.  
  31.     public Object getValueByKey(Object key) {
  32.         return kv.get(key);
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement