1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.value; |
7 | |
8 | import java.sql.PreparedStatement; |
9 | import java.sql.SQLException; |
10 | |
11 | /** |
12 | * Implementation of the BOOLEAN data type. |
13 | */ |
14 | public class ValueBoolean extends Value { |
15 | |
16 | /** |
17 | * The precision in digits. |
18 | */ |
19 | public static final int PRECISION = 1; |
20 | |
21 | /** |
22 | * The maximum display size of a boolean. |
23 | * Example: FALSE |
24 | */ |
25 | public static final int DISPLAY_SIZE = 5; |
26 | |
27 | /** |
28 | * Of type Object so that Tomcat doesn't set it to null. |
29 | */ |
30 | private static final Object TRUE = new ValueBoolean(true); |
31 | private static final Object FALSE = new ValueBoolean(false); |
32 | |
33 | private final Boolean value; |
34 | |
35 | private ValueBoolean(boolean value) { |
36 | this.value = Boolean.valueOf(value); |
37 | } |
38 | |
39 | @Override |
40 | public int getType() { |
41 | return Value.BOOLEAN; |
42 | } |
43 | |
44 | @Override |
45 | public String getSQL() { |
46 | return getString(); |
47 | } |
48 | |
49 | @Override |
50 | public String getString() { |
51 | return value.booleanValue() ? "TRUE" : "FALSE"; |
52 | } |
53 | |
54 | @Override |
55 | public Value negate() { |
56 | return (ValueBoolean) (value.booleanValue() ? FALSE : TRUE); |
57 | } |
58 | |
59 | @Override |
60 | public Boolean getBoolean() { |
61 | return value; |
62 | } |
63 | |
64 | @Override |
65 | protected int compareSecure(Value o, CompareMode mode) { |
66 | boolean v2 = ((ValueBoolean) o).value.booleanValue(); |
67 | boolean v = value.booleanValue(); |
68 | return (v == v2) ? 0 : (v ? 1 : -1); |
69 | } |
70 | |
71 | @Override |
72 | public long getPrecision() { |
73 | return PRECISION; |
74 | } |
75 | |
76 | @Override |
77 | public int hashCode() { |
78 | return value.booleanValue() ? 1 : 0; |
79 | } |
80 | |
81 | @Override |
82 | public Object getObject() { |
83 | return value; |
84 | } |
85 | |
86 | @Override |
87 | public void set(PreparedStatement prep, int parameterIndex) |
88 | throws SQLException { |
89 | prep.setBoolean(parameterIndex, value.booleanValue()); |
90 | } |
91 | |
92 | /** |
93 | * Get the boolean value for the given boolean. |
94 | * |
95 | * @param b the boolean |
96 | * @return the value |
97 | */ |
98 | public static ValueBoolean get(boolean b) { |
99 | return (ValueBoolean) (b ? TRUE : FALSE); |
100 | } |
101 | |
102 | @Override |
103 | public int getDisplaySize() { |
104 | return DISPLAY_SIZE; |
105 | } |
106 | |
107 | @Override |
108 | public boolean equals(Object other) { |
109 | // there are only ever two instances, so the instance must match |
110 | return this == other; |
111 | } |
112 | |
113 | } |