KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > value > ValueFloat


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.value;
6
7 import java.sql.PreparedStatement JavaDoc;
8 import java.sql.SQLException JavaDoc;
9
10 import org.h2.message.Message;
11
12 /**
13  * @author Thomas
14  */

15
16 public class ValueFloat extends Value {
17     public static final int PRECISION = 7;
18
19     private float value;
20     private static final float FLOAT_ZERO = 0.0F;
21     private static final float FLOAT_ONE = 1.0F;
22     private static final ValueFloat ZERO = new ValueFloat(FLOAT_ZERO);
23     private static final ValueFloat ONE = new ValueFloat(FLOAT_ONE);
24
25     private ValueFloat(float value) {
26         this.value = value;
27     }
28
29     public Value add(Value v) {
30         ValueFloat v2 = (ValueFloat) v;
31         return ValueFloat.get(value + v2.value);
32     }
33
34     public Value subtract(Value v) {
35         ValueFloat v2 = (ValueFloat) v;
36         return ValueFloat.get(value - v2.value);
37     }
38
39     public Value negate() {
40         return ValueFloat.get(-value);
41     }
42
43     public Value multiply(Value v) {
44         ValueFloat v2 = (ValueFloat) v;
45         return ValueFloat.get(value * v2.value);
46     }
47
48     public Value divide(Value v) throws SQLException JavaDoc {
49         ValueFloat v2 = (ValueFloat) v;
50         if (v2.value == 0.0) {
51             throw Message.getSQLException(Message.DIVISION_BY_ZERO_1, getSQL());
52         }
53         return ValueFloat.get(value / v2.value);
54     }
55
56     public String JavaDoc getSQL() {
57         return getString();
58     }
59
60     public int getType() {
61         return Value.FLOAT;
62     }
63
64     protected int compareSecure(Value o, CompareMode mode) {
65         ValueFloat v = (ValueFloat) o;
66         if (value == v.value) {
67             return 0;
68         }
69         return value > v.value ? 1 : -1;
70     }
71
72     public int getSignum() {
73         return value == 0 ? 0 : (value < 0 ? -1 : 1);
74     }
75
76     public float getFloat() {
77         return value;
78     }
79
80     public String JavaDoc getString() {
81         return String.valueOf(value);
82     }
83
84     public long getPrecision() {
85         return PRECISION;
86     }
87
88     public int getScale() {
89         // TODO value: what is the scale of a float?
90
return 0;
91     }
92
93     public int hashCode() {
94         long hash = Float.floatToIntBits(value);
95         return (int) (hash ^ (hash >> 32));
96     }
97
98     public Object JavaDoc getObject() {
99         return new Float JavaDoc(value);
100     }
101
102     public void set(PreparedStatement JavaDoc prep, int parameterIndex) throws SQLException JavaDoc {
103         prep.setFloat(parameterIndex, value);
104     }
105
106     public static ValueFloat get(float d) {
107         if (FLOAT_ZERO == d) {
108             return ZERO;
109         } else if (FLOAT_ONE == d) {
110             return ONE;
111         }
112         return (ValueFloat) Value.cache(new ValueFloat(d));
113     }
114
115 // public String getJavaString() {
116
// return getString();
117
// }
118

119     public int getDisplaySize() {
120         return PRECISION + 2;
121     }
122     
123     protected boolean isEqual(Value v) {
124         return v instanceof ValueFloat && value == ((ValueFloat)v).value;
125     }
126
127 }
128
Popular Tags