KickJava   Java API By Example, From Geeks To Geeks.

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


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.math.BigInteger JavaDoc;
8 import java.sql.PreparedStatement JavaDoc;
9 import java.sql.SQLException JavaDoc;
10
11 import org.h2.engine.Constants;
12 import org.h2.message.Message;
13
14 public class ValueLong extends Value {
15
16     private long value;
17     
18     public static final int PRECISION = 19;
19     private static final int STATIC_SIZE = 10;
20     private static ValueLong[] cache;
21     private static final BigInteger JavaDoc MIN = new BigInteger JavaDoc("" + Long.MIN_VALUE);
22     private static final BigInteger JavaDoc MAX = new BigInteger JavaDoc("" + Long.MAX_VALUE);
23
24     static {
25         cache = new ValueLong[STATIC_SIZE];
26         for (int i = 0; i < STATIC_SIZE; i++) {
27             cache[i] = new ValueLong(i);
28         }
29     }
30
31     private ValueLong(long value) {
32         this.value = value;
33     }
34
35     public Value add(Value v) throws SQLException JavaDoc {
36         ValueLong other = (ValueLong) v;
37         if(Constants.OVERFLOW_EXCEPTIONS) {
38             long result = value + other.value;
39             int sv = value == 0 ? 0 : (value < 0 ? -1 : 1);
40             int so = other.value == 0 ? 0 : (other.value < 0 ? -1 : 1);
41             int sr = result == 0 ? 0 : (result < 0 ? -1 : 1);
42             // if the operands have different signs overflow can not occur
43
// if the operands have the same sign, and the result has a different sign, then it is an overflow
44
// it can not be an overflow when one of the operands is 0
45
if(sv!=so || sr==so || sv==0 || so==0) {
46                 return ValueLong.get(result);
47             }
48             throw getOverflow();
49         }
50         return ValueLong.get(value + other.value);
51     }
52     
53     public int getSignum() {
54         return value == 0 ? 0 : (value < 0 ? -1 : 1);
55     }
56
57     public Value negate() throws SQLException JavaDoc {
58         if(Constants.OVERFLOW_EXCEPTIONS) {
59             if(value == Long.MIN_VALUE) {
60                 throw getOverflow();
61             }
62         }
63         return ValueLong.get(-value);
64     }
65     
66     private SQLException JavaDoc getOverflow() {
67         return Message.getSQLException(Message.OVERFLOW_FOR_TYPE_1, DataType.getDataType(Value.LONG).name);
68     }
69
70     public Value subtract(Value v) throws SQLException JavaDoc {
71         ValueLong other = (ValueLong) v;
72         if(Constants.OVERFLOW_EXCEPTIONS) {
73             int sv = value == 0 ? 0 : (value < 0 ? -1 : 1);
74             int so = other.value == 0 ? 0 : (other.value < 0 ? -1 : 1);
75             // if the operands have the same sign, then overflow can not occur
76
// if the second operand is 0, then overflow can not occur
77
if(sv==so || so==0) {
78                 return ValueLong.get(value - other.value);
79             }
80             // now, if the other value is Long.MIN_VALUE, it must be an overflow
81
// x - Long.MIN_VALUE overflows for x>=0
82
return add(other.negate());
83         }
84         return ValueLong.get(value - other.value);
85     }
86     
87     private boolean isInteger(long a) {
88         return a >= Integer.MIN_VALUE && a <= Integer.MAX_VALUE;
89     }
90
91     public Value multiply(Value v) throws SQLException JavaDoc {
92         ValueLong other = (ValueLong) v;
93         if(Constants.OVERFLOW_EXCEPTIONS) {
94             long result = value * other.value;
95             if(value == 0 || value == 1 || other.value == 0 || other.value == 1) {
96                 return ValueLong.get(result);
97             }
98             if(isInteger(value) && isInteger(other.value)) {
99                 return ValueLong.get(result);
100             }
101             // just checking one case is not enough: Long.MIN_VALUE * -1
102
// probably this is correct but I'm not sure
103
// if(result / value == other.value && result / other.value == value) {
104
// return ValueLong.get(result);
105
//}
106
BigInteger JavaDoc bv = new BigInteger JavaDoc("" + value);
107             BigInteger JavaDoc bo = new BigInteger JavaDoc("" + other.value);
108             BigInteger JavaDoc br = bv.multiply(bo);
109             if(br.compareTo(MIN) < 0 || br.compareTo(MAX) > 0) {
110                 throw getOverflow();
111             }
112             return ValueLong.get(br.longValue());
113         }
114         return ValueLong.get(value * other.value);
115     }
116
117     public Value divide(Value v) throws SQLException JavaDoc {
118         ValueLong other = (ValueLong) v;
119         if (other.value == 0) {
120             throw Message.getSQLException(Message.DIVISION_BY_ZERO_1, getSQL());
121         }
122         return ValueLong.get(value / other.value);
123     }
124
125     public String JavaDoc getSQL() {
126         return getString();
127     }
128
129     public int getType() {
130         return Value.LONG;
131     }
132
133     public long getLong() {
134         return value;
135     }
136
137     protected int compareSecure(Value o, CompareMode mode) {
138         ValueLong v = (ValueLong) o;
139         if (value == v.value) {
140             return 0;
141         }
142         return value > v.value ? 1 : -1;
143     }
144
145     public String JavaDoc getString() {
146         return String.valueOf(value);
147     }
148
149     public long getPrecision() {
150         return PRECISION;
151     }
152
153     public int hashCode() {
154         return (int) (value ^ (value >> 32));
155     }
156
157     public Object JavaDoc getObject() {
158         return new Long JavaDoc(value);
159     }
160
161     public void set(PreparedStatement JavaDoc prep, int parameterIndex) throws SQLException JavaDoc {
162         prep.setLong(parameterIndex, value);
163     }
164
165     public static ValueLong get(long i) {
166         if (i >= 0 && i < STATIC_SIZE) {
167             return cache[(int) i];
168         }
169         return (ValueLong) Value.cache(new ValueLong(i));
170     }
171
172 // public String getJavaString() {
173
// return getString();
174
// }
175

176     public int getDisplaySize() {
177         return PRECISION;
178     }
179     
180     protected boolean isEqual(Value v) {
181         return v instanceof ValueLong && value == ((ValueLong)v).value;
182     }
183
184 }
185
Popular Tags