]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/util/Measure.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / util / Measure.java
1 /*\r
2 **********************************************************************\r
3 * Copyright (c) 2004-2009, International Business Machines\r
4 * Corporation and others.  All Rights Reserved.\r
5 **********************************************************************\r
6 * Author: Alan Liu\r
7 * Created: April 20, 2004\r
8 * Since: ICU 3.0\r
9 **********************************************************************\r
10 */\r
11 package com.ibm.icu.util;\r
12 \r
13 import java.lang.Number;\r
14 \r
15 /**\r
16  * An amount of a specified unit, consisting of a Number and a Unit.\r
17  * For example, a length measure consists of a Number and a length\r
18  * unit, such as feet or meters.  This is an abstract class.\r
19  * Subclasses specify a concrete Unit type.\r
20  *\r
21  * <p>Measure objects are parsed and formatted by subclasses of\r
22  * MeasureFormat.\r
23  *\r
24  * <p>Measure objects are immutable.\r
25  *\r
26  * @see java.lang.Number\r
27  * @see com.ibm.icu.util.MeasureUnit\r
28  * @see com.ibm.icu.text.MeasureFormat\r
29  * @author Alan Liu\r
30  * @stable ICU 3.0\r
31  */\r
32 public abstract class Measure {\r
33     \r
34     private Number number;\r
35 \r
36     private MeasureUnit unit;\r
37 \r
38     /**\r
39      * Constructs a new object given a number and a unit.\r
40      * @param number the number\r
41      * @param unit the unit\r
42      * @stable ICU 3.0\r
43      */\r
44     protected Measure(Number number, MeasureUnit unit) {\r
45         if (number == null || unit == null) {\r
46             throw new NullPointerException();\r
47         }\r
48         this.number = number;\r
49         this.unit = unit;\r
50     }\r
51     \r
52     /**\r
53      * Returns true if the given object is equal to this object.\r
54      * @return true if this object is equal to the given object\r
55      * @stable ICU 3.0\r
56      */\r
57     public boolean equals(Object obj) {\r
58         if (obj == null) return false;\r
59         if (obj == this) return true;\r
60         try {\r
61             Measure m = (Measure) obj;\r
62             return unit.equals(m.unit) && numbersEqual(number, m.number);\r
63         } catch (ClassCastException e) {\r
64             return false;\r
65         }\r
66     }\r
67     \r
68     /*\r
69      * See if two numbers are identical or have the same double value.\r
70      * @param a A number\r
71      * @param b Another number to be compared with\r
72      * @return Returns true if two numbers are identical or have the same double value.\r
73      */\r
74     // TODO improve this to catch more cases (two different longs that have same double values, BigDecimals, etc)\r
75     private static boolean numbersEqual(Number a, Number b) {\r
76         if (a.equals(b)) {\r
77             return true;\r
78         }\r
79         if (a.doubleValue() == b.doubleValue()) {\r
80             return true;\r
81         }\r
82         return false;\r
83     }\r
84 \r
85     /**\r
86      * Returns a hashcode for this object.\r
87      * @return a 32-bit hash\r
88      * @stable ICU 3.0\r
89      */\r
90     public int hashCode() {\r
91         return number.hashCode() ^ unit.hashCode();\r
92     }\r
93 \r
94     /**\r
95      * Returns a string representation of this object.\r
96      * @return a string representation consisting of the ISO currency\r
97      * code together with the numeric amount\r
98      * @stable ICU 3.0\r
99      */\r
100     public String toString() {\r
101         return number.toString() + ' ' + unit.toString();\r
102     }\r
103 \r
104     /**\r
105      * Returns the numeric value of this object.\r
106      * @return this object's Number\r
107      * @stable ICU 3.0\r
108      */\r
109     public Number getNumber() {\r
110         return number;\r
111     }\r
112 \r
113     /**\r
114      * Returns the unit of this object.\r
115      * @return this object's Unit\r
116      * @stable ICU 3.0\r
117      */\r
118     public MeasureUnit getUnit() {\r
119         return unit;\r
120     }\r
121 }\r