KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > measure > quantities > Scalar


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 javax.measure.quantities;
10
11 import java.io.Serializable JavaDoc;
12
13 import javax.measure.converters.UnitConverter;
14 import javax.measure.units.Unit;
15
16 /**
17  * This class represents a simple quantity implementation, that is completely
18  * specified by its magnitude and has no direction.
19  *
20  * <p>Instances of this class are immutable</p>
21  *
22  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
23  * @version 3.1, April 22, 2006
24  */

25 public class Scalar<Q extends Quantity> extends Number JavaDoc implements Quantity<Q>,
26         Comparable JavaDoc<Quantity<Q>>, Serializable JavaDoc {
27
28     /**
29      * Holds the amount value.
30      */

31     private double _amount;
32
33     /**
34      * Holds the unit.
35      */

36     private Unit<Q> _unit;
37
38     /**
39      * Creates a scalar quantity of specified amount stated in
40      * the specified unit.
41      *
42      * @param amount the quantity amount stated in the specified unit.
43      * @param unit the unit used to specified the amount
44      */

45     public Scalar(double amount, Unit<Q> unit) {
46         _amount = amount;
47         _unit = unit;
48     }
49
50     /**
51      * Returns the amount stated in this scalar quantity {@link #getUnit unit}.
52      *
53      * @return the amount value.
54      */

55     public final double getAmount() {
56         return _amount;
57     }
58
59     /**
60      * Returns the unit used for this scalar quantity.
61      *
62      * @return the amount unit.
63      */

64     public final Unit<Q> getUnit() {
65         return _unit;
66     }
67
68     // Implements Quantity<Q>
69
public final double doubleValue(Unit<Q> unit) {
70         if (unit == _unit)
71             return _amount;
72         UnitConverter cvtr = _unit.getConverterTo(unit);
73         return cvtr.convert(_amount);
74     }
75
76     // Implements Quantity<Q>
77
public final long longValue(Unit<Q> unit) throws ArithmeticException JavaDoc {
78         double doubleValue = doubleValue(unit);
79         if ((doubleValue >= Long.MIN_VALUE) && (doubleValue <= Long.MAX_VALUE))
80             return Math.round(doubleValue);
81         throw new ArithmeticException JavaDoc(doubleValue + " " + unit
82                 + " cannot be represented as long");
83     }
84
85     /**
86      * Compares the value of this quantity to the specified quantity value
87      * both stated using the same unit.
88      *
89      * @param that the quantity to compare with.
90      * @return negative integer, zero, or a positive integer as this quantity
91      * amount is less than, equal to, or greater than the specified
92      * quantity.
93      */

94     public int compareTo(Quantity<Q> that) {
95         double thatValue = that.doubleValue(_unit);
96         if (this._amount < thatValue) {
97             return -1;
98         } else if (this._amount > thatValue) {
99             return 1;
100         } else {
101             long l1 = java.lang.Double.doubleToLongBits(this._amount);
102             long l2 = java.lang.Double.doubleToLongBits(thatValue);
103             return (l1 == l2 ? 0 : (l1 < l2 ? -1 : 1));
104         }
105     }
106     
107     /**
108      * Returns the amount as an <code>int</code>.
109      * This may involve rounding or truncation.
110      *
111      * @return the numeric value represented by this object after conversion
112      * to type <code>int</code>.
113      */

114     public final int intValue() {
115         return (int) getAmount();
116     }
117
118     /**
119      * Returns the amount as a <code>long</code>.
120      * This may involve rounding or truncation.
121      *
122      * @return the numeric value represented by this object after conversion
123      * to type <code>long</code>.
124      */

125     public final long longValue() {
126         return (long) getAmount();
127     }
128
129     /**
130      * Returns the amount as a <code>float</code>.
131      * This may involve rounding.
132      *
133      * @return the numeric value represented by this object after conversion
134      * to type <code>float</code>.
135      */

136     public final float floatValue() {
137         return (float) getAmount();
138     }
139
140     /**
141      * Returns the amount as a <code>double</code>.
142      *
143      * @return the numeric value represented by this object after conversion
144      * to type <code>double</code>.
145      */

146     public final double doubleValue() {
147         return getAmount();
148     }
149
150     /**
151      * Compares this scalar against the specified object.
152      *
153      * @param that the object to compare with.
154      * @return <code>true</code> if the objects are the same;
155      * <code>false</code> otherwise.
156      */

157     public boolean equals(Object JavaDoc that) {
158         return (that instanceof Scalar)
159                 && (this._amount == ((Scalar) that)._amount);
160     }
161
162     /**
163      * Returns the hash code for this scalar.
164      *
165      * @return the hash code value.
166      */

167     public int hashCode() {
168         int h = Float.floatToIntBits((float) _amount);
169         h += ~(h << 9);
170         h ^= (h >>> 14);
171         h += (h << 4);
172         return h ^ (h >>> 10);
173     }
174
175     private static final long serialVersionUID = 1L;
176 }
Popular Tags