001: /*
002: * $Id: StringType.java,v 1.14 2005/11/24 20:47:31 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.DatabaseMetaData;
049: import java.util.Comparator;
050:
051: import org.axiondb.AxionException;
052: import org.axiondb.DataType;
053:
054: /**
055: * A {@link DataType} representing a {@link String} value.
056: *
057: * @version $Revision: 1.14 $ $Date: 2005/11/24 20:47:31 $
058: * @author Chuck Burdick
059: * @author Rodney Waldhoff
060: * @author Ahimanikya Satapathy
061: */
062: public class StringType extends BaseDataType {
063:
064: public StringType() {
065: }
066:
067: public int getJdbcType() {
068: return java.sql.Types.VARCHAR;
069: }
070:
071: public String getPreferredValueClassName() {
072: return "java.lang.String";
073: }
074:
075: public int getColumnDisplaySize() {
076: return 32766;
077: }
078:
079: public int getPrecision() {
080: return Integer.MAX_VALUE;
081: }
082:
083: public boolean isCaseSensitive() {
084: return true;
085: }
086:
087: /**
088: * Returns <code>"string"</code>
089: *
090: * @return <code>"string"</code>
091: */
092: public String toString() {
093: return "string";
094: }
095:
096: public byte[] toByteArray(Object value) throws AxionException {
097: return toString(value).getBytes();
098: }
099:
100: /**
101: * Returns <code>true</code> iff <i>value </i> is <code>null</code> or a
102: * {@link String String}.
103: */
104: public boolean accepts(Object value) {
105: return true;
106: }
107:
108: /**
109: * Returns a {@link String}converted from the given <i>value </i>, or throws
110: * {@link IllegalArgumentException}if the given <i>value </i> isn't
111: * {@link #accepts acceptable}.
112: */
113: public Object convert(Object value) throws AxionException {
114: if (null == value) {
115: return null;
116: }
117: if (value instanceof Clob) {
118: return new CLOBType().toString(value);
119: }
120: if (value instanceof Blob) {
121: try {
122: return new String(((Blob) value).getBytes(1,
123: (int) ((Blob) value).length()));
124: } catch (Exception e) {
125: throw new AxionException(e);
126: }
127: }
128: if (value instanceof byte[]) {
129: return new String((byte[]) value);
130: } else {
131: return value.toString();
132: }
133: }
134:
135: public boolean supportsSuccessor() {
136: return true;
137: }
138:
139: public Object successor(Object value)
140: throws IllegalArgumentException {
141: String v = ((String) value);
142: return v + "\0";
143: }
144:
145: /**
146: * @see #write
147: */
148: public Object read(DataInput in) throws IOException {
149: String val = in.readUTF();
150: if ("null".equals(val)) {
151: if (!in.readBoolean()) {
152: return null;
153: }
154: }
155: return val;
156: }
157:
158: /**
159: * Writes the given <i>value </i> to the given <code>DataOutput</code>.
160: * <code>Null</code> values are written as <code>"null", false</code>, the
161: * non-null String <code>"null"</code> is written as <code>"null", true</code>,
162: * all other values are written directly.
163: *
164: * @param value the value to write, which must be {@link #accepts acceptable}
165: */
166: public void write(Object value, DataOutput out) throws IOException {
167: if (null == value) {
168: out.writeUTF("null");
169: out.writeBoolean(false);
170: } else if ("null".equals(value)) {
171: out.writeUTF("null");
172: out.writeBoolean(true);
173: } else {
174: out.writeUTF(value.toString());
175: }
176: }
177:
178: public DataType makeNewInstance() {
179: return new StringType();
180: }
181:
182: public String getLiteralPrefix() {
183: return "'";
184: }
185:
186: public String getLiteralSuffix() {
187: return "'";
188: }
189:
190: public short getSearchableCode() {
191: return DatabaseMetaData.typeSearchable;
192: }
193:
194: public int compare(Object a, Object b) {
195: return ((String) a).compareTo((String) b);
196: }
197:
198: protected Comparator getComparator() {
199: return this ;
200: }
201:
202: public static final StringType instance() {
203: return INSTANCE;
204: }
205:
206: private static final StringType INSTANCE = new StringType();
207:
208: private static final long serialVersionUID = -2663968706274343425L;
209: }
|