001: /*
002: * $Id: CharacterType.java,v 1.14 2005/12/20 18:32:42 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.sql.Types;
050: import java.util.Comparator;
051: import java.util.HashMap;
052: import java.util.Map;
053:
054: import org.axiondb.AxionException;
055: import org.axiondb.DataType;
056:
057: /**
058: * A {@link DataType}representing a single char value.
059: *
060: * @version $Revision: 1.14 $ $Date: 2005/12/20 18:32:42 $
061: * @author Chuck Burdick
062: * @author Rodney Waldhoff
063: * @author Ahimanikya Satapathy
064: */
065: public class CharacterType extends BaseDataType implements
066: DataType.NonFixedPrecision {
067:
068: public CharacterType() {
069: this (1);
070: }
071:
072: public CharacterType(int size) {
073: _size = size;
074: }
075:
076: /** @return {@link Types#CHAR} */
077: public int getJdbcType() {
078: return Types.CHAR;
079: }
080:
081: public String getPreferredValueClassName() {
082: return "java.lang.String";
083: }
084:
085: public final int getPrecision() {
086: return _size;
087: }
088:
089: public int getColumnDisplaySize() {
090: return getPrecision();
091: }
092:
093: public boolean isCaseSensitive() {
094: return true;
095: }
096:
097: /**
098: * Returns <code>"character"</code>
099: *
100: * @return <code>"character"</code>
101: */
102: public String toString() {
103: return "character(" + _size + ")";
104: }
105:
106: public byte[] toByteArray(Object value) throws AxionException {
107: return toString(value).getBytes();
108: }
109:
110: /**
111: * Returns <code>true</code> iff <i>value </i> is <code>null</code>, a
112: * <tt>Character</tt>, or a single character <tt>String</tt>.
113: */
114: public boolean accepts(Object value) {
115: return true;
116: }
117:
118: public boolean supportsSuccessor() {
119: return true;
120: }
121:
122: public Object successor(Object value)
123: throws IllegalArgumentException {
124: return ((String) value) + "\0";
125: }
126:
127: /**
128: * Returns an <tt>String</tt> converted from the given <i>value </i>, or throws
129: * {@link IllegalArgumentException}if the given <i>value </i> isn't
130: * {@link #accepts acceptable}.
131: */
132: public Object convert(Object value) throws AxionException {
133: if (null == value) {
134: return null;
135: }
136: if (value instanceof String) {
137: return process((String) value);
138: }
139: if (value instanceof char[]) {
140: return process(new String((char[]) value));
141: }
142: if (value instanceof byte[]) {
143: return process(new String((byte[]) value));
144: }
145: if (value instanceof Clob) {
146: return process(new CLOBType().toString(value));
147: }
148: if (value instanceof Blob) {
149: try {
150: return process(new String(((Blob) value).getBytes(1,
151: (int) ((Blob) value).length())));
152: } catch (Exception e) {
153: throw new AxionException(e);
154: }
155: } else {
156: return process(value.toString());
157: }
158: }
159:
160: /**
161: * @see #write
162: */
163: public Object read(DataInput in) throws IOException {
164: String val = in.readUTF();
165: if ("null".equals(val)) {
166: if (!in.readBoolean()) {
167: return null;
168: }
169: }
170: return val;
171: }
172:
173: /**
174: * Writes the given <i>value </i> to the given {@link DataOutput}.<code>Null</code>
175: * values are written as <code>Character.MIN_VALUE</code>,<code>false</code>.
176: * <code>Character.MIN_VALUE</code> values are written as
177: * <code>Character.MIN_VALUE</code>,<code>true</code>. All other values are
178: * written directly.
179: *
180: * @param value the value to write, which must be {@link #accepts acceptable}
181: */
182: public void write(Object value, DataOutput out) throws IOException {
183: if (null == value) {
184: out.writeUTF("null");
185: out.writeBoolean(false);
186: } else if ("null".equals(value)) {
187: out.writeUTF("null");
188: out.writeBoolean(true);
189: } else {
190: out.writeUTF(value.toString());
191: }
192: }
193:
194: public DataType makeNewInstance() {
195: return new CharacterType();
196: }
197:
198: public int compare(Object a, Object b) {
199: return ((String) a).compareTo((String) b);
200: }
201:
202: public void setPrecision(int newSize) {
203: _size = newSize;
204: }
205:
206: public String getLiteralPrefix() {
207: return "'";
208: }
209:
210: public String getLiteralSuffix() {
211: return "'";
212: }
213:
214: public short getSearchableCode() {
215: return DatabaseMetaData.typeSearchable;
216: }
217:
218: protected Comparator getComparator() {
219: return this ;
220: }
221:
222: protected String process(String value) throws AxionException {
223: return padOutput(truncateIfLegal(value));
224: }
225:
226: protected final String truncateIfLegal(String source)
227: throws AxionException {
228: // A value longer than _size can be truncated if the excess characters are all
229: // whitespace; otherwise, SQLSTATE 22001 must be raised.
230: if (source.length() <= _size) {
231: return source;
232: } else {
233: String trimmedSource = rightTrim(source);
234: if (trimmedSource.length() <= _size) {
235: return trimmedSource;
236: }
237: throw new AxionException(22001);
238: }
239: }
240:
241: protected final String rightTrim(String source) {
242: // Per ANSI/ISO 2003 spec, do not trim whitespace from values whose lengths are less
243: // than the max size for the datatype.
244: if (source.length() <= _size) {
245: return source;
246: }
247:
248: int trimSourceLen = source.length();
249: int i, len = trimSourceLen - 1;
250: char trimChar = ' ';
251:
252: // ignore white space and all ASCII control characters as well
253: for (i = len; i > 0 && source.charAt(i) <= trimChar; i--)
254: ;
255: return (i == len) ? source : source.substring(0, i + 1);
256: }
257:
258: private final String padOutput(String source) {
259: if (source.length() == _size) {
260: return source;
261: }
262: return source.concat(getOrCreatePadTemplate(_size
263: - source.length()));
264: }
265:
266: private final String getOrCreatePadTemplate(int size) {
267: String padTemplate = (String) PAD_MAP.get(Integer
268: .toString(size));
269: if (padTemplate == null) {
270: StringBuffer buf = new StringBuffer();
271: for (int i = 0; i < _size; i++) {
272: buf.append(PAD_CHAR);
273: }
274: buf.setLength(size);
275:
276: padTemplate = buf.toString();
277: PAD_MAP.put(Integer.toString(size), padTemplate);
278: }
279: return padTemplate;
280: }
281:
282: private int _size = 1;
283:
284: private static final long serialVersionUID = 7980560473106205818L;
285: private transient static final char PAD_CHAR = ' ';
286: private transient static final Map PAD_MAP = new HashMap();
287: }
|