KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Unit.java, base class for units in jchart2d.
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 /**
27  * A unit.
28  * <p>
29  *
30  * @author <a HREF='mailto:Achim.Westermann@gmx.de'>Achim Westermann </a>
31  *
32  * @version $Revision: 1.1 $
33  *
34  * @see info.monitorenter.util.units.UnitFactory
35  *
36  * @see info.monitorenter.util.units.IUnitSystem
37  *
38  * @see info.monitorenter.util.units.UnitSystemSI
39  */

40 public abstract class AUnit extends Object JavaDoc {
41
42   /** Decimals for rounding. */
43   protected int m_decimals = 2;
44
45   /**
46    * The factor a result of {@link #getValue(double)} had to be multiplied with
47    * if the real (unitless) value has to be calculated.
48    */

49   protected double m_factor;
50
51   /**
52    * The next smaller unit to this one within this unit's {@link IUnitSystem}.
53    * <p>
54    */

55   protected AUnit m_nexLowerUnit;
56
57   /**
58    * The next greater unit to this one within this unit's {@link IUnitSystem}.
59    * <p>
60    */

61   protected AUnit m_nextHigherUnit;
62
63   /**
64    * The short unit name of this unit a result of {@link #getValue(double)} has
65    * to be related with to know the this result is displayed in a unit.
66    * <p>
67    */

68   protected String JavaDoc m_unitName;
69
70   /**
71    * Protected constructor to ensure package access only.
72    * <p>
73    *
74    * Use {@link UnitFactory#getInstance()} and
75    * {@link UnitFactory#getUnit(double, IUnitSystem)} to obtain a proper unit.
76    * <p>
77    *
78    */

79   protected AUnit() {
80     // nop
81
}
82
83   /**
84    * Returns the number of decimals that should be be taken into account if the
85    * method {@link #getValue(double)} is invoked (rounding).
86    * <p>
87    *
88    * @return the number of decimals that should be be taken into account if the
89    * method {@link #getValue(double)} is invoked (rounding).
90    */

91   public int getDecimals() {
92     return this.m_decimals;
93   }
94
95   /**
96    * Returns the factor a result of {@link #getValue(double)} had to be
97    * multiplied with if the real (unitless) value has to be calculated.
98    * <p>
99    *
100    * For performance reason (fast access) factor is public. This is against
101    * "safety by desing" so do never set this value.
102    * <p>
103    *
104    * @return the factor a result of {@link #getValue(double)} had to be
105    * multiplied with if the real (unitless) value has to be calculated.
106    */

107   public double getFactor() {
108     return this.m_factor;
109   }
110
111   /**
112    * Returns the the value divided by this unit's factor, rounded to this unit's
113    * configured decimals and suffixed by the unit name.
114    * <p>
115    *
116    * @param value
117    * the value for the label.
118    *
119    * @return the the value divided by this unit's factor, rounded by this unit's
120    * configured decimals and suffixed by the unit name.
121    *
122    * @see #getUnitName()
123    * @see #getDecimals()
124    *
125    */

126   public String JavaDoc getLabel(final double value) {
127     return new StringBuffer JavaDoc().append(round(value / this.m_factor)).append(" ").append(
128         this.m_unitName).toString();
129   }
130
131   /**
132    * Returns the next smaller unit to this one within this unit's
133    * {@link IUnitSystem}.
134    * <p>
135    *
136    * If this is already the smallest unit, this will be returned so add
137    * <code>unit == unit.getLowerUnit()</code> as the termination criteria in
138    * loops to search for the smallest unit (to avoid endless loops).
139    * <p>
140    *
141    * @return the next lower unit to this one within this unit's
142    * {@link IUnitSystem}.
143    */

144   public AUnit getNexLowerUnit() {
145     return this.m_nexLowerUnit;
146   }
147
148   /**
149    * Returns the next greater unit to this one within this unit's
150    * {@link IUnitSystem}.
151    * <p>
152    *
153    * If this is already the greates unit, this will be returned so add
154    * <code>unit == unit.getNextHigherUnit()</code> as the termination criteria
155    * in loops to search for the greatest unit (to avoid endless loops).
156    * <p>
157    *
158    * @return the next greater unit to this one within this unit's
159    * {@link IUnitSystem}.
160    */

161   public AUnit getNextHigherUnit() {
162     return this.m_nextHigherUnit;
163   }
164
165   /**
166    * Retunrns the short unit name of this unit a result of
167    * {@link #getValue(double)} has to be related with to know the this result is
168    * displayed in a unit.
169    * <p>
170    *
171    * @return the short unit name of this unit a result of
172    * {@link #getValue(double)} has to be related with to know the this
173    * result is displayed in a unit.
174    * <p>
175    */

176   public String JavaDoc getUnitName() {
177     return this.m_unitName;
178   }
179
180   /**
181    * Transforms the given absolute value into the represented unit value by
182    * dividing by the specific factor.
183    * <p>
184    *
185    * The result is rounded using the actual decimal setting.
186    * <p>
187    *
188    * @param value
189    * the value to represent in this unit.
190    *
191    * @return The value to display in this unit rounded using the internal
192    * decimals.
193    */

194   public double getValue(final double value) {
195     return round(value / this.m_factor);
196   }
197
198   /**
199    * Internal rounding routine for {@link #getValue(double)}.
200    * <p>
201    *
202    * @param value
203    * the value to round.
204    * @return the given value rounded to the amount of decimals configured.
205    */

206   private final double round(final double value) {
207     double tmp = Math.pow(10, this.m_decimals);
208     return (Math.floor(value * tmp + 0.5d)) / tmp;
209   }
210
211   /**
212    * Define how many decimals should be taken into account if the method
213    * {@link #getValue(double)} is invoked (rounding).
214    * <p>
215    *
216    * @param aftercomma
217    * the number of decimals that should be taken into account if the
218    * method {@link #getValue(double)} is invoked (rounding)
219    */

220   public void setDecimals(final int aftercomma) {
221     if (aftercomma >= 0) {
222       this.m_decimals = aftercomma;
223     }
224   }
225
226   /**
227    * Returns the same as {@link #getUnitName()}, prefer calling this directly
228    * if desired.
229    * <p>
230    *
231    * @return the same as {@link #getUnitName()}, prefer calling this directly
232    * if desired.
233    *
234    */

235   public String JavaDoc toString() {
236     return this.getUnitName();
237   }
238 }
239
Popular Tags