KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > lang > Float


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package java.lang;
17
18 /**
19  * Wraps a primitve <code>float</code> as an object.
20  */

21 public final class Float extends Number JavaDoc implements Comparable JavaDoc {
22   public static final float MAX_VALUE = 3.4028235e+38f;
23   public static final float MIN_VALUE = 1.4e-45f;
24   public static final float NaN = 0f / 0f;
25   public static final float NEGATIVE_INFINITY = -1f / 0f;
26   public static final float POSITIVE_INFINITY = 1f / 0f;
27
28   public static int compare(float x, float y) {
29     if (x < y) {
30       return -1;
31     } else if (x > y) {
32       return 1;
33     } else {
34       return 0;
35     }
36   }
37
38   public static native boolean isInfinite(float x) /*-{
39     return !isFinite(x);
40   }-*/
;
41
42   public static native boolean isNaN(float x) /*-{
43     return isNaN(x);
44   }-*/
;
45
46   public static float parseFloat(String JavaDoc s) throws NumberFormatException JavaDoc {
47     return (float)__parseAndValidateDouble(s);
48   }
49
50   public static String JavaDoc toString(float b) {
51     return String.valueOf(b);
52   }
53
54   public static Float JavaDoc valueOf(String JavaDoc s) throws NumberFormatException JavaDoc {
55     return new Float JavaDoc(Float.parseFloat(s));
56   }
57
58   private final transient float value;
59
60   public Float(float value) {
61     this.value = value;
62   }
63
64   public Float(String JavaDoc s) {
65     value = parseFloat(s);
66   }
67
68   public byte byteValue() {
69     return (byte) value;
70   }
71
72   public int compareTo(Float JavaDoc b) {
73     if (value < b.value) {
74       return -1;
75     } else if (value > b.value) {
76       return 1;
77     } else {
78       return 0;
79     }
80   }
81
82   public int compareTo(Object JavaDoc o) {
83     return compareTo((Float JavaDoc) o);
84   }
85
86   public double doubleValue() {
87     return value;
88   }
89
90   public boolean equals(Object JavaDoc o) {
91     return (o instanceof Float JavaDoc) && (((Float JavaDoc) o).value == value);
92   }
93
94   public float floatValue() {
95     return value;
96   }
97
98   /**
99    * Performance caution: using Float objects as map keys is not recommended.
100    * Using floating point values as keys is generally a bad idea due to
101    * difficulty determining exact equality. In addition, there is no efficient
102    * JavaScript equivalent of <code>floatToIntBits</code>. As a result, this
103    * method computes a hash code by truncating the whole number portion of the
104    * float, which may lead to poor performance for certain value sets if Floats
105    * are used as keys in a {@link java.util.HashMap}.
106    */

107   public int hashCode() {
108     return (int) value;
109   }
110
111   public int intValue() {
112     return (int) value;
113   }
114
115   public boolean isInfinite() {
116     return isInfinite(value);
117   }
118
119   public boolean isNaN() {
120     return isNaN(value);
121   }
122
123   public long longValue() {
124     return (long) value;
125   }
126
127   public short shortValue() {
128     return (short) value;
129   }
130
131   public String JavaDoc toString() {
132     return toString(value);
133   }
134
135 }
136
Popular Tags