/*
* Copyright (C) 2003-2011 eXo Platform SAS.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.etk.model.plugins.entity.type;
import java.util.HashMap;
import java.util.Map;
import org.etk.model.plugins.vt2.PropertyMetaType;
import org.etk.orm.plugins.bean.type.SimpleTypeProvider;
import org.etk.reflect.api.ClassTypeInfo;
import org.etk.reflect.api.TypeInfo;
import org.etk.reflect.api.TypeVariableInfo;
/**
* Created by The eXo Platform SAS
* Author : eXoPlatform
* exo@exoplatform.com
* Jul 15, 2011
*/
class SimpleTypeMappingImpl<I> implements SimpleTypeBinding {
/** . */
private static final Map<ClassTypeInfo, PropertyMetaType<?>> propertyMetaTypes;
static {
//
Map<ClassTypeInfo, PropertyMetaType<?>> _jcrTypes = new HashMap<ClassTypeInfo, PropertyMetaType<?>>();
_jcrTypes.put((ClassTypeInfo)SimpleTypeResolver.typeDomain.resolve(SimpleTypeProvider.STRING.class), PropertyMetaType.STRING);
_jcrTypes.put((ClassTypeInfo)SimpleTypeResolver.typeDomain.resolve(SimpleTypeProvider.NAME.class), PropertyMetaType.NAME);
_jcrTypes.put((ClassTypeInfo)SimpleTypeResolver.typeDomain.resolve(SimpleTypeProvider.LONG.class), PropertyMetaType.LONG);
_jcrTypes.put((ClassTypeInfo)SimpleTypeResolver.typeDomain.resolve(SimpleTypeProvider.DOUBLE.class), PropertyMetaType.DOUBLE);
_jcrTypes.put((ClassTypeInfo)SimpleTypeResolver.typeDomain.resolve(SimpleTypeProvider.BOOLEAN.class), PropertyMetaType.BOOLEAN);
_jcrTypes.put((ClassTypeInfo)SimpleTypeResolver.typeDomain.resolve(SimpleTypeProvider.DATE.class), PropertyMetaType.DATE);
propertyMetaTypes = _jcrTypes;
}
/** . */
private SimpleTypeProvider<I, ?> instance;
/** . */
private final PropertyMetaType<I> propertyMetaType;
/** . */
final ClassTypeInfo typeInfo;
/** . */
final TypeInfo external;
SimpleTypeMappingImpl(ClassTypeInfo typeInfo) {
// Find the right subclass
ClassTypeInfo current = typeInfo;
while (!current.getSuperClass().getName().equals(SimpleTypeProvider.class.getName())) {
current = current.getSuperClass();
}
//
ClassTypeInfo stp = (ClassTypeInfo)SimpleTypeResolver.typeDomain.resolve(SimpleTypeProvider.class);
TypeVariableInfo tvi = stp.getTypeParameters().get(1); // <E>
TypeInfo aaa = typeInfo.resolve(tvi);
// if (!aaa.equals(typeInfo)) {
// throw new AssertionError(aaa + " should be equals to " + typeInfo);
// }
//
PropertyMetaType aaaaa = propertyMetaTypes.get(current);
//
this.propertyMetaType = aaaaa;
this.typeInfo = typeInfo;
this.external = aaa;
}
SimpleTypeMappingImpl(ClassTypeInfo typeInfo, PropertyMetaType<I> propertyMetaType) {
ClassTypeInfo stp = (ClassTypeInfo)SimpleTypeResolver.typeDomain.resolve(SimpleTypeProvider.class);
TypeVariableInfo tvi = stp.getTypeParameters().get(1); // <E>
TypeInfo aaa = typeInfo.resolve(tvi);
//
this.propertyMetaType = propertyMetaType;
this.typeInfo = typeInfo;
this.external = aaa;
}
SimpleTypeMappingImpl(Class<? extends SimpleTypeProvider<I, ?>> type, PropertyMetaType<I> propertyMetaType) {
this((ClassTypeInfo)SimpleTypeResolver.typeDomain.resolve(type), propertyMetaType);
}
public PropertyMetaType<I> getPropertyMetaType() {
return propertyMetaType;
}
public SimpleTypeProvider<I, ?> create() {
if (instance == null) {
Class type = (Class)typeInfo.unwrap();
try {
instance = (SimpleTypeProvider<I,?>)type.newInstance();
}
catch (InstantiationException e) {
throw new AssertionError(e);
}
catch (IllegalAccessException e) {
throw new AssertionError(e);
}
}
return instance;
}
}