Search in sources :

Example 1 with MapType

use of org.whole.lang.pojo.model.MapType in project whole by wholeplatform.

the class PojoUtils method calculateTypeName.

public static String calculateTypeName(Type type) {
    String typeName = null;
    switch(type.wGetEntityDescriptor().getOrdinal()) {
        case PrimitiveType_ord:
            typeName = StringUtils.toUpperCap(type.wStringValue()) + "Data";
            break;
        case ReferenceType_ord:
            typeName = StringUtils.toSimpleName(type.wStringValue());
            break;
        case ArrayType_ord:
            ArrayType arrayType = (ArrayType) type;
            typeName = calculateTypeName(arrayType.getElementType()) + "Array";
            break;
        case CollectionType_ord:
            CollectionType collectionType = (CollectionType) type;
            String interfaceName = collectionType.getCollectionInterface().wEnumValue().toString();
            typeName = calculateTypeName(collectionType.getElementType()) + interfaceName;
            break;
        case MapType_ord:
            MapType mapType = (MapType) type;
            typeName = calculateTypeName(mapType.getKeyType()) + "To" + calculateTypeName(mapType.getValueType()) + "Map";
            break;
    }
    return typeName;
}
Also used : ArrayType(org.whole.lang.pojo.model.ArrayType) CollectionType(org.whole.lang.pojo.model.CollectionType) MapType(org.whole.lang.pojo.model.MapType)

Example 2 with MapType

use of org.whole.lang.pojo.model.MapType in project whole by wholeplatform.

the class PojoUtils method create.

@SuppressWarnings("unchecked")
public static <E extends IEntity> E create(Object fromObject, Type type, Library library) {
    // map product type
    ProductDeclaration productDeclaration = findProductDeclaration(type, library);
    E toIEntity = PojoUtils.<E>createEntity(productDeclaration, library);
    // translate contents as needed
    switch(productDeclaration.wGetEntityDescriptor().getOrdinal()) {
        case PojoDeclaration_ord:
            translate(fromObject, toIEntity, (PojoDeclaration) productDeclaration, library);
            break;
        case EnumDeclaration_ord:
            EnumValue enumValue = DefaultDataTypePersistenceParser.instance.parseEnumValue(toIEntity.wGetEntityDescriptor(), fromObject.toString());
            toIEntity.wSetValue(enumValue);
            break;
        case // TODO
        AnnotationDeclaration_ord:
            break;
        case DataTypeDeclaration_ord:
            Type elementType;
            switch(type.wGetEntityDescriptor().getOrdinal()) {
                case PrimitiveType_ord:
                case ReferenceType_ord:
                    toIEntity.wSetValue(fromObject);
                    break;
                case ArrayType_ord:
                    ArrayType arrayType = (ArrayType) type;
                    elementType = arrayType.getElementType();
                    for (int i = 0; i < Array.getLength(fromObject); i++) toIEntity.wAdd(create(Array.get(fromObject, i), elementType, library));
                    break;
                case CollectionType_ord:
                    CollectionType collectionType = (CollectionType) type;
                    Collection<Object> fromCollection = (Collection<Object>) fromObject;
                    elementType = collectionType.getElementType();
                    for (Object element : fromCollection) toIEntity.wAdd(create(element, elementType, library));
                    break;
                case MapType_ord:
                    MapType mapType = (MapType) type;
                    Map<Object, Object> fromMap = (Map<Object, Object>) fromObject;
                    Type keyType = mapType.getKeyType();
                    elementType = mapType.getValueType();
                    for (Entry<Object, Object> element : fromMap.entrySet()) // FIXME workaround for Java 8 compiler
                    toIEntity.wSet(// FIXME workaround for Java 8 compiler
                    (IEntity) create(element.getKey(), keyType, library), create(element.getValue(), elementType, library));
                    break;
            }
            break;
    }
    return toIEntity;
}
Also used : IEntity(org.whole.lang.model.IEntity) EnumValue(org.whole.lang.model.EnumValue) MapType(org.whole.lang.pojo.model.MapType) ArrayType(org.whole.lang.pojo.model.ArrayType) PrimitiveType(org.whole.lang.pojo.model.PrimitiveType) PrimitiveType(org.whole.lang.pojo.reflect.PojoEntityDescriptorEnum.PrimitiveType) Type(org.whole.lang.pojo.model.Type) MapType(org.whole.lang.pojo.model.MapType) ArrayType(org.whole.lang.pojo.model.ArrayType) ReferenceType(org.whole.lang.pojo.reflect.PojoEntityDescriptorEnum.ReferenceType) CollectionType(org.whole.lang.pojo.model.CollectionType) ReferenceType(org.whole.lang.pojo.model.ReferenceType) ProductDeclaration(org.whole.lang.pojo.model.ProductDeclaration) CollectionType(org.whole.lang.pojo.model.CollectionType) Collection(java.util.Collection) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with MapType

use of org.whole.lang.pojo.model.MapType in project whole by wholeplatform.

the class Helpers method toPojoType.

