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