001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2001, Institut de Recherche pour le Développement
006: * (C) 1999, Fisheries and Oceans Canada
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Lesser General Public
010: * License as published by the Free Software Foundation;
011: * version 2.1 of the License.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Lesser General Public License for more details.
017: */
018: package org.geotools.measure;
019:
020: // Miscellaneous
021: import javax.units.Unit;
022:
023: import org.geotools.resources.Utilities;
024:
025: /**
026: * A scalar with an unit.
027: *
028: * @since 2.1
029: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/referencing/src/main/java/org/geotools/measure/Measure.java $
030: * @version $Id: Measure.java 20874 2006-08-07 10:00:01Z jgarnett $
031: * @author Martin Desruisseaux
032: */
033: public final class Measure extends Number {
034: /**
035: * For compatibility with different versions.
036: */
037: private static final long serialVersionUID = 6917234039472328164L;
038:
039: /**
040: * The scalar value.
041: */
042: private final double value;
043:
044: /**
045: * The unit.
046: */
047: private final Unit unit;
048:
049: /**
050: * Creates a new measure with the specified value and unit.
051: */
052: public Measure(final double value, final Unit unit) {
053: this .value = value;
054: this .unit = unit;
055: }
056:
057: /** Returns the scalar value. */
058: public double doubleValue() {
059: return (double) value;
060: }
061:
062: /** Returns the scalar value. */
063: public float floatValue() {
064: return (float) value;
065: }
066:
067: /** Returns the scalar value. */
068: public long longValue() {
069: return (long) value;
070: }
071:
072: /** Returns the scalar value. */
073: public int intValue() {
074: return (int) value;
075: }
076:
077: /** Returns the scalar value. */
078: public short shortValue() {
079: return (short) value;
080: }
081:
082: /** Returns the scalar value. */
083: public byte byteValue() {
084: return (byte) value;
085: }
086:
087: /**
088: * Returns the unit.
089: */
090: public Unit getUnit() {
091: return unit;
092: }
093:
094: /**
095: * Returns a hash code value for this measure.
096: */
097: public int hashCode() {
098: long code = Double.doubleToLongBits(value);
099: return (int) code ^ (int) (code >>> 32) ^ unit.hashCode();
100: }
101:
102: /**
103: * Compares this measure with the specified object for equality.
104: */
105: public boolean equals(final Object object) {
106: if (object instanceof Measure) {
107: final Measure that = (Measure) object;
108: return Double.doubleToLongBits(value) == Double
109: .doubleToLongBits(that.value)
110: && Utilities.equals(unit, that.unit);
111: }
112: return false;
113: }
114:
115: /**
116: * Returns a string representation of this measure.
117: */
118: public String toString() {
119: final StringBuffer buffer = new StringBuffer();
120: buffer.append(value);
121: buffer.append(' ');
122: buffer.append(unit);
123: return buffer.toString();
124: }
125: }
|