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