KickJava   Java API By Example, From Geeks To Geeks.

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


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.engine.Constants;
11 import org.h2.message.Message;
12
13 public class ValueShort extends Value {
14     public static final int PRECISION = 5;
15
16     private short value;
17
18     private ValueShort(short value) {
19         this.value = value;
20     }
21
22     public Value add(Value v) throws SQLException JavaDoc {
23         ValueShort other = (ValueShort) v;
24         if(Constants.OVERFLOW_EXCEPTIONS) {
25             return checkRange(value + other.value);
26         }
27         return ValueShort.get((short) (value + other.value));
28     }
29     
30     private ValueShort checkRange(int value) throws SQLException JavaDoc {
31         if(value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
32             throw Message.getSQLException(Message.OVERFLOW_FOR_TYPE_1, DataType.getDataType(Value.SHORT).name);
33         } else {
34             return ValueShort.get((short)value);
35         }
36     }
37
38     public int getSignum() {
39         return value == 0 ? 0 : (value < 0 ? -1 : 1);
40     }
41
42     public Value negate() throws SQLException JavaDoc {
43         if(Constants.OVERFLOW_EXCEPTIONS) {
44             return checkRange(-(int)value);
45         }
46         return ValueShort.get((short) (-value));
47     }
48
49     public Value subtract(Value v) throws SQLException JavaDoc {
50         ValueShort other = (ValueShort) v;
51         if(Constants.OVERFLOW_EXCEPTIONS) {
52             return checkRange(value - other.value);
53         }
54         return ValueShort.get((short) (value - other.value));
55     }
56
57     public Value multiply(Value v) throws SQLException JavaDoc {
58         ValueShort other = (ValueShort) v;
59         if(Constants.OVERFLOW_EXCEPTIONS) {
60             return checkRange(value * other.value);
61         }
62         return ValueShort.get((short) (value * other.value));
63     }
64
65     public Value divide(Value v) throws SQLException JavaDoc {
66         ValueShort other = (ValueShort) v;
67         if (other.value == 0) {
68             throw Message.getSQLException(Message.DIVISION_BY_ZERO_1, getSQL());
69         }
70         return ValueShort.get((short) (value / other.value));
71     }
72
73     public String JavaDoc getSQL() {
74         return getString();
75     }
76
77     public int getType() {
78         return Value.SHORT;
79     }
80
81     public short getShort() {
82         return value;
83     }
84
85     protected int compareSecure(Value o, CompareMode mode) {
86         ValueShort v = (ValueShort) o;
87         if (value == v.value) {
88             return 0;
89         }
90         return value > v.value ? 1 : -1;
91     }
92
93     public String JavaDoc getString() {
94         return String.valueOf(value);
95     }
96
97     public long getPrecision() {
98         return PRECISION;
99     }
100
101     public int hashCode() {
102         return value;
103     }
104
105     public Object JavaDoc getObject() {
106         return new Short JavaDoc(value);
107     }
108
109     public void set(PreparedStatement JavaDoc prep, int parameterIndex) throws SQLException JavaDoc {
110         prep.setShort(parameterIndex, value);
111     }
112
113     public static ValueShort get(short i) {
114         return (ValueShort) Value.cache(new ValueShort(i));
115     }
116
117 // public String getJavaString() {
118
// return getString();
119
// }
120

121     public int getDisplaySize() {
122         return PRECISION;
123     }
124     
125     protected boolean isEqual(Value v) {
126         return v instanceof ValueShort && value == ((ValueShort)v).value;
127     }
128
129 }
130
Popular Tags