2 **********************************************************************
\r
3 * Copyright (c) 2004-2010, International Business Machines
\r
4 * Corporation and others. All Rights Reserved.
\r
5 **********************************************************************
\r
7 * Created: April 20, 2004
\r
9 **********************************************************************
\r
11 package com.ibm.icu.util;
\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
20 * <p>Measure objects are parsed and formatted by subclasses of
\r
23 * <p>Measure objects are immutable.
\r
25 * @see java.lang.Number
\r
26 * @see com.ibm.icu.util.MeasureUnit
\r
27 * @see com.ibm.icu.text.MeasureFormat
\r
31 public abstract class Measure {
\r
33 private Number number;
\r
35 private MeasureUnit unit;
\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
43 protected Measure(Number number, MeasureUnit unit) {
\r
44 if (number == null || unit == null) {
\r
45 throw new NullPointerException();
\r
47 this.number = number;
\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
56 public boolean equals(Object obj) {
\r
57 if (obj == null) return false;
\r
58 if (obj == this) return true;
\r
60 Measure m = (Measure) obj;
\r
61 return unit.equals(m.unit) && numbersEqual(number, m.number);
\r
62 } catch (ClassCastException e) {
\r
68 * See if two numbers are identical or have the same double value.
\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
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
78 if (a.doubleValue() == b.doubleValue()) {
\r
85 * Returns a hashcode for this object.
\r
86 * @return a 32-bit hash
\r
89 public int hashCode() {
\r
90 return number.hashCode() ^ unit.hashCode();
\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
99 public String toString() {
\r
100 return number.toString() + ' ' + unit.toString();
\r
104 * Returns the numeric value of this object.
\r
105 * @return this object's Number
\r
108 public Number getNumber() {
\r
113 * Returns the unit of this object.
\r
114 * @return this object's Unit
\r
117 public MeasureUnit getUnit() {
\r