public static Type toPojoType(IEntity javaType, IEntity packageName) {
    PojoEntityFactory pef = PojoEntityFactory.instance;
    switch(javaType.wGetEntityDescriptor().getOrdinal()) {
        case JavaEntityDescriptorEnum.SimpleType_ord:
        case JavaEntityDescriptorEnum.QualifiedType_ord:
            String javaTypeName = DataTypeUtils.getAsPersistenceString(javaType);
            String packageNameString = packageName.wStringValue();
            String typeName = (javaTypeName.indexOf('.') == -1 ? (StringUtils.isAmbiguous(javaTypeName) ? "java.lang" : packageNameString) + "." : "") + javaTypeName;
            if (StringUtils.isString(typeName))
                return pef.createPrimitiveType(PrimitiveTypeEnum.String);
            else if ("java.util.Set".equals(typeName))
                return pef.createCollectionType(pef.createCollectionInterface(CollectionInterfaceEnum.Set), pef.createReferenceType("java.lang.Object"));
            else if ("java.util.List".equals(typeName))
                return pef.createCollectionType(pef.createCollectionInterface(CollectionInterfaceEnum.List), pef.createReferenceType("java.lang.Object"));
            else if ("java.util.Map".equals(typeName))
                return pef.createMapType(pef.createReferenceType("java.lang.Object"), pef.createReferenceType("java.lang.Object"));
            else
                return pef.createReferenceType(typeName);
        case JavaEntityDescriptorEnum.PrimitiveType_ord:
            return (Type) DataTypeUtils.convertCloneIfParented(JavaEntityFactory.instance.createPrimitiveType(((PrimitiveType) javaType).getValue()), PojoEntityDescriptorEnum.PrimitiveType);
        case JavaEntityDescriptorEnum.ArrayType_ord:
            ArrayType arrayType = (ArrayType) javaType;
            return pef.createArrayType(toPojoType(arrayType.getComponentType(), packageName));
        case JavaEntityDescriptorEnum.ParameterizedType_ord:
            ParameterizedType parameterizedType = (ParameterizedType) javaType;
            Types typeArguments = parameterizedType.getTypeArguments();
            Type pojoType = toPojoType(parameterizedType.getType(), packageName);
            if (Matcher.matchImpl(PojoEntityDescriptorEnum.CollectionType, pojoType) && typeArguments.wSize() == 1)
                ((CollectionType) pojoType).setElementType(toPojoType(typeArguments.wGet(0), packageName));
            else if (Matcher.matchImpl(PojoEntityDescriptorEnum.MapType, pojoType) && typeArguments.wSize() == 2) {
                ((MapType) pojoType).setKeyType(toPojoType(typeArguments.wGet(0), packageName));
                ((MapType) pojoType).setValueType(toPojoType(typeArguments.wGet(1), packageName));
            }
            return pojoType;
        default:
            throw new IllegalStateException("cannot convert java type to pojo type: " + javaType);
    }
}
Also used : PojoEntityFactory(org.whole.lang.pojo.factories.PojoEntityFactory) ArrayType(org.whole.lang.java.model.ArrayType) ParameterizedType(org.whole.lang.java.model.ParameterizedType) Types(org.whole.lang.java.model.Types) MapType(org.whole.lang.pojo.model.MapType) Type(org.whole.lang.pojo.model.Type) ParameterizedType(org.whole.lang.java.model.ParameterizedType) CollectionType(org.whole.lang.pojo.model.CollectionType) ArrayType(org.whole.lang.java.model.ArrayType) PrimitiveType(org.whole.lang.java.model.PrimitiveType)

Example 4 with MapType

use of org.whole.lang.pojo.model.MapType in project whole by wholeplatform.

the class MapTypePart method getModelSpecificChildren.

protected List<IEntity> getModelSpecificChildren() {
    List<IEntity> list = new ArrayList<IEntity>(2);
    MapType entity = getModelEntity();
    list.add(entity.getKeyType());
    list.add(entity.getValueType());
    return list;
}
Also used : IEntity(org.whole.lang.model.IEntity) ArrayList(java.util.ArrayList) MapType(org.whole.lang.pojo.model.MapType)

Aggregations

MapType (org.whole.lang.pojo.model.MapType)4 CollectionType (org.whole.lang.pojo.model.CollectionType)3 IEntity (org.whole.lang.model.IEntity)2 ArrayType (org.whole.lang.pojo.model.ArrayType)2 Type (org.whole.lang.pojo.model.Type)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ArrayType (org.whole.lang.java.model.ArrayType)1 ParameterizedType (org.whole.lang.java.model.ParameterizedType)1 PrimitiveType (org.whole.lang.java.model.PrimitiveType)1 Types (org.whole.lang.java.model.Types)1 EnumValue (org.whole.lang.model.EnumValue)1 PojoEntityFactory (org.whole.lang.pojo.factories.PojoEntityFactory)1 PrimitiveType (org.whole.lang.pojo.model.PrimitiveType)1 ProductDeclaration (org.whole.lang.pojo.model.ProductDeclaration)1 ReferenceType (org.whole.lang.pojo.model.ReferenceType)1 PrimitiveType (org.whole.lang.pojo.reflect.PojoEntityDescriptorEnum.PrimitiveType)1 ReferenceType (org.whole.lang.pojo.reflect.PojoEntityDescriptorEnum.ReferenceType)1