2 **********************************************************************
\r
3 * Copyright (c) 2004-2009, 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
13 import java.lang.Number;
\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
21 * <p>Measure objects are parsed and formatted by subclasses of
\r
24 * <p>Measure objects are immutable.
\r
26 * @see java.lang.Number
\r
27 * @see com.ibm.icu.util.MeasureUnit
\r
28 * @see com.ibm.icu.text.MeasureFormat
\r
32 public abstract class Measure {
\r
34 private Number number;
\r
36 private MeasureUnit unit;
\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
44 protected Measure(Number number, MeasureUnit unit) {
\r
45 if (number == null || unit == null) {
\r
46 throw new NullPointerException();
\r
48 this.number = number;
\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
57 public boolean equals(Object obj) {
\r
58 if (obj == null) return false;
\r
59 if (obj == this) return true;
\r
61 Measure m = (Measure) obj;
\r
62 return unit.equals(m.unit) && numbersEqual(number, m.number);
\r
63 } catch (ClassCastException e) {
\r
69 * See if two numbers are identical or have the same double value.
\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
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
79 if (a.doubleValue() == b.doubleValue()) {
\r
86 * Returns a hashcode for this object.
\r
87 * @return a 32-bit hash
\r
90 public int hashCode() {
\r
91 return number.hashCode() ^ unit.hashCode();
\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
100 public String toString() {
\r
101 return number.toString() + ' ' + unit.toString();
\r
105 * Returns the numeric value of this object.
\r
106 * @return this object's Number
\r
109 public Number getNumber() {
\r
114 * Returns the unit of this object.
\r
115 * @return this object's Unit
\r
118 public MeasureUnit getUnit() {
\r