KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > monitorenter > util > units > UnitFactory


1 /*
2  * Unit.java, singleton for caching and accessing UnitSystems.
3  * Copyright (C) Achim Westermann, created on 12.05.2005, 20:11:17
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * If you modify or optimize the code in a useful way please let me know.
20  * Achim.Westermann@gmx.de
21  *
22  */

23
24 package info.monitorenter.util.units;
25
26 import java.util.LinkedList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 /**
33  * Singleton that caches instances of whole unit- systems and provides you with
34  * the matching unit for a maximum value.
35  * <p>
36  *
37  * @author <a HREF='mailto:Achim.Westermann@gmx.de'>Achim Westermann </a>
38  *
39  * @version $Revision: 1.1 $
40  *
41  * @see info.monitorenter.util.units.IUnitSystem
42  *
43  */

44 public final class UnitFactory extends Object JavaDoc {
45
46   /** Singleton instance. */
47   private static UnitFactory instance;
48
49   /**
50    * Marker unit that represents a "non-unit" that does not modify anything in
51    * {@link AUnit#getValue(double)}.
52    */

53   public static final AUnit UNCHANGED = new UnitUnchanged();
54
55   /** Cache for {@link UnitSystem} instances. */
56   private static final Map JavaDoc UNITSYSTEMS = new HashMap JavaDoc();
57
58   /**
59    * Singleton retrieval method.
60    * <p>
61    *
62    * @return the unique instance within the current VM.
63    */

64   public static UnitFactory getInstance() {
65     if (UnitFactory.instance == null) {
66       UnitFactory.instance = new UnitFactory();
67     }
68     return UnitFactory.instance;
69   }
70
71   /**
72    * Singleton constructor.
73    * <p>
74    *
75    */

76   private UnitFactory() {
77     // nop
78
}
79
80   /**
81    * Returns the unit for the given argument absolute max.
82    * <p>
83    *
84    * The unit is chosen in a way that
85    *
86    *
87    * @param absoluteMax
88    * the absolute maximum value that has to be put into relation to the
89    * unit to retrieve.
90    * @param units
91    * the UnitSystem to use.
92    * @return the unit for the given argument absolute max.
93    */

94   public AUnit getUnit(final double absoluteMax, final IUnitSystem units) {
95     List JavaDoc choice;
96     // lazy initialisation
97
choice = (List JavaDoc) UnitFactory.UNITSYSTEMS.get(units.getClass().getName());
98     if (choice == null) {
99       choice = this.initUnitSystem(units);
100     }
101     // Now to find the right unit.
102
Iterator JavaDoc it = choice.iterator();
103     AUnit ret = null, old = null;
104     if (it.hasNext()) {
105       old = (AUnit) it.next();
106       while (it.hasNext()) {
107         ret = (AUnit) it.next();
108         if (absoluteMax < (ret.getFactor())) {
109           return old;
110         }
111         old = ret;
112       }
113       // highest unit available
114
return old;
115     }
116     // no clue
117
return UnitFactory.UNCHANGED;
118   }
119
120   /**
121    * Returns a list of all different {@link AUnit} instances available in the
122    * given unit system.
123    * <p>
124    *
125    * @param unitsystem
126    * the unit system of interest.
127    *
128    * @return a list of all different {@link AUnit} instances available in the
129    * given unit system.
130    */

131   public List JavaDoc getUnits(final IUnitSystem unitsystem) {
132     List JavaDoc choice = (List JavaDoc) UnitFactory.UNITSYSTEMS.get(unitsystem.getClass().getName());
133     // lazy initialisation
134
if (choice == null) {
135       choice = this.initUnitSystem(unitsystem);
136     }
137     return choice;
138
139   }
140
141   /**
142    * Internally loads and caches the unit system.
143    * <p>
144    *
145    * @param units
146    * the unit system to initialize.
147    *
148    * @return the list of {@link Unit} instances of the given unit system.
149    */

150   private List JavaDoc initUnitSystem(final IUnitSystem units) {
151     List JavaDoc choice = new LinkedList JavaDoc();
152     Class JavaDoc[] clazzs = units.getUnits();
153     AUnit unit = null, previous = null;
154     for (int i = 0; i < clazzs.length; i++) {
155       if (!AUnit.class.isAssignableFrom(clazzs[i])) {
156         System.err.println("UnitFactory: wrong class " + clazzs[i].getName() + " delivered by "
157             + units.getClass().getName());
158         continue;
159       } else {
160         try {
161           unit = (AUnit) clazzs[i].newInstance();
162           choice.add(unit);
163           if (previous == null) {
164             previous = unit;
165           } else {
166             previous.m_nextHigherUnit = unit;
167           }
168           unit.m_nexLowerUnit = previous;
169           previous = unit;
170         } catch (InstantiationException JavaDoc e) {
171           System.err.println("UnitFactory encountered problems by instantiation of "
172               + clazzs[i].getName());
173         } catch (IllegalAccessException JavaDoc f) {
174           System.err.println("UnitFactory has no access to " + clazzs[i].getName());
175         }
176       }
177     }
178     unit.m_nextHigherUnit = unit;
179     // hardcoded minsearch sort:
180
double tmpfactori, tmpfactorj;
181     int min;
182     int stop = choice.size();
183     for (int i = 0; i < stop - 1; i++) {
184       min = i;
185       tmpfactori = ((AUnit) choice.get(i)).getFactor();
186       for (int j = i + 1; j < stop; j++) {
187         tmpfactorj = ((AUnit) choice.get(j)).getFactor();
188         if (tmpfactorj < tmpfactori) {
189           tmpfactori = tmpfactorj;
190           min = j;
191         }
192       }
193       choice.add(i, choice.remove(min));
194     }
195     UnitFactory.UNITSYSTEMS.put(units.getClass().getName(), choice);
196     return choice;
197   }
198 }
199
Popular Tags