1 9 package javolution.context; 10 11 import j2me.lang.ThreadLocal; 12 import j2me.lang.UnsupportedOperationException; 13 import javolution.JavolutionError; 14 15 35 public class HeapContext extends Context { 36 37 40 private static Factory FACTORY = new Factory() { 41 protected Object create() { 42 return new HeapContext(); 43 } 44 }; 45 46 49 private final ThreadLocal _localPools = new ThreadLocal () { 50 protected Object initialValue() { 51 LocalPools pools = new LocalPools(false); 52 pools._owner = Thread.currentThread(); 53 return pools; 54 } 55 }; 56 57 60 public HeapContext() { 61 } 62 63 69 public staticContext current() { 70 for (Context ctx = Context.current(); ctx != null; ctx = ctx.getOuter()) { 71 if (ctx instanceof HeapContext) 72 return (HeapContext) ctx; 73 if (ctx instanceof PoolContext) 74 return null; 75 } 76 throw new JavolutionError("No heap context or pool context"); 77 } 78 79 82 public static void enter() { 83 HeapContext ctx = (HeapContext) FACTORY.object(); 84 ctx._isInternal = true; 85 Context.enter(ctx); 86 } 87 private transient boolean _isInternal; 88 89 95 public static void exit() { 96 HeapContext ctx = (HeapContext) Context.current(); 97 if (!ctx._isInternal) throw new UnsupportedOperationException 98 ("The context to exit must be specified"); 99 ctx._isInternal = false; 100 Context.exitNoCheck(ctx); 101 FACTORY.recycle(ctx); 102 } 103 104 108 public void clear() { 109 ((LocalPools) _localPools.get()).clear(); 110 } 111 112 protected void enterAction() { 114 Context outer = this.getOuter(); 115 outer.getLocalPools().deactivatePools(); 116 this.getLocalPools().activatePools(); 117 } 118 119 protected void exitAction() { 121 this.getLocalPools().deactivatePools(); 122 Context outer = this.getOuter(); 123 outer.getLocalPools().activatePools(); 124 } 125 126 final LocalPools getLocalPools() { 127 return (LocalPools) _localPools.get(); 128 } 129 130 } | Popular Tags |