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 primitve <code>float</code> as an object.
020: */
021: public final class Float extends Number implements Comparable<Float> {
022: public static final float MAX_VALUE = 3.4028235e+38f;
023: public static final float MIN_VALUE = 1.4e-45f;
024: public static final float MAX_EXPONENT = 127;
025: public static final float MIN_EXPONENT = -126;
026: public static final float MIN_NORMAL = 1.1754943508222875E-38f;
027: public static final float NaN = 0f / 0f;
028: public static final float NEGATIVE_INFINITY = -1f / 0f;
029: public static final float POSITIVE_INFINITY = 1f / 0f;
030: public static final int SIZE = 32;
031:
032: public static int compare(float x, float y) {
033: if (x < y) {
034: return -1;
035: } else if (x > y) {
036: return 1;
037: } else {
038: return 0;
039: }
040: }
041:
042: /**
043: * @skip Here for shared implementation with Arrays.hashCode
044: */
045: public static int hashCode(float f) {
046: return (int) f;
047: }
048:
049: public static native boolean isInfinite(float x) /*-{
050: return !isFinite(x);
051: }-*/;
052:
053: public static native boolean isNaN(float x) /*-{
054: return isNaN(x);
055: }-*/;
056:
057: public static float parseFloat(String s)
058: throws NumberFormatException {
059: return (float) __parseAndValidateDouble(s);
060: }
061:
062: public static String toString(float b) {
063: return String.valueOf(b);
064: }
065:
066: public static Float valueOf(float f) {
067: return new Float(f);
068: }
069:
070: public static Float valueOf(String s) throws NumberFormatException {
071: return new Float(Float.parseFloat(s));
072: }
073:
074: private final transient float value;
075:
076: public Float(float value) {
077: this .value = value;
078: }
079:
080: public Float(String s) {
081: value = parseFloat(s);
082: }
083:
084: @Override
085: public byte byteValue() {
086: return (byte) value;
087: }
088:
089: public int compareTo(Float b) {
090: if (value < b.value) {
091: return -1;
092: } else if (value > b.value) {
093: return 1;
094: } else {
095: return 0;
096: }
097: }
098:
099: @Override
100: public double doubleValue() {
101: return value;
102: }
103:
104: @Override
105: public boolean equals(Object o) {
106: return (o instanceof Float) && (((Float) o).value == value);
107: }
108:
109: @Override
110: public float floatValue() {
111: return value;
112: }
113:
114: /**
115: * Performance caution: using Float objects as map keys is not recommended.
116: * Using floating point values as keys is generally a bad idea due to
117: * difficulty determining exact equality. In addition, there is no efficient
118: * JavaScript equivalent of <code>floatToIntBits</code>. As a result, this
119: * method computes a hash code by truncating the whole number portion of the
120: * float, which may lead to poor performance for certain value sets if Floats
121: * are used as keys in a {@link java.util.HashMap}.
122: */
123: @Override
124: public int hashCode() {
125: return hashCode(value);
126: }
127:
128: @Override
129: public int intValue() {
130: return (int) value;
131: }
132:
133: public boolean isInfinite() {
134: return isInfinite(value);
135: }
136:
137: public boolean isNaN() {
138: return isNaN(value);
139: }
140:
141: @Override
142: public long longValue() {
143: return (long) value;
144: }
145:
146: @Override
147: public short shortValue() {
148: return (short) value;
149: }
150:
151: @Override
152: public String toString() {
153: return toString(value);
154: }
155:
156: }
|