001: /*
002: * $Id: BooleanType.java,v 1.15 2005/10/13 02:25:32 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2004 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.types;
042:
043: import java.io.DataInput;
044: import java.io.DataOutput;
045: import java.io.IOException;
046: import java.sql.Types;
047: import java.util.Comparator;
048:
049: import org.axiondb.AxionException;
050: import org.axiondb.DataType;
051:
052: /**
053: * A {@link DataType}representing a Boolean value.
054: *
055: * @version $Revision: 1.15 $ $Date: 2005/10/13 02:25:32 $
056: * @author Rodney Waldhoff
057: * @author Ahimanikya Satapathy
058: */
059: public class BooleanType extends BaseDataType {
060:
061: public BooleanType() {
062: }
063:
064: /** @return {@link Types#BOOLEAN} */
065: public int getJdbcType() {
066: return Types.BOOLEAN;
067: // return Types.BIT;
068: }
069:
070: public String getPreferredValueClassName() {
071: return "java.lang.Boolean";
072: }
073:
074: public int getPrecision() {
075: return 1;
076: }
077:
078: public int getColumnDisplaySize() {
079: return 5;
080: }
081:
082: public Comparator getComparator() {
083: return COMPARATOR_INSTANCE;
084: }
085:
086: /**
087: * Returns <code>"boolean"</code>
088: *
089: * @return <code>"boolean"</code>
090: */
091: public String toString() {
092: return "boolean";
093: }
094:
095: public boolean toBoolean(Object value) throws AxionException {
096: try {
097: return ((Boolean) (convert(value))).booleanValue();
098: } catch (ClassCastException e) {
099: throw new AxionException("Can't convert " + value
100: + " to boolean.", e);
101: } catch (NullPointerException e) {
102: throw new AxionException("Can't convert null to boolean.",
103: e);
104: } catch (IllegalArgumentException e) {
105: throw new AxionException("Can't convert " + value
106: + " to boolean.", e);
107: }
108: }
109:
110: /**
111: * Returns <code>true</code> iff <i>value </i> is <code>null</code>, or a
112: * <code>Boolean</code>.
113: */
114: public boolean accepts(Object value) {
115: if (null == value) {
116: return true;
117: } else if (value instanceof Boolean) {
118: return true;
119: } else if (value instanceof String) {
120: return true;
121: } else {
122: return false;
123: }
124: }
125:
126: /**
127: * Returns a <code>Boolean</code> converted from the given <i>value </i>, or throws
128: * {@link IllegalArgumentException}if the given <i>value </i> isn't
129: * {@link #accepts acceptable}.
130: */
131: public Object convert(Object value) throws IllegalArgumentException {
132: if (null == value) {
133: return null;
134: } else if (value instanceof Boolean) {
135: return value;
136: } else if (value instanceof String) {
137: return Boolean.valueOf(value.toString());
138: } else {
139: throw new IllegalArgumentException("Can't convert "
140: + value.getClass().getName() + " " + value
141: + " into a Boolean.");
142: }
143: }
144:
145: /**
146: * @see #write
147: */
148: public Object read(DataInput in) throws IOException {
149: byte value = in.readByte();
150: switch (value) {
151: case -1:
152: return null;
153: case 0:
154: return Boolean.FALSE;
155: case 1:
156: return Boolean.TRUE;
157: default:
158: throw new IOException("Expected -1, 0, or 1, found "
159: + value);
160: }
161: }
162:
163: /**
164: * Writes the given <i>value </i> to the given <code>DataOutput</code>.
165: * <code>Null</code> values are written as <code>(byte)-1</code>,
166: * <code>false</code> values are written as <code>(byte)0</code>,
167: * <code>true</code> values are written as <code>(byte)1</code>.
168: *
169: * @param value the value to write, which must be {@link #accepts acceptable}
170: */
171: public void write(Object value, DataOutput out) throws IOException {
172: if (null == value) {
173: out.writeByte(-1);
174: } else {
175: boolean val = ((Boolean) (convert(value))).booleanValue();
176: if (val) {
177: out.writeByte(1);
178: } else {
179: out.writeByte(0);
180: }
181: }
182: }
183:
184: public DataType makeNewInstance() {
185: return new BooleanType();
186: }
187:
188: /**
189: * A {@link Comparator}for Boolean values.
190: */
191: static class BooleanComparator implements Comparator {
192:
193: public int compare(Object left, Object right) {
194: if (null == left) {
195: return (null == right) ? 0 : 1;
196: } else if (null == right) {
197: return -1;
198: }
199:
200: return compare(((Boolean) left).booleanValue(),
201: ((Boolean) right).booleanValue());
202: }
203:
204: private final int compare(boolean left, boolean right) {
205: // sorts false before true
206: return (left ? (right ? 0 : 1) : (right ? -1 : 0));
207: }
208:
209: }
210:
211: private static final BooleanComparator COMPARATOR_INSTANCE = new BooleanComparator();
212: private static final long serialVersionUID = 8587725427253945440L;
213: }
|