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