001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Nuxeo - initial API and implementation
011 *
012 * $Id$
013 */
014
015package org.nuxeo.ecm.core.schema.types;
016
017import java.io.InputStream;
018import java.util.Calendar;
019import java.util.Date;
020import java.util.Hashtable;
021import java.util.List;
022import java.util.Map;
023
024import org.nuxeo.ecm.core.schema.types.primitives.BinaryType;
025import org.nuxeo.ecm.core.schema.types.primitives.BooleanType;
026import org.nuxeo.ecm.core.schema.types.primitives.DateType;
027import org.nuxeo.ecm.core.schema.types.primitives.DoubleType;
028import org.nuxeo.ecm.core.schema.types.primitives.IntegerType;
029import org.nuxeo.ecm.core.schema.types.primitives.LongType;
030import org.nuxeo.ecm.core.schema.types.primitives.StringType;
031
032/**
033 * Maps ECM types to Java classes.
034 *
035 * @author bstefanescu
036 */
037public final class JavaTypes {
038
039    private static final Map<Class<?>, Type> class2Types = new Hashtable<Class<?>, Type>();
040
041    private static final Map<Type, Class<?>> types2Class = new Hashtable<Type, Class<?>>();
042
043    // Utility class.
044    private JavaTypes() {
045    }
046
047    public static boolean isList(Object object) {
048        return object instanceof List;
049    }
050
051    public static boolean isComplex(Object object) {
052        return object instanceof Map;
053    }
054
055    public static Type getType(Class<?> klass) {
056        return class2Types.get(klass);
057    }
058
059    public static Class<?> getClass(Type type) {
060        if (type.isSimpleType() && !((SimpleType) type).isPrimitive()) {
061            return getClass(((SimpleType) type).getPrimitiveType());
062        }
063        return types2Class.get(type);
064    }
065
066    public static Class<?> getPrimitiveClass(Type type) {
067        Class<?> k = types2Class.get(type);
068        if (k == Long.class) {
069            return Long.TYPE;
070        } else if (k == Integer.class) {
071            return Integer.TYPE;
072        } else if (k == Double.class) {
073            return Double.TYPE;
074        } else if (k == Float.class) {
075            return Float.TYPE;
076        } else if (k == Short.class) {
077            return Short.TYPE;
078        } else if (k == Byte.class) {
079            return Byte.TYPE;
080        } else if (k == Character.class) {
081            return Character.TYPE;
082        }
083        return k;
084    }
085
086    public static void bind(Type type, Class<?> klass) {
087        class2Types.put(klass, type);
088        types2Class.put(type, klass);
089    }
090
091    static {
092        bind(StringType.INSTANCE, String.class);
093        bind(LongType.INSTANCE, Long.class);
094        bind(IntegerType.INSTANCE, Integer.class);
095        bind(DoubleType.INSTANCE, Double.class);
096        bind(BooleanType.INSTANCE, Boolean.class);
097        bind(BinaryType.INSTANCE, InputStream.class);
098        bind(DateType.INSTANCE, Date.class);
099        bind(DateType.INSTANCE, Calendar.class);
100
101        // bind(typeService.getTypeManager().getType("content"), Blob.class);
102        // bind(typeService.getTypeManager().getType("content"), List.class);
103        // bind(CompType.INSTANCE, Blob.class);
104    }
105
106}