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