1 package javolution.realtime; 2 3 import j2me.io.Serializable; 4 import javolution.lang.Reference; 5 import javolution.util.FastMap; 6 7 48 public class LocalReference implements Reference, Serializable { 49 50 53 private volatile Object _defaultValue; 54 55 58 public LocalReference() { 59 this(null); 60 } 61 62 67 public LocalReference(Object defaultValue) { 68 _defaultValue = defaultValue; 69 } 70 71 72 80 public Object get() { 81 for (Context ctx = Context.currentContext(); ctx != null; ctx = ctx 82 .getOuter()) { 83 if (ctx instanceof LocalContext) { 84 Object value = ((LocalContext)ctx)._references.get(this); 85 if (value != null) { 86 return (Object) value; 87 } 88 } 89 } 90 return _defaultValue; 92 } 93 94 100 public void set(Object value) { 101 for (Context ctx = Context.currentContext(); ctx != null; ctx = ctx 102 .getOuter()) { 103 if (ctx instanceof LocalContext) { 104 FastMap references = ((LocalContext)ctx)._references; 105 synchronized (references) { references.put(this, value); 107 } 108 return; 109 } 110 } 111 _defaultValue = value; 113 } 114 115 120 public Object getDefault() { 121 return _defaultValue; 122 } 123 124 129 public void setDefault(Object defaultValue) { 130 _defaultValue = defaultValue; 131 } 132 } | Popular Tags |