1 9 package javolution.lang; 10 11 import j2me.io.Serializable; 12 import j2me.util.Iterator; 13 import j2me.util.Map; 14 import javolution.util.FastMap; 15 16 71 public final class PersistentReference implements Reference, Serializable { 72 73 76 private static final FastMap COLLECTION = new FastMap(); 77 78 81 private static final FastMap VALUES = new FastMap(); 82 83 86 private static final Object LOCK = new Object(); 87 88 91 private final String _id; 92 93 96 private volatile Object_value; 97 98 106 public PersistentReference(String id, ObjectdefaultValue) { 107 synchronized (LOCK) { 108 if (COLLECTION.containsKey(id)) 109 throw new IllegalArgumentException("id: " + id + " in use"); 110 _id = id; 111 if (VALUES.containsKey(id)) { _value = (Object) VALUES.get(id); 113 ; 114 } else { 115 _value = defaultValue; 116 } 117 COLLECTION.put(id, this); 118 VALUES.put(id, _value); 119 } 120 } 121 122 127 public String id() { 128 return _id; 129 } 130 131 public Objectget() { 133 return _value; 134 } 135 136 public void set(Objectvalue) { 138 synchronized (LOCK) { 139 _value = value; 140 VALUES.put(_id, _value); 141 } 142 } 143 144 150 public static Map values() { 151 return VALUES.unmodifiable(); } 153 154 162 public static void put(String id, Object value) { 163 synchronized (LOCK) { 164 if (COLLECTION.containsKey(id)) { 165 PersistentReference ref = (PersistentReference) COLLECTION 166 .get(id); 167 ref._value = value; 168 } 169 VALUES.put(id, value); 170 } 171 } 172 173 179 public static void putAll(Map values) { 180 for (Iterator i = values.entrySet().iterator(); i.hasNext();) { 181 Map.Entry e = (Map.Entry) i.next(); 182 put((String) e.getKey(), e.getValue()); 183 } 184 } 185 } | Popular Tags |