001/* 002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.core.schema.types; 023 024import java.io.InputStream; 025import java.util.Calendar; 026import java.util.Date; 027import java.util.HashMap; 028import java.util.List; 029import java.util.Map; 030 031import org.nuxeo.ecm.core.schema.types.primitives.BinaryType; 032import org.nuxeo.ecm.core.schema.types.primitives.BooleanType; 033import org.nuxeo.ecm.core.schema.types.primitives.DateType; 034import org.nuxeo.ecm.core.schema.types.primitives.DoubleType; 035import org.nuxeo.ecm.core.schema.types.primitives.IntegerType; 036import org.nuxeo.ecm.core.schema.types.primitives.LongType; 037import org.nuxeo.ecm.core.schema.types.primitives.StringType; 038 039/** 040 * Maps ECM types to Java classes. 041 * 042 * @author bstefanescu 043 */ 044public final class JavaTypes { 045 046 private static final Map<Class<?>, Type> class2Types = new HashMap<Class<?>, Type>(); 047 048 private static final Map<Type, Class<?>> types2Class = new HashMap<Type, Class<?>>(); 049 050 // Utility class. 051 private JavaTypes() { 052 } 053 054 public static boolean isList(Object object) { 055 return object instanceof List; 056 } 057 058 public static boolean isComplex(Object object) { 059 return object instanceof Map; 060 } 061 062 public static Type getType(Class<?> klass) { 063 return class2Types.get(klass); 064 } 065 066 public static Class<?> getClass(Type type) { 067 if (type.isSimpleType() && !((SimpleType) type).isPrimitive()) { 068 return getClass(((SimpleType) type).getPrimitiveType()); 069 } 070 return types2Class.get(type); 071 } 072 073 public static Class<?> getPrimitiveClass(Type type) { 074 Class<?> k = types2Class.get(type); 075 if (k == Long.class) { 076 return Long.TYPE; 077 } else if (k == Integer.class) { 078 return Integer.TYPE; 079 } else if (k == Double.class) { 080 return Double.TYPE; 081 } else if (k == Float.class) { 082 return Float.TYPE; 083 } else if (k == Short.class) { 084 return Short.TYPE; 085 } else if (k == Byte.class) { 086 return Byte.TYPE; 087 } else if (k == Character.class) { 088 return Character.TYPE; 089 } 090 return k; 091 } 092 093 public static void bind(Type type, Class<?> klass) { 094 class2Types.put(klass, type); 095 types2Class.put(type, klass); 096 } 097 098 static { 099 bind(StringType.INSTANCE, String.class); 100 bind(LongType.INSTANCE, Long.class); 101 bind(IntegerType.INSTANCE, Integer.class); 102 bind(DoubleType.INSTANCE, Double.class); 103 bind(BooleanType.INSTANCE, Boolean.class); 104 bind(BinaryType.INSTANCE, InputStream.class); 105 bind(DateType.INSTANCE, Date.class); 106 bind(DateType.INSTANCE, Calendar.class); 107 108 // bind(typeService.getTypeManager().getType("content"), Blob.class); 109 // bind(typeService.getTypeManager().getType("content"), List.class); 110 // bind(CompType.INSTANCE, Blob.class); 111 } 112 113}