001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.completex.objective.components.persistency.core.impl;
010:
011: import com.completex.objective.components.persistency.ColumnType;
012: import com.completex.objective.components.persistency.core.TypeHandlerRegistry;
013: import com.completex.objective.components.persistency.type.BigDecimalTypeHandler;
014: import com.completex.objective.components.persistency.type.BinaryTypeHandler;
015: import com.completex.objective.components.persistency.type.BlobTypeHandler;
016: import com.completex.objective.components.persistency.type.BooleanTypeHandler;
017: import com.completex.objective.components.persistency.type.ByteArrayTypeHandler;
018: import com.completex.objective.components.persistency.type.ClobStringHandler;
019: import com.completex.objective.components.persistency.type.DateTypeHandler;
020: import com.completex.objective.components.persistency.type.DefaultTypeHandler;
021: import com.completex.objective.components.persistency.type.DetachedBlobTypeHandler;
022: import com.completex.objective.components.persistency.type.DetachedByteArrayInputStreamTypeHandler;
023: import com.completex.objective.components.persistency.type.DetachedClobTypeHandler;
024: import com.completex.objective.components.persistency.type.DoubleTypeHandler;
025: import com.completex.objective.components.persistency.type.LongTypeHandler;
026: import com.completex.objective.components.persistency.type.TypeHandler;
027:
028: import java.io.InputStream;
029: import java.util.Date;
030: import java.util.HashMap;
031: import java.util.Map;
032:
033: /**
034: * @author Gennady Krizhevsky
035: */
036: public class TypeHandlerRegistryImpl implements TypeHandlerRegistry {
037:
038: public static final DefaultTypeHandler DEFAULT_TYPE_HANDLER = new DefaultTypeHandler();
039:
040: private HashMap typeHandlers = new HashMap();
041: private HashMap bindTypeHandlers = new HashMap();
042:
043: {
044: //
045: // Register default read type handlers:
046: //
047: registerTypeHandler(ColumnType.BOOLEAN,
048: new BooleanTypeHandler());
049: registerTypeHandler(ColumnType.LONG, new LongTypeHandler());
050: registerTypeHandler(ColumnType.DOUBLE, new DoubleTypeHandler());
051: registerTypeHandler(ColumnType.NUMBER,
052: new BigDecimalTypeHandler());
053: registerTypeHandler(ColumnType.BIGDECIMAL,
054: new BigDecimalTypeHandler());
055: registerTypeHandler(ColumnType.CHAR, DEFAULT_TYPE_HANDLER);
056: registerTypeHandler(ColumnType.STRING, DEFAULT_TYPE_HANDLER);
057: registerTypeHandler(ColumnType.DATE, new DateTypeHandler());
058: registerTypeHandler(ColumnType.TIMESTAMP, new DateTypeHandler());
059: registerTypeHandler(ColumnType.BINARY, new BinaryTypeHandler());
060: registerTypeHandler(ColumnType.BYTE_ARRAY,
061: new ByteArrayTypeHandler());
062: registerTypeHandler(ColumnType.DETACHED_INPUT_STREAM,
063: new DetachedByteArrayInputStreamTypeHandler());
064: registerTypeHandler(ColumnType.BLOB, new BlobTypeHandler());
065: registerTypeHandler(ColumnType.DETACHED_BLOB,
066: new DetachedBlobTypeHandler());
067: registerTypeHandler(ColumnType.CLOB,
068: new DetachedClobTypeHandler());
069: registerTypeHandler(ColumnType.DETACHED_CLOB,
070: new DetachedClobTypeHandler());
071: registerTypeHandler(ColumnType.CLOB_STRING,
072: new ClobStringHandler());
073: registerTypeHandler(ColumnType.BOOLEAN_PRIMITIVE,
074: new BooleanTypeHandler());
075: registerTypeHandler(ColumnType.LONG_PRIMITIVE,
076: new LongTypeHandler());
077: registerTypeHandler(ColumnType.DOUBLE_PRIMITIVE,
078: new DoubleTypeHandler());
079:
080: //
081: // Register default bind type handlers:
082: //
083: registerBindTypeHandler(Boolean.class, new BooleanTypeHandler());
084: registerBindTypeHandler(boolean.class, new BooleanTypeHandler());
085: registerBindTypeHandler(Date.class, new DateTypeHandler());
086: registerBindTypeHandler(InputStream.class,
087: new BinaryTypeHandler());
088: }
089:
090: /**
091: * Registers default type handler for read and bind for given ColumnType.
092: * ColumnType.getName() is used as a key
093: *
094: * @param columnType ColumnType
095: * @param typeHandler TypeHandler
096: */
097: public void registerTypeHandler(ColumnType columnType,
098: TypeHandler typeHandler) {
099: typeHandlers.put(columnType, typeHandler);
100: }
101:
102: /**
103: * Returns true if typeHandler is found by ColumnType.getName()
104: *
105: * @param columnType
106: * @return true if typeHandler is found by ColumnType.getName()
107: */
108: public boolean isRegisteredTypeHandler(ColumnType columnType) {
109: return typeHandlers.containsKey(columnType);
110: }
111:
112: /**
113: * Registers TypeHandler only for bind. It will be used only if default
114: * type handler for read and bind is not found or the column type is not specified
115: *
116: * @param clazz Class
117: * @param typeHandler TypeHandler
118: */
119: public void registerBindTypeHandler(Class clazz,
120: TypeHandler typeHandler) {
121: bindTypeHandlers.put(clazz, typeHandler);
122: }
123:
124: /**
125: * Returns true if typeHandler is found by ColumnType.getName()
126: *
127: * @param clazz Class that has to be handled
128: * @return true if typeHandler is found by clazz
129: */
130: public boolean isRegisteredBindTypeHandler(Class clazz) {
131: return bindTypeHandlers.containsKey(clazz);
132: }
133:
134: /**
135: * Returnss TypeHandler only for bind by given class. It will be used only if default
136: * type handler for read and bind is not found or the column type is not specified
137: *
138: * @param clazz Class
139: * @return TypeHandler only for bind. It will be used only if default
140: * type handler for read and bind is not found or the column type is not specified
141: */
142: public TypeHandler getBindTypeHandler(Class clazz) {
143: TypeHandler typeHandler = (TypeHandler) bindTypeHandlers
144: .get(clazz);
145: if (typeHandler == null) {
146: typeHandler = DEFAULT_TYPE_HANDLER;
147: }
148: return typeHandler;
149: }
150:
151: /**
152: * Returns default type handler for read and bind for given ColumnType
153: *
154: * @param columnType
155: * @return default type handler for read and bind for given ColumnType
156: */
157: public TypeHandler getTypeHandler(ColumnType columnType) {
158: return getTypeHandler(columnType, typeHandlers);
159: }
160:
161: static TypeHandler getTypeHandler(ColumnType columnType,
162: Map typeHandlers) {
163: if (columnType != null) {
164: if (columnType.getCustomTypeHandler() != null) {
165: return columnType.getCustomTypeHandler();
166: } else {
167: TypeHandler typeHandler = (TypeHandler) typeHandlers
168: .get(columnType);
169: if (typeHandler == null) {
170: typeHandler = DEFAULT_TYPE_HANDLER;
171: }
172: return typeHandler;
173: }
174: } else {
175: return DEFAULT_TYPE_HANDLER;
176: }
177: }
178:
179: }
|