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