01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.value;
07:
08: import java.sql.PreparedStatement;
09: import java.sql.SQLException;
10:
11: /**
12: * Implementation of the BOOLEAN data type.
13: */
14: public class ValueBoolean extends Value {
15: public static final int PRECISION = 1;
16: public static final int DISPLAY_SIZE = 5; // false
17:
18: private final Boolean value;
19:
20: private static final ValueBoolean TRUE = new ValueBoolean(true);
21: private static final ValueBoolean FALSE = new ValueBoolean(false);
22:
23: private ValueBoolean(boolean value) {
24: this .value = Boolean.valueOf("" + value);
25: }
26:
27: public int getType() {
28: return Value.BOOLEAN;
29: }
30:
31: public String getSQL() {
32: return getString();
33: }
34:
35: public String getString() {
36: return value.booleanValue() ? "TRUE" : "FALSE";
37: }
38:
39: public Value negate() throws SQLException {
40: return value.booleanValue() ? FALSE : TRUE;
41: }
42:
43: public Boolean getBoolean() {
44: return value;
45: }
46:
47: protected int compareSecure(Value o, CompareMode mode) {
48: boolean v2 = ((ValueBoolean) o).value.booleanValue();
49: boolean v = value.booleanValue();
50: return (v == v2) ? 0 : (v ? 1 : -1);
51: }
52:
53: public long getPrecision() {
54: return PRECISION;
55: }
56:
57: public int hashCode() {
58: return value.booleanValue() ? 1 : 0;
59: }
60:
61: public Object getObject() {
62: return value;
63: }
64:
65: public void set(PreparedStatement prep, int parameterIndex)
66: throws SQLException {
67: prep.setBoolean(parameterIndex, value.booleanValue());
68: }
69:
70: public static ValueBoolean get(boolean b) {
71: return b ? TRUE : FALSE;
72: }
73:
74: public int getDisplaySize() {
75: return DISPLAY_SIZE;
76: }
77:
78: public boolean equals(Object other) {
79: // there are only ever two instances, so the instance must match
80: return this == other;
81: }
82:
83: }
|