01: /*
02: * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
03: * Copyright (C) 2006 - JScience (http://jscience.org/)
04: * All rights reserved.
05: *
06: * Permission to use, copy, modify, and distribute this software is
07: * freely granted, provided that this notice is preserved.
08: */
09: package org.jscience.physics.model;
10:
11: import javax.measure.converter.UnitConverter;
12: import javax.measure.unit.BaseUnit;
13: import javax.measure.unit.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.unit.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 = new LocalContext.Reference<PhysicalModel>();
41:
42: /**
43: * Holds the dimensional model.
44: */
45: private static final Dimension.Model DIMENSIONAL_MODEL = new Dimension.Model() {
46:
47: public Dimension getDimension(BaseUnit<?> unit) {
48: return PhysicalModel.Current.get().getDimension(unit);
49: }
50:
51: public UnitConverter getTransform(BaseUnit<?> unit) {
52: return PhysicalModel.Current.get().getTransform(unit);
53: }
54: };
55:
56: /**
57: * Default constructor (allows for derivation).
58: */
59: protected PhysicalModel() {
60: }
61:
62: /**
63: * Returns the current physical model (default: instance of
64: * {@link StandardModel}).
65: *
66: * @return the context-local physical model.
67: */
68: public static final PhysicalModel current() {
69: PhysicalModel physicalModel = PhysicalModel.Current.get();
70: return (physicalModel == null) ? StandardModel.INSTANCE
71: : 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: }
|