001: /*
002: **********************************************************************
003: * Copyright (c) 2004-2006, International Business Machines
004: * Corporation and others. All Rights Reserved.
005: **********************************************************************
006: * Author: Alan Liu
007: * Created: April 20, 2004
008: * Since: ICU 3.0
009: **********************************************************************
010: */
011: package com.ibm.icu.util;
012:
013: import java.lang.Number;
014:
015: /**
016: * An amount of a specified unit, consisting of a Number and a Unit.
017: * For example, a length measure consists of a Number and a length
018: * unit, such as feet or meters. This is an abstract class.
019: * Subclasses specify a concrete Unit type.
020: *
021: * <p>Measure objects are parsed and formatted by subclasses of
022: * MeasureFormat.
023: *
024: * <p>Measure objects are immutable.
025: *
026: * @see java.lang.Number
027: * @see com.ibm.icu.util.MeasureUnit
028: * @see com.ibm.icu.text.MeasureFormat
029: * @author Alan Liu
030: * @stable ICU 3.0
031: */
032: public abstract class Measure {
033:
034: private Number number;
035:
036: private MeasureUnit unit;
037:
038: /**
039: * Constructs a new object given a number and a unit.
040: * @param number the number
041: * @param unit the unit
042: * @stable ICU 3.0
043: */
044: protected Measure(Number number, MeasureUnit unit) {
045: if (number == null || unit == null) {
046: throw new NullPointerException();
047: }
048: this .number = number;
049: this .unit = unit;
050: }
051:
052: /**
053: * Returns true if the given object is equal to this object.
054: * @return true if this object is equal to the given object
055: * @stable ICU 3.0
056: */
057: public boolean equals(Object obj) {
058: if (obj == null)
059: return false;
060: if (obj == this )
061: return true;
062: try {
063: Measure m = (Measure) obj;
064: return number.equals(m.number) && unit.equals(m.unit);
065: } catch (ClassCastException e) {
066: return false;
067: }
068: }
069:
070: /**
071: * Returns a hashcode for this object.
072: * @return a 32-bit hash
073: * @stable ICU 3.0
074: */
075: public int hashCode() {
076: return number.hashCode() ^ unit.hashCode();
077: }
078:
079: /**
080: * Returns a string representation of this object.
081: * @return a string representation consisting of the ISO currency
082: * code together with the numeric amount
083: * @stable ICU 3.0
084: */
085: public String toString() {
086: return number.toString() + ' ' + unit.toString();
087: }
088:
089: /**
090: * Returns the numeric value of this object.
091: * @return this object's Number
092: * @stable ICU 3.0
093: */
094: public Number getNumber() {
095: return number;
096: }
097:
098: /**
099: * Returns the unit of this object.
100: * @return this object's Unit
101: * @stable ICU 3.0
102: */
103: public MeasureUnit getUnit() {
104: return unit;
105: }
106: }
|