001: /*
002: * $Id: FloatType.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 a single-precision floating-point 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 FloatType extends BaseNumberDataType {
061:
062: public FloatType() {
063: }
064:
065: public int getJdbcType() {
066: return java.sql.Types.FLOAT;
067: }
068:
069: public String getPreferredValueClassName() {
070: return "java.lang.Float";
071: }
072:
073: public int getPrecision() {
074: return String.valueOf(Float.MAX_VALUE).length();
075: }
076:
077: public int getScale() {
078: // NOTE: Per ANSI SQL-2003 standard approximate datatypes like float do not have
079: // a scale value - only a precision value.
080: return 0;
081: }
082:
083: public int getColumnDisplaySize() {
084: return String.valueOf(Float.MAX_VALUE).length();
085: }
086:
087: /**
088: * Returns <code>"float"</code>
089: *
090: * @return <code>"float"</code>
091: */
092: public String toString() {
093: return "float";
094: }
095:
096: /**
097: * Returns a <tt>Float</tt> converted from the given <i>value </i>, or throws
098: * {@link IllegalArgumentException}if the given <i>value </i> isn't
099: * {@link #accepts acceptable}.
100: */
101: public Object convert(Object value) throws AxionException {
102: double doubleValue = 0.0;
103: if (value instanceof Float) {
104: float floatValue = ((Float) value).floatValue();
105: if (Float.isInfinite(floatValue) || Float.isNaN(floatValue)) {
106: throw new AxionException(22003);
107: }
108: return value;
109: } else if (value instanceof Number) {
110: doubleValue = ((Number) value).doubleValue();
111: } else if (value instanceof String) {
112: try {
113: doubleValue = new BigDecimal(value.toString().trim())
114: .doubleValue();
115: } catch (NumberFormatException e) {
116: throw new AxionException(22018);
117: }
118: } else {
119: return super .convert(value);
120: }
121:
122: assertValueInRange(doubleValue);
123: return new Float(doubleValue);
124: }
125:
126: /**
127: * @see #write
128: */
129: public Object read(DataInput in) throws IOException {
130: float value = in.readFloat();
131: if (Float.MIN_VALUE == value) {
132: if (!in.readBoolean()) {
133: return null;
134: }
135: }
136: return new Float(value);
137: }
138:
139: /** <code>false</code> */
140: public boolean supportsSuccessor() {
141: return true;
142: }
143:
144: public Object successor(Object value)
145: throws IllegalArgumentException {
146: float v = ((Float) value).floatValue();
147: if (v == Float.MAX_VALUE) {
148: return value;
149: }
150:
151: int ieee754Bits = Float.floatToIntBits(v);
152: int sign = (ieee754Bits & SIGN_MASK);
153: boolean isNegative = (SIGN_NEGATIVE == sign);
154:
155: int accumExpMantissa = (ieee754Bits & EXP_MANTISSA_MASK);
156: if (isNegative) {
157: if (0x0L != accumExpMantissa) {
158: accumExpMantissa -= 1;
159: } else {
160: sign = 0x0;
161: accumExpMantissa = 0x0;
162: }
163: } else {
164: if (0x0L != accumExpMantissa) {
165: accumExpMantissa += 1;
166: } else {
167: accumExpMantissa = 1;
168: }
169: }
170:
171: return new Float(Float.intBitsToFloat(sign | accumExpMantissa));
172: }
173:
174: public void write(Object value, DataOutput out) throws IOException {
175: if (null == value) {
176: out.writeFloat(Float.MIN_VALUE);
177: out.writeBoolean(false);
178: } else {
179: try {
180: float val = ((Float) (convert(value))).floatValue();
181: out.writeFloat(val);
182: if (Float.MIN_VALUE == val) {
183: out.writeBoolean(true);
184: }
185: } catch (AxionException e) {
186: throw new IOException(e.getMessage());
187: }
188:
189: }
190: }
191:
192: public DataType makeNewInstance() {
193: return new FloatType();
194: }
195:
196: public int compare(Object a, Object b) {
197: float pa = ((Number) a).floatValue();
198: float pb = ((Number) b).floatValue();
199: return (pa < pb) ? -1 : ((pa == pb) ? 0 : 1);
200: }
201:
202: protected Comparator getComparator() {
203: return this ;
204: }
205:
206: private void assertValueInRange(double doubleValue)
207: throws AxionException {
208: if (doubleValue > Float.MAX_VALUE
209: || doubleValue < -Float.MAX_VALUE) {
210: throw new AxionException(22003);
211: }
212: }
213:
214: private static final long serialVersionUID = -831981915887585231L;
215:
216: private static final int SIGN_MASK = 0x80000000;
217: private static final int SIGN_NEGATIVE = SIGN_MASK;
218: private static final int EXP_MANTISSA_MASK = 0x7fffffff;
219: }
|