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