001: /*
002: * $Id: AnyType.java,v 1.12 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.sql.Blob;
047: import java.sql.Clob;
048: import java.sql.Date;
049: import java.sql.Time;
050: import java.sql.Timestamp;
051:
052: import org.axiondb.AxionException;
053: import org.axiondb.DataType;
054:
055: /**
056: * Generic implemention of {@link DataType}, for use by {@link org.axiondb.BindVariable}.
057: *
058: * @version $Revision: 1.12 $ $Date: 2005/05/02 22:29:40 $
059: * @author Rodney Waldhoff
060: */
061: public class AnyType extends BaseDataType {
062: public DataType makeNewInstance() {
063: return new AnyType();
064: }
065:
066: public boolean accepts(Object value) {
067: return true;
068: }
069:
070: public Object convert(Object value) {
071: return value;
072: }
073:
074: public Object read(DataInput in) throws IOException {
075: throw new IOException("This type is not meant to be saved.");
076: }
077:
078: public void write(Object value, DataOutput out) throws IOException {
079: throw new IOException("This type is not meant to be saved.");
080: }
081:
082: public int getColumnDisplaySize() {
083: return 0;
084: }
085:
086: public int getJdbcType() {
087: return java.sql.Types.OTHER;
088: }
089:
090: public String getPreferredValueClassName() {
091: return "java.lang.String";
092: }
093:
094: public boolean supportsSuccessor() {
095: return false;
096: }
097:
098: public Object successor(Object value)
099: throws IllegalArgumentException {
100: throw new UnsupportedOperationException();
101: }
102:
103: protected Number toNumber(Object value) throws AxionException {
104: if (null == value) {
105: return null;
106: } else if (value instanceof Number) {
107: return (Number) value;
108: } else {
109: return new Integer(toInt(value));
110: }
111: }
112:
113: public boolean toBoolean(Object value) throws AxionException {
114: if (value instanceof Boolean) {
115: return ((Boolean) value).booleanValue();
116: } else if ("true".equals(toString(value))) {
117: return true;
118: } else if ("false".equals(toString(value))) {
119: return false;
120: } else {
121: throw new AxionException(
122: "Can't convert "
123: + value
124: + " to a boolean value, expected 'true' or 'false'.",
125: 22018);
126: }
127: }
128:
129: public byte toByte(Object value) throws AxionException {
130: if (value instanceof Number) {
131: return ((Number) value).byteValue();
132: }
133: try {
134: return Byte.parseByte(String.valueOf(value));
135: } catch (NumberFormatException e) {
136: throw new AxionException("Can't convert " + value
137: + " to byte.", 22018);
138: }
139: }
140:
141: public byte[] toByteArray(Object value) throws AxionException {
142: if (value instanceof byte[]) {
143: return ((byte[]) value);
144: }
145:
146: try {
147: return toString(value).getBytes();
148: } catch (RuntimeException e) {
149: throw new AxionException("Can't convert " + value
150: + " to byte array.", 22018);
151: }
152: }
153:
154: public double toDouble(Object value) throws AxionException {
155: if (value instanceof Number) {
156: return ((Number) value).doubleValue();
157: }
158:
159: try {
160: return Double.parseDouble(String.valueOf(value));
161: } catch (NumberFormatException e) {
162: throw new AxionException("Can't convert " + value
163: + " to double.", 22018);
164: }
165: }
166:
167: public float toFloat(Object value) throws AxionException {
168: if (value instanceof Number) {
169: return ((Number) value).floatValue();
170: }
171:
172: try {
173: return Float.parseFloat(String.valueOf(value));
174: } catch (NumberFormatException e) {
175: throw new AxionException("Can't convert " + value
176: + " to float.", 22018);
177: }
178: }
179:
180: public int toInt(Object value) throws AxionException {
181: if (value instanceof Number) {
182: return ((Number) value).intValue();
183: }
184:
185: try {
186: return Integer.parseInt(String.valueOf(value));
187: } catch (NumberFormatException e) {
188: throw new AxionException("Can't convert " + value
189: + " to int.", 22018);
190: }
191: }
192:
193: public long toLong(Object value) throws AxionException {
194: if (value instanceof Number) {
195: return ((Number) value).longValue();
196: } else if (value instanceof Date) {
197: return ((Date) value).getTime();
198: } else if (value instanceof Time) {
199: return ((Time) value).getTime();
200: } else if (value instanceof Timestamp) {
201: return ((Timestamp) value).getTime();
202: } else {
203: try {
204: return Long.parseLong(String.valueOf(value));
205: } catch (NumberFormatException e) {
206: throw new AxionException("Can't convert " + value
207: + " to long.", 22018);
208: }
209: }
210: }
211:
212: public short toShort(Object value) throws AxionException {
213: if (value instanceof Number) {
214: return ((Number) value).shortValue();
215: }
216:
217: try {
218: return Short.parseShort(String.valueOf(value));
219: } catch (NumberFormatException e) {
220: throw new AxionException("Can't convert " + value
221: + " to short.", 22018);
222: }
223: }
224:
225: public String toString(Object value) throws AxionException {
226: if (null == value) {
227: return null;
228: } else if (value instanceof String) {
229: return (String) value;
230: } else {
231: return String.valueOf(value);
232: }
233: }
234:
235: public Date toDate(Object value) throws AxionException {
236: if (null == value) {
237: return null;
238: } else if (value instanceof Date) {
239: return (Date) value;
240: } else if (value instanceof Number) {
241: return new Date(((Number) value).longValue());
242: } else {
243: throw new AxionException("Can't convert " + value
244: + " to Time.", 22018); // should be smarter
245: }
246: }
247:
248: public Time toTime(Object value) throws AxionException {
249: if (null == value) {
250: return null;
251: } else if (value instanceof Time) {
252: return (Time) value;
253: } else if (value instanceof Number) {
254: return new Time(((Number) value).longValue());
255: } else {
256: throw new AxionException("Can't convert " + value
257: + " to Time.", 22018); // should be smarter
258: }
259: }
260:
261: public Timestamp toTimestamp(Object value) throws AxionException {
262: if (null == value) {
263: return null;
264: } else if (value instanceof Timestamp) {
265: return (Timestamp) value;
266: } else if (value instanceof Number) {
267: return new Timestamp(((Number) value).longValue());
268: } else {
269: throw new AxionException("Can't convert " + value
270: + " to Timestamp.", 22018); // should be smarter
271: }
272: }
273:
274: public Clob toClob(Object value) throws AxionException {
275: if (null == value) {
276: return null;
277: } else if (value instanceof Clob) {
278: return (Clob) value;
279: } else {
280: return new StringClob(toString(value));
281: }
282: }
283:
284: public Blob toBlob(Object value) throws AxionException {
285: if (null == value) {
286: return null;
287: } else if (value instanceof Blob) {
288: return (Blob) value;
289: } else {
290: throw new AxionException("Can't convert " + value
291: + " to Blob.", 22018);
292: }
293: }
294:
295: public static final AnyType INSTANCE = new AnyType();
296: }
|