KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > context > HeapContext


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2006 - Javolution (http://javolution.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package javolution.context;
10
11 import j2me.lang.ThreadLocal;
12 import j2me.lang.UnsupportedOperationException;
13 import javolution.JavolutionError;
14
15 /**
16  * <p> This class represents a heap context. In this context
17  * {@link ObjectFactory object factories} return new objects from the
18  * heap unless explicitely {@link ObjectFactory#recycle(Object) recycled}
19  * instances are available. For example:[code]
20  * // The default context is a HeapContext.
21  * char[] buffer = CHAR_4096_FACTORY.object(); // Possibly recycled.
22  * while (reader.read(buffer) > 0) { ... }
23  * CHAR_4096_FACTORY.recycle(buffer); // Explicitely recycles the buffer.
24  * ...
25  * static ObjectFactory<char[]> CHAR_4096_FACTORY = new ObjectFactory<char[]>() {
26  * protected char[] create() {
27  * return new char[4096];
28  * }
29  * };
30  * [/code]</p>
31  *
32  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
33  * @version 4.2, December 14, 2006
34  */

35 public class HeapContext extends Context {
36
37     /**
38      * Holds the context factory.
39      */

40     private static Factory FACTORY = new Factory() {
41         protected Object JavaDoc create() {
42             return new HeapContext();
43         }
44     };
45
46     /**
47      * Holds the thread-local pools (for explicit object recycling).
48      */

49     private final ThreadLocal JavaDoc _localPools = new ThreadLocal JavaDoc() {
50         protected Object JavaDoc initialValue() {
51             LocalPools pools = new LocalPools(false);
52             pools._owner = Thread.currentThread();
53             return pools;
54         }
55     };
56
57     /**
58      * Default constructor.
59      */

60     public HeapContext() {
61     }
62
63     /**
64      * Returns the current heap context or <code>null<code> if the current
65      * thread executes within a {@link PoolContext}.
66      *
67      * @return the current heap context.
68      */

69     public static/*HeapContext*/Context 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     /**
80      * Enters a {@link HeapContext} possibly recycled.
81      */

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     /**
90      * Exits and recycles the current {@link HeapContext}.
91      *
92      * @throws UnsupportedOperationException if the current context
93      * has not been entered using HeapContext.enter()
94      */

95     public static void exit() {
96         HeapContext ctx = (HeapContext) Context.current();
97         if (!ctx._isInternal) throw new UnsupportedOperationException JavaDoc
98            ("The context to exit must be specified");
99         ctx._isInternal = false;
100         Context.exitNoCheck(ctx);
101         FACTORY.recycle(ctx);
102     }
103
104     /**
105      * Clears the pools associated to this context (for the current thread
106      * only).
107      */

108     public void clear() {
109         ((LocalPools) _localPools.get()).clear();
110     }
111
112     // Implements Context abstract method.
113
protected void enterAction() {
114         Context outer = this.getOuter();
115         outer.getLocalPools().deactivatePools();
116         this.getLocalPools().activatePools();
117     }
118
119     // Implements Context abstract method.
120
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