1 9 package javolution.context; 10 11 import j2me.lang.Comparable; 12 import javolution.util.FastMap; 13 import javolution.xml.XMLFormat; 14 import javolution.xml.stream.XMLStreamException; 15 16 40 public class PersistentContext extends Context { 41 42 45 private static final Class CLASS = new PersistentContext().getClass();; 46 47 50 private static PersistentContext _PersistentContext = new PersistentContext(); 51 52 55 private final FastMap _idToValue = new FastMap(); 56 57 61 protected static final XMLFormatXML = new XMLFormat( 62 CLASS) { 63 public void read(InputElement xml, Object obj) 64 throws XMLStreamException { 65 final PersistentContext ctx = (PersistentContext) obj; 66 ctx._idToValue.putAll((FastMap) xml.get("References")); 67 } 68 69 public void write(Object obj, OutputElement xml) 70 throws XMLStreamException { 71 final PersistentContext ctx = (PersistentContext) obj; 72 xml.add(ctx._idToValue, "References"); 73 } 74 }; 75 76 79 public PersistentContext() { 80 } 81 82 87 public static void setCurrent(PersistentContext ctx) { 88 _PersistentContext = ctx; 89 synchronized (Reference.INSTANCES) { 90 for (FastMap.Entry e = Reference.INSTANCES.head(), end = Reference.INSTANCES.tail(); 91 (e = (FastMap.Entry) e.getNext())!= end;) { 92 Reference reference = (Reference) e.getValue(); 93 if (ctx._idToValue.containsKey(reference._id)) { 94 reference.set(ctx._idToValue.get(reference._id)); 95 } 96 } 97 } 98 } 99 100 105 public staticContext current() { 106 return _PersistentContext; 107 } 108 109 113 protected void enterAction() { 114 throw new j2me.lang.UnsupportedOperationException( 115 "Cannot enter persistent context (already in)"); 116 } 117 118 122 protected void exitAction() { 123 throw new j2me.lang.UnsupportedOperationException( 124 "Cannot exit persistent context (always in)"); 125 } 126 127 170 public static class Reference implements 171 javolution.lang.Reference { 172 173 176 private final static FastMap INSTANCES = new FastMap(); 177 178 181 private final String _id; 182 183 186 private Object _value; 187 188 196 public Reference(String id, Object defaultValue) { 197 _id = id; 198 _value = defaultValue; 199 synchronized (INSTANCES) { 200 if (INSTANCES.containsKey(id)) 201 throw new IllegalArgumentException ("Identifier " + id 202 + " already in use"); 203 INSTANCES.put(id, this); 204 } 205 if (_PersistentContext._idToValue.containsKey(id)) { 206 set((Object ) _PersistentContext._idToValue.get(id)); 207 } 208 } 209 210 public Object get() { 212 return _value; 213 } 214 215 public void set(Object value) { 217 _value = value; 218 notifyChange(); 219 } 220 221 229 public void setMinimum(Object value) { 230 synchronized (this) { 231 if (value instanceof Comparable ) { 232 Object prevValue = get(); 233 if (((Comparable ) value).compareTo(prevValue) > 0) { 234 set(value); 235 } 236 } else if (value instanceof Integer ) { 237 Object prevValue = get(); 238 if (((Integer ) value).intValue() > ((Integer ) prevValue) 239 .intValue()) { 240 set(value); 241 } 242 } else { 243 throw new IllegalArgumentException (); 244 } 245 } 246 } 247 248 256 public void setMaximum(Object value) { 257 synchronized (this) { 258 if (value instanceof Comparable ) { 259 Object prevValue = get(); 260 if (((Comparable ) value).compareTo(prevValue) < 0) { 261 set(value); 262 } 263 } else if (value instanceof Integer ) { 264 Object prevValue = get(); 265 if (((Integer ) value).intValue() < ((Integer ) prevValue) 266 .intValue()) { 267 set(value); 268 } 269 } else { 270 throw new IllegalArgumentException (); 271 } 272 } 273 } 274 275 281 public String toString() { 282 return String.valueOf(get()); 283 } 284 285 290 protected void notifyChange() { 291 } 292 } 293 } | Popular Tags |