KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > util > Measure


1 /*
2 **********************************************************************
3 * Copyright (c) 2004-2006, 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 import java.lang.Number JavaDoc;
14
15 /**
16  * An amount of a specified unit, consisting of a Number and a Unit.
17  * For example, a length measure consists of a Number and a length
18  * unit, such as feet or meters. This is an abstract class.
19  * Subclasses specify a concrete Unit type.
20  *
21  * <p>Measure objects are parsed and formatted by subclasses of
22  * MeasureFormat.
23  *
24  * <p>Measure objects are immutable.
25  *
26  * @see java.lang.Number
27  * @see com.ibm.icu.util.MeasureUnit
28  * @see com.ibm.icu.text.MeasureFormat
29  * @author Alan Liu
30  * @stable ICU 3.0
31  */

32 public abstract class Measure {
33     
34     private Number JavaDoc number;
35
36     private MeasureUnit unit;
37
38     /**
39      * Constructs a new object given a number and a unit.
40      * @param number the number
41      * @param unit the unit
42      * @stable ICU 3.0
43      */

44     protected Measure(Number JavaDoc number, MeasureUnit unit) {
45         if (number == null || unit == null) {
46             throw new NullPointerException JavaDoc();
47         }
48         this.number = number;
49         this.unit = unit;
50     }
51     
52     /**
53      * Returns true if the given object is equal to this object.
54      * @return true if this object is equal to the given object
55      * @stable ICU 3.0
56      */

57     public boolean equals(Object JavaDoc obj) {
58         if (obj == null) return false;
59         if (obj == this) return true;
60         try {
61             Measure m = (Measure) obj;
62             return number.equals(m.number) && unit.equals(m.unit);
63         } catch (ClassCastException JavaDoc e) {
64             return false;
65         }
66     }
67
68     /**
69      * Returns a hashcode for this object.
70      * @return a 32-bit hash
71      * @stable ICU 3.0
72      */

73     public int hashCode() {
74         return number.hashCode() ^ unit.hashCode();
75     }
76
77     /**
78      * Returns a string representation of this object.
79      * @return a string representation consisting of the ISO currency
80      * code together with the numeric amount
81      * @stable ICU 3.0
82      */

83     public String JavaDoc toString() {
84         return number.toString() + ' ' + unit.toString();
85     }
86
87     /**
88      * Returns the numeric value of this object.
89      * @return this object's Number
90      * @stable ICU 3.0
91      */

92     public Number JavaDoc getNumber() {
93         return number;
94     }
95
96     /**
97      * Returns the unit of this object.
98      * @return this object's Unit
99      * @stable ICU 3.0
100      */

101     public MeasureUnit getUnit() {
102         return unit;
103     }
104 }
105
Popular Tags