001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.internal;
022:
023: import com.db4o.internal.handlers.*;
024: import com.db4o.reflect.*;
025:
026: /**
027: * @exclude
028: */
029: public class Handlers4 {
030:
031: public static final int INT_ID = 1;
032:
033: public static final int LONG_ID = 2;
034:
035: public static final int FLOAT_ID = 3;
036:
037: public static final int BOOLEAN_ID = 4;
038:
039: public static final int DOUBLE_ID = 5;
040:
041: public static final int BYTE_ID = 6;
042:
043: public static final int CHAR_ID = 7;
044:
045: public static final int SHORT_ID = 8;
046:
047: public static final int STRING_ID = 9;
048:
049: public static final int DATE_ID = 10;
050:
051: public static final int UNTYPED_ID = 11;
052:
053: public static final int ANY_ARRAY_ID = 12;
054:
055: public static final int ANY_ARRAY_N_ID = 13;
056:
057: public static boolean handlerCanHold(TypeHandler4 handler,
058: ReflectClass claxx) {
059: TypeHandler4 baseTypeHandler = baseTypeHandler(handler);
060: if (handlesSimple(baseTypeHandler)) {
061: return claxx.equals(((BuiltinTypeHandler) baseTypeHandler)
062: .classReflector());
063: }
064:
065: if (baseTypeHandler instanceof UntypedFieldHandler) {
066: return true;
067: }
068:
069: ClassMetadata classMetadata = (ClassMetadata) baseTypeHandler;
070: ReflectClass classReflector = classMetadata.classReflector();
071: if (classReflector.isCollection()) {
072: return true;
073: }
074: return classReflector.isAssignableFrom(claxx);
075: }
076:
077: public static boolean handlesSimple(TypeHandler4 handler) {
078: TypeHandler4 baseTypeHandler = baseTypeHandler(handler);
079: return (baseTypeHandler instanceof PrimitiveHandler)
080: || (baseTypeHandler instanceof StringHandler);
081: }
082:
083: public static boolean handlesClass(TypeHandler4 handler) {
084: return baseTypeHandler(handler) instanceof ClassMetadata;
085: }
086:
087: public static ReflectClass primitiveClassReflector(
088: TypeHandler4 handler) {
089: TypeHandler4 baseTypeHandler = baseTypeHandler(handler);
090: if (baseTypeHandler instanceof PrimitiveHandler) {
091: return ((PrimitiveHandler) baseTypeHandler)
092: .primitiveClassReflector();
093: }
094: return null;
095: }
096:
097: public static TypeHandler4 baseTypeHandler(TypeHandler4 handler) {
098: if (handler instanceof ArrayHandler) {
099: return ((ArrayHandler) handler)._handler;
100: }
101: if (handler instanceof PrimitiveFieldHandler) {
102: return ((PrimitiveFieldHandler) handler).typeHandler();
103: }
104: return handler;
105: }
106:
107: public static ReflectClass baseType(ReflectClass clazz) {
108: if (clazz.isArray()) {
109: return baseType(clazz.getComponentType());
110: }
111: return clazz;
112: }
113: }
|