001: /*
002: * $Id: ByteType.java,v 1.10 2005/05/02 22:29:40 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2005 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.math.BigDecimal;
047: import java.sql.Types;
048: import java.util.Comparator;
049:
050: import org.axiondb.AxionException;
051: import org.axiondb.DataType;
052:
053: /**
054: * A {@link DataType}representing a byte value.
055: *
056: * @version $Revision: 1.10 $ $Date: 2005/05/02 22:29:40 $
057: * @author Rodney Waldhoff
058: * @author Ahimanikya Satapathy
059: * @author Jonathan Giron
060: */
061: public class ByteType extends BaseNumberDataType {
062:
063: public ByteType() {
064: }
065:
066: /** @return {@link Types#TINYINT} */
067: public int getJdbcType() {
068: return Types.TINYINT;
069: }
070:
071: public int getColumnDisplaySize() {
072: return String.valueOf(Byte.MIN_VALUE).length();
073: }
074:
075: public String getPreferredValueClassName() {
076: return "java.lang.Byte";
077: }
078:
079: public int getPrecision() {
080: return String.valueOf(Byte.MAX_VALUE).length();
081: }
082:
083: public String toString() {
084: return "byte";
085: }
086:
087: /**
088: * Returns an <tt>Byte</tt> converted from the given <i>value </i>, or throws
089: * {@link IllegalArgumentException}if the given <i>value </i> isn't
090: * {@link #accepts acceptable}.
091: */
092: public Object convert(Object value) throws AxionException {
093: short rawValue = (short) 0;
094:
095: if (value instanceof Byte) {
096: return value;
097: } else if (value instanceof Number) {
098: rawValue = ((Number) value).shortValue();
099: } else if (value instanceof String) {
100: try {
101: rawValue = new BigDecimal(value.toString().trim())
102: .shortValue();
103: } catch (NumberFormatException e) {
104: throw new AxionException(22018);
105: }
106: } else {
107: return super .convert(value);
108: }
109:
110: assertValueInRange(rawValue);
111: return new Byte((byte) rawValue);
112: }
113:
114: public Object successor(Object value)
115: throws IllegalArgumentException {
116: byte v = ((Byte) value).byteValue();
117: if (v == Byte.MAX_VALUE) {
118: return value;
119: }
120: return new Byte(++v);
121: }
122:
123: /**
124: * @see #write
125: */
126: public Object read(DataInput in) throws IOException {
127: byte value = in.readByte();
128: if (Byte.MIN_VALUE == value) {
129: if (!in.readBoolean()) {
130: return null;
131: }
132: }
133: return new Byte(value);
134: }
135:
136: /**
137: * Writes the given <i>value </i> to the given <code>DataOutput</code>.
138: * <code>Null</code> values are written as <code>Byte.MIN_VALUE</code>,
139: * <code>false</code>.<code>Byte.MIN_VALUE</code> values are written as
140: * <code>Byte.MIN_VALUE</code>,<code>true</code>. All other values are written
141: * directly.
142: *
143: * @param value the value to write, which must be {@link #accepts acceptable}
144: */
145: public void write(Object value, DataOutput out) throws IOException {
146: if (null == value) {
147: out.writeByte(Byte.MIN_VALUE);
148: out.writeBoolean(false);
149: } else {
150: try {
151: byte val = ((Byte) (convert(value))).byteValue();
152: out.writeByte(val);
153: if (Byte.MIN_VALUE == val) {
154: out.writeBoolean(true);
155: }
156: } catch (AxionException e) {
157: throw new IOException(e.getMessage());
158: }
159: }
160: }
161:
162: public DataType makeNewInstance() {
163: return new ByteType();
164: }
165:
166: public int compare(Object a, Object b) {
167: byte pa = ((Number) a).byteValue();
168: byte pb = ((Number) b).byteValue();
169: return (pa - pb);
170: }
171:
172: protected Comparator getComparator() {
173: return this ;
174: }
175:
176: private void assertValueInRange(short rawValue)
177: throws AxionException {
178: if (rawValue < Byte.MIN_VALUE || rawValue > Byte.MAX_VALUE) {
179: throw new AxionException(22003);
180: }
181: }
182:
183: private static final long serialVersionUID = -8198635981830444420L;
184: }
|