1/* GENERATED SOURCE. DO NOT MODIFY. */
2// © 2016 and later: Unicode, Inc. and others.
3// License & terms of use: http://www.unicode.org/copyright.html#License
4/*
5**********************************************************************
6* Copyright (c) 2004-2013, International Business Machines
7* Corporation and others.  All Rights Reserved.
8**********************************************************************
9* Author: Alan Liu
10* Created: April 20, 2004
11* Since: ICU 3.0
12**********************************************************************
13*/
14package android.icu.util;
15
16
17/**
18 * An amount of a specified unit, consisting of a Number and a Unit.
19 * For example, a length measure consists of a Number and a length
20 * unit, such as feet or meters.
21 *
22 * <p>Measure objects are parsed and formatted by subclasses of
23 * MeasureFormat.
24 *
25 * <p>Measure objects are immutable. All subclasses must guarantee that.
26 * (However, subclassing is discouraged.)
27 *
28 * @see java.lang.Number
29 * @see android.icu.util.MeasureUnit
30 * @see android.icu.text.MeasureFormat
31 * @author Alan Liu
32 */
33public class Measure {
34
35    private final Number number;
36    private final 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     */
43    public Measure(Number number, MeasureUnit unit) {
44        if (number == null || unit == null) {
45            throw new NullPointerException("Number and MeasureUnit must not be null");
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     */
55    @Override
56    public boolean equals(Object obj) {
57        if (obj == this) {
58            return true;
59        }
60        if (!(obj instanceof Measure)) {
61            return false;
62        }
63        Measure m = (Measure) obj;
64        return unit.equals(m.unit) && numbersEqual(number, m.number);
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     */
88    @Override
89    public int hashCode() {
90        return 31 * Double.valueOf(number.doubleValue()).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     */
98    @Override
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     */
107    public Number getNumber() {
108        return number;
109    }
110
111    /**
112     * Returns the unit of this object.
113     * @return this object's Unit
114     */
115    public MeasureUnit getUnit() {
116        return unit;
117    }
118}
119