001: /*
002: * $Id: UnsignedByteType.java,v 1.10 2005/05/10 00:15:20 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:
048: import org.axiondb.AxionException;
049: import org.axiondb.DataType;
050:
051: /**
052: * A {@link DataType}representing an unsigned byte value.
053: *
054: * @version $Revision: 1.10 $ $Date: 2005/05/10 00:15:20 $
055: * @author Rodney Waldhoff
056: * @author Chuck Burdick
057: * @author Jonathan Giron
058: */
059: public class UnsignedByteType extends BaseNumberDataType {
060:
061: static final short MIN_VALUE = (short) 0;
062: static final short MAX_VALUE = (short) 255;
063:
064: public UnsignedByteType() {
065: }
066:
067: public int getJdbcType() {
068: return java.sql.Types.TINYINT;
069: }
070:
071: public String getPreferredValueClassName() {
072: return "java.lang.Short";
073: }
074:
075: public int getPrecision() {
076: return String.valueOf(MAX_VALUE).length();
077: }
078:
079: public int getColumnDisplaySize() {
080: return String.valueOf(MAX_VALUE).length();
081: }
082:
083: public String toString() {
084: return "unsignedbyte";
085: }
086:
087: /**
088: * Returns <code>true</code> iff <i>value</i> is <code>null</code>,
089: * a <tt>Number</tt>, within the unsigned byte range (0 to 255).
090: */
091: public boolean accepts(Object value) {
092: if (value instanceof Number) {
093: short s = ((Number) value).shortValue();
094: try {
095: assertValidUnsignedByte(s);
096: } catch (AxionException e) {
097: return false;
098: }
099: return true;
100: } else {
101: return super .accepts(value);
102: }
103: }
104:
105: /**
106: * Returns an <tt>Short</tt> converted from the given <i>value </i>, or throws
107: * {@link IllegalArgumentException}if the given <i>value </i> isn't
108: * {@link #accepts acceptable}.
109: */
110: public Object convert(Object value) throws AxionException {
111: if (value instanceof Number) {
112: short s = ((Number) value).shortValue();
113: assertValidUnsignedByte(s);
114: return new Short(s);
115: } else if (value instanceof String) {
116: try {
117: Short sval = new Short(new BigDecimal(value.toString()
118: .trim()).shortValue());
119: assertValidUnsignedByte(sval.shortValue());
120: return sval;
121: } catch (NumberFormatException e) {
122: throw new AxionException(22018);
123: }
124: } else {
125: return super .convert(value);
126: }
127: }
128:
129: public Object successor(Object value)
130: throws IllegalArgumentException {
131: short v = ((Short) value).shortValue();
132:
133: try {
134: assertValidUnsignedByte(v);
135: } catch (AxionException e) {
136: throw new IllegalArgumentException(e.getMessage());
137: }
138:
139: if (v == MAX_VALUE) {
140: return value;
141: }
142: return new Short(++v);
143: }
144:
145: /**
146: * @see #write
147: */
148: public Object read(DataInput in) throws IOException {
149: short value = toShort(in.readByte());
150: if (MIN_VALUE == value) {
151: if (!in.readBoolean()) {
152: return null;
153: }
154: }
155: return new Short(value);
156: }
157:
158: /**
159: * Writes the given <i>value </i> to the given <code>DataOutput</code>.
160: * <code>Null</code> values are written as <code>MIN_VALUE</code>,
161: * <code>false</code>.<code>MIN_VALUE</code> values are written as
162: * <code>MIN_VALUE</code>,<code>true</code>. All other values are written
163: * directly.
164: *
165: * @param value the value to write, which must be {@link #accepts acceptable}
166: */
167: public void write(Object value, DataOutput out) throws IOException {
168: if (null == value) {
169: out.writeByte(fromShort(MIN_VALUE));
170: out.writeBoolean(false);
171: } else {
172: short val;
173: try {
174: val = ((Short) (convert(value))).shortValue();
175: out.writeByte(fromShort(val));
176: if (MIN_VALUE == val) {
177: out.writeBoolean(true);
178: }
179: } catch (AxionException e) {
180: throw new IOException(e.getMessage());
181: }
182: }
183: }
184:
185: public DataType makeNewInstance() {
186: return new UnsignedByteType();
187: }
188:
189: public boolean isUnsigned() {
190: return true;
191: }
192:
193: private final short toShort(byte value) {
194: return (short) (value & MAX_VALUE);
195: }
196:
197: private final byte fromShort(short value) {
198: return (byte) (value & MAX_VALUE);
199: }
200:
201: private final void assertValidUnsignedByte(short value)
202: throws AxionException {
203: if (value > MAX_VALUE || value < MIN_VALUE) {
204: throw new AxionException(22003);
205: }
206: }
207:
208: private static final long serialVersionUID = 2501005806335170795L;
209: }
|