Variant.java

biz/hammurapi/sql/Variant.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 070-B Cyclomatic complexity is too high: 22, maximum allowed is 20 1 177:9
Java Inspector 086 Use equals() instead of == or != to compare objects. 1 133:98
Java Inspector 086 Use equals() instead of == or != to compare objects. 1 182:32
Java Inspector 015 Do not change parameter value. For comprehensibility, formal parameters should be final 2 101:47
Java Inspector 015 Do not change parameter value. For comprehensibility, formal parameters should be final 2 104:44
Java Inspector 015 Do not change parameter value. For comprehensibility, formal parameters should be final 2 107:46
Java Inspector 015 Do not change parameter value. For comprehensibility, formal parameters should be final 2 110:46
Java Inspector 015 Do not change parameter value. For comprehensibility, formal parameters should be final 2 113:45
Java Inspector 015 Do not change parameter value. For comprehensibility, formal parameters should be final 2 116:43
Java Inspector 015 Do not change parameter value. For comprehensibility, formal parameters should be final 2 119:44
Java Inspector 015 Do not change parameter value. For comprehensibility, formal parameters should be final 2 122:46
Java Inspector 015 Do not change parameter value. For comprehensibility, formal parameters should be final 2 127:54
Java Inspector 015 Do not change parameter value. For comprehensibility, formal parameters should be final 2 130:93
Java Inspector 015 Do not change parameter value. For comprehensibility, formal parameters should be final 2 145:46
Java Inspector 015 Do not change parameter value. For comprehensibility, formal parameters should be final 2 148:45
Java Inspector 070-A Cyclomatic complexity is too high: 18, maximum allowed is 12 2 98:9
Java Inspector 070-A Cyclomatic complexity is too high: 22, maximum allowed is 12 2 177:9
Java Inspector 082 Parenthesis are redundant. 2 163:57
Java Inspector 082 Parenthesis are redundant. 2 166:62
Java Inspector 082 Parenthesis are redundant. 2 168:36
Java Inspector 082 Parenthesis are redundant. 2 172:36
Java Inspector 089 Undocumented constructor 2 42:9
Java Inspector 089 Undocumented constructor 2 47:9
Java Inspector 089 Undocumented constructor 2 52:9
Java Inspector 089 Undocumented constructor 2 57:9
Java Inspector 089 Undocumented constructor 2 62:9
Java Inspector 089 Undocumented constructor 2 67:9
Java Inspector 089 Undocumented constructor 2 72:9
Java Inspector 089 Undocumented constructor 2 77:9
Java Inspector 089 Undocumented constructor 2 82:9
Java Inspector 089 Undocumented constructor 2 87:9
Java Inspector 089 Undocumented constructor 2 93:9
Java Inspector 089 Undocumented method 2 98:9
Java Inspector 089 Undocumented method 2 156:9
Java Inspector 089 Undocumented method 2 177:9
Java Inspector 003 do, while, if, and for statements need a brace enclosed block 3 179:25
Java Inspector 003 do, while, if, and for statements need a brace enclosed block 3 181:25
Java Inspector 003 do, while, if, and for statements need a brace enclosed block 3 183:25
Java Inspector 003 do, while, if, and for statements need a brace enclosed block 3 186:25
Java Inspector 003 do, while, if, and for statements need a brace enclosed block 3 188:25
Java Inspector 003 do, while, if, and for statements need a brace enclosed block 3 190:25
Java Inspector 003 do, while, if, and for statements need a brace enclosed block 3 193:25
Java Inspector 003 do, while, if, and for statements need a brace enclosed block 3 196:25
Java Inspector 003 do, while, if, and for statements need a brace enclosed block 3 198:25
Java Inspector 003 do, while, if, and for statements need a brace enclosed block 3 200:25
Java Inspector 003 do, while, if, and for statements need a brace enclosed block 3 203:33
Java Inspector 003 do, while, if, and for statements need a brace enclosed block 3 205:25
Java Inspector 003 do, while, if, and for statements need a brace enclosed block 3 207:25
Java Inspector 003 do, while, if, and for statements need a brace enclosed block 3 209:25
Java Inspector 003 do, while, if, and for statements need a brace enclosed block 3 212:33
Java Inspector 003 do, while, if, and for statements need a brace enclosed block 3 214:25
Java Inspector 003 do, while, if, and for statements need a brace enclosed block 3 216:25
Java Inspector 025 Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] 3 157:35
Java Inspector 025 Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] 3 159:59
Java Inspector 025 Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] 3 159:66
Java Inspector 025 Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] 3 163:67
Java Inspector 025 Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] 3 166:77
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 107:53
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 126:37
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 151:56
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 42:9
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 47:9
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 52:9
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 57:9
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 62:9
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 67:9
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 72:9
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 77:9
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 82:9
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 87:9
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 93:9
Java Inspector 090 Unnecessary else part in if. The main part terminates control flow (return, break, throw, or continue). 3 136:49

Source code

