001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package java.lang;
017:
018: /**
019: * Wraps a primitive <code>double</code> as an object.
020: */
021: public final class Double extends Number implements Comparable<Double> {
022: public static final double MAX_VALUE = 1.7976931348623157e+308;
023: public static final double MIN_VALUE = 4.9e-324;
024: public static final double MIN_NORMAL = 2.2250738585072014e-308;
025: public static final int MAX_EXPONENT = 1023;
026: // ==Math.getExponent(Double.MAX_VALUE);
027: public static final int MIN_EXPONENT = -1022;
028: // ==Math.getExponent(Double.MIN_NORMAL);;
029:
030: public static final double NaN = 0d / 0d;
031: public static final double NEGATIVE_INFINITY = -1d / 0d;
032: public static final double POSITIVE_INFINITY = 1d / 0d;
033: public static final int SIZE = 64;
034:
035: public static int compare(double x, double y) {
036: if (x < y) {
037: return -1;
038: } else if (x > y) {
039: return 1;
040: } else {
041: return 0;
042: }
043: }
044:
045: /**
046: * @skip Here for shared implementation with Arrays.hashCode
047: */
048: public static int hashCode(double d) {
049: return (int) d;
050: }
051:
052: public static native boolean isInfinite(double x) /*-{
053: return !isFinite(x);
054: }-*/;
055:
056: public static native boolean isNaN(double x) /*-{
057: return isNaN(x);
058: }-*/;
059:
060: public static double parseDouble(String s)
061: throws NumberFormatException {
062: return __parseAndValidateDouble(s);
063: }
064:
065: public static String toString(double b) {
066: return String.valueOf(b);
067: }
068:
069: public static Double valueOf(double d) {
070: return new Double(d);
071: }
072:
073: public static Double valueOf(String s) throws NumberFormatException {
074: return new Double(Double.parseDouble(s));
075: }
076:
077: private final transient double value;
078:
079: public Double(double value) {
080: this .value = value;
081: }
082:
083: public Double(String s) {
084: value = parseDouble(s);
085: }
086:
087: @Override
088: public byte byteValue() {
089: return (byte) value;
090: }
091:
092: public int compareTo(Double b) {
093: if (value < b.value) {
094: return -1;
095: } else if (value > b.value) {
096: return 1;
097: } else {
098: return 0;
099: }
100: }
101:
102: @Override
103: public double doubleValue() {
104: return value;
105: }
106:
107: @Override
108: public boolean equals(Object o) {
109: return (o instanceof Double) && (((Double) o).value == value);
110: }
111:
112: @Override
113: public float floatValue() {
114: return (float) value;
115: }
116:
117: /**
118: * Performance caution: using Double objects as map keys is not recommended.
119: * Using double values as keys is generally a bad idea due to difficulty
120: * determining exact equality. In addition, there is no efficient JavaScript
121: * equivalent of <code>doubleToIntBits</code>. As a result, this method
122: * computes a hash code by truncating the whole number portion of the double,
123: * which may lead to poor performance for certain value sets if Doubles are
124: * used as keys in a {@link java.util.HashMap}.
125: */
126: @Override
127: public int hashCode() {
128: return hashCode(value);
129: }
130:
131: @Override
132: public int intValue() {
133: return (int) value;
134: }
135:
136: public boolean isInfinite() {
137: return isInfinite(value);
138: }
139:
140: public boolean isNaN() {
141: return isNaN(value);
142: }
143:
144: @Override
145: public long longValue() {
146: return (long) value;
147: }
148:
149: @Override
150: public short shortValue() {
151: return (short) value;
152: }
153:
154: @Override
155: public String toString() {
156: return toString(value);
157: }
158:
159: }
|