KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javolution > realtime > LocalContext


1 /*
2  * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
3  * Copyright (C) 2005 - 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.realtime;
10
11 import javolution.util.FastMap;
12
13 /**
14  * <p> This class represents a local context; it is used to define locally
15  * scoped setting through the use of {@link LocalReference} typically
16  * wrapped within a static method. For example:<pre>
17  * LargeInteger.setModulus(m); // Performs integer operations modulo m.
18  * Length.showAs(NonSI.INCH); // Shows length in inches.
19  * RelativisticModel.select(); // Uses relativistic physical model.
20  * QuantityFormat.getInstance(); // Returns local format for quantities.
21  * </pre></p>
22  *
23  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
24  * @version 3.3, May 10, 2004
25  */

26 public final class LocalContext extends Context {
27
28     /**
29      * Holds any reference associated to this context (reference to
30      * referent mapping).
31      */

32     final FastMap _references = new FastMap();
33
34     /**
35      * Default constructor.
36      */

37     LocalContext() {
38     }
39
40     /**
41      * Enters a {@link LocalContext}.
42      */

43     public static void enter() {
44         LocalContext ctx = (LocalContext) push(LOCAL_CONTEXT_CLASS);
45         if (ctx == null) {
46             ctx = new LocalContext();
47             push(ctx);
48         }
49     }
50     private static final Class LOCAL_CONTEXT_CLASS = new LocalContext().getClass();
51
52     /**
53      * Exits the current {@link LocalContext}.
54      *
55      * @throws ClassCastException if the current context is not a
56      * {@link LocalContext}.
57      */

58     public static void exit() {
59         LocalContext ctx = (LocalContext) pop();
60         ctx._references.clear();
61     }
62
63     // Implements abstract method.
64
protected void dispose() {
65         // No resource allocated.
66
}
67 }
Popular Tags