1package biz.hammurapi.sql;
2
3import java.io.Serializable;
4import java.lang.reflect.Method;
5import java.sql.PreparedStatement;
6import java.sql.SQLException;
7
8import biz.hammurapi.sql.columns.ObjectColumn;
9
10/**
11 * Wrapper for different types.
12 * @author Pavel
13 */
14public class Variant implements Parameterizer, Serializable {
15
16 private static final int TYPE_BOOLEAN = 0;
17 private static final int TYPE_BYTE = 1;
18 private static final int TYPE_CHAR = 2;
19 private static final int TYPE_DOUBLE = 3;
20 private static final int TYPE_FLOAT = 4;
21 private static final int TYPE_INT = 5;
22 private static final int TYPE_LONG = 6;
23 private static final int TYPE_STRING = 7;
24 private static final int TYPE_OBJECT = 8;
25 private static final int TYPE_TYPED_OBJECT = 9;
26 private static final int TYPE_SHORT = 10;
27
28 private int type;
29
30 private boolean booleanValue;
31 private byte byteValue;
32 private char charValue;
33 private double doubleValue;
34 private float floatValue;
35 private int intValue;
36 private long longValue;
37 private String stringValue;
38 private Object objectValue;
39 private int sqlType;
40 private short shortValue;
41
42 public Variant(boolean value) {
43 type = TYPE_BOOLEAN;
44 this.booleanValue = value;
45 }
46
47 public Variant(byte value) {
48 type = TYPE_BYTE;
49 this.byteValue = value;
50 }
51
52 public Variant(char value) {
53 type = TYPE_CHAR;
54 this.charValue = value;
55 }
56
57 public Variant(double value) {
58 type = TYPE_DOUBLE;
59 this.doubleValue = value;
60 }
61
62 public Variant(float value) {
63 type = TYPE_FLOAT;
64 this.floatValue = value;
65 }
66
67 public Variant(int value) {
68 type = TYPE_INT;
69 this.intValue = value;
70 }
71
72 public Variant(long value) {
73 type = TYPE_LONG;
74 this.longValue = value;
75 }
76
77 public Variant(String value) {
78 type = TYPE_STRING;
79 this.stringValue = value;
80 }
81
82 public Variant(Object value) {
83 type = TYPE_OBJECT;
84 this.objectValue = value;
85 }
86
87 public Variant(Object value, int sqlType) {
88 type = TYPE_TYPED_OBJECT;
89 this.objectValue = value;
90 this.sqlType = sqlType;
91 }
92
93 public Variant(short value) {
94 type = TYPE_SHORT;
95 this.shortValue = value;
96 }
97
98 public int parameterize(PreparedStatement ps, int idx) throws SQLException {
99 switch (type) {
100 case TYPE_BOOLEAN:
101 ps.setBoolean(idx++, booleanValue);
102 break;
103 case TYPE_BYTE:
104 ps.setByte(idx++, byteValue);
105 break;
106 case TYPE_CHAR:
107 ps.setString(idx++, ""+charValue);
108 break;
109 case TYPE_DOUBLE:
110 ps.setDouble(idx++, doubleValue);
111 break;
112 case TYPE_FLOAT:
113 ps.setFloat(idx++, floatValue);
114 break;
115 case TYPE_INT:
116 ps.setInt(idx++, intValue);
117 break;
118 case TYPE_LONG:
119 ps.setLong(idx++, longValue);
120 break;
121 case TYPE_STRING:
122 ps.setString(idx++, stringValue);
123 break;
124 case TYPE_OBJECT:
125 Method setter = ObjectColumn.findSetter(objectValue.getClass());
126 if ("setObject".equals(setter.getName())) {
127 ps.setObject(idx++, objectValue);
128 } else {
129 try {
130 setter.invoke(ps, new Object[] {new Integer(idx++), objectValue});
131 } catch (Exception e) {
132 Throwable cause = e;
133 while (cause.getCause()!=null && cause.getCause()!=cause) {
134 cause = cause.getCause();
135 }
136 if (cause instanceof SQLException) {
137 throw (SQLException) cause;
138 } else {
139 throw new SQLRuntimeException(e);
140 }
141 }
142 }
143 break;
144 case TYPE_TYPED_OBJECT:
145 ps.setObject(idx++, objectValue, sqlType);
146 break;
147 case TYPE_SHORT:
148 ps.setShort(idx++, shortValue);
149 break;
150 default:
151 throw new SQLException("Unrecognized value type: "+type);
152 }
153 return idx;
154 }
155
156 public int hashCode() {
157 final int prime = 31;
158 int result = 1;
159 result = prime * result + (booleanValue ? 1231 : 1237);
160 result = prime * result + charValue;
161 long temp;
162 temp = Double.doubleToLongBits(doubleValue);
163 result = prime * result + (int) (temp ^ (temp >>> 32));
164 result = prime * result + Float.floatToIntBits(floatValue);
165 result = prime * result + intValue;
166 result = prime * result + (int) (longValue ^ (longValue >>> 32));
167 result = prime * result
168 + ((objectValue == null) ? 0 : objectValue.hashCode());
169 result = prime * result + shortValue;
170 result = prime * result + sqlType;
171 result = prime * result
172 + ((stringValue == null) ? 0 : stringValue.hashCode());
173 result = prime * result + type;
174 return result;
175 }
176
177 public boolean equals(Object obj) {
178 if (this == obj)
179 return true;
180 if (obj == null)
181 return false;
182 if (getClass() != obj.getClass())
183 return false;
184 final Variant other = (Variant) obj;
185 if (booleanValue != other.booleanValue)
186 return false;
187 if (byteValue != other.byteValue)
188 return false;
189 if (charValue != other.charValue)
190 return false;
191 if (Double.doubleToLongBits(doubleValue) != Double
192 .doubleToLongBits(other.doubleValue))
193 return false;
194 if (Float.floatToIntBits(floatValue) != Float
195 .floatToIntBits(other.floatValue))
196 return false;
197 if (intValue != other.intValue)
198 return false;
199 if (longValue != other.longValue)
200 return false;
201 if (objectValue == null) {
202 if (other.objectValue != null)
203 return false;
204 } else if (!objectValue.equals(other.objectValue))
205 return false;
206 if (shortValue != other.shortValue)
207 return false;
208 if (sqlType != other.sqlType)
209 return false;
210 if (stringValue == null) {
211 if (other.stringValue != null)
212 return false;
213 } else if (!stringValue.equals(other.stringValue))
214 return false;
215 if (type != other.type)
216 return false;
217 return true;
218 }
219
220}
221