KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jscience > physics > models > PhysicalModel


1 /*
2  * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
3  * Copyright (C) 2006 - JScience (http://jscience.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 org.jscience.physics.models;
10
11 import javax.measure.converters.UnitConverter;
12 import javax.measure.units.BaseUnit;
13 import javax.measure.units.Dimension;
14
15 import javolution.context.LocalContext;
16
17 /**
18  * <p> This abstract class represents a physical model. Instances of this
19  * class determinate the current quantities dimensions.</p>
20  *
21  * <p> To select a model, one needs only to call the model <code>select</code>
22  * static method. For example:[code]
23  * public static void main(String[] args) {
24  * // Global (LocalContext should be used for thread-local settings).
25  * RelativisticModel.select();
26  * ...
27  * [/code]</p>
28  *
29  * <p> Selecting a predefined model automatically sets the dimension of
30  * the {@link javax.measure.units.BaseUnit base units}.</p>
31  *
32  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
33  * @version 3.1, April 22, 2006
34  */

35 public abstract class PhysicalModel implements Dimension.Model {
36
37     /**
38      * Holds the current physical model.
39      */

40     private static LocalContext.Reference<PhysicalModel> Current
41         = new LocalContext.Reference<PhysicalModel>();
42     
43     /**
44      * Holds the dimensional model.
45      */

46     private static final Dimension.Model DIMENSIONAL_MODEL
47         = new Dimension.Model() {
48
49             public Dimension getDimension(BaseUnit unit) {
50                 return PhysicalModel.Current.get().getDimension(unit);
51             }
52
53             public UnitConverter getTransform(BaseUnit unit) {
54                 return PhysicalModel.Current.get().getTransform(unit);
55             }};
56     
57     /**
58      * Default constructor (allows for derivation).
59      */

60     protected PhysicalModel() {
61     }
62
63     /**
64      * Returns the current physical model (default: instance of
65      * {@link StandardModel}).
66      *
67      * @return the context-local physical model.
68      */

69     public static final PhysicalModel current() {
70         PhysicalModel physicalModel = PhysicalModel.Current.get();
71         return (physicalModel == null) ? StandardModel.INSTANCE : physicalModel;
72     }
73
74     /**
75      * Sets the current model (this method is called when the a predefined
76      * model is selected).
77      *
78      * @param model the context-local physical model.
79      * @see #current
80      */

81     protected static final void setCurrent(PhysicalModel model) {
82         PhysicalModel.Current.set(model);
83         Dimension.setModel(DIMENSIONAL_MODEL);
84     }
85
86 }
Popular Tags