Search in sources :

Example 1 with CollectionType

use of org.qi4j.api.type.CollectionType in project qi4j-sdk by Qi4j.

the class ValueTypeFactory method newValueType.

@SuppressWarnings({ "raw", "unchecked" })
public ValueType newValueType(Type type, Class declaringClass, Class compositeType, LayerModel layer, ModuleModel module) {
    ValueType valueType = null;
    if (CollectionType.isCollection(type)) {
        if (type instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType) type;
            Type collectionType = pt.getActualTypeArguments()[0];
            if (collectionType instanceof TypeVariable) {
                TypeVariable collectionTypeVariable = (TypeVariable) collectionType;
                collectionType = Classes.resolveTypeVariable(collectionTypeVariable, declaringClass, compositeType);
            }
            ValueType collectedType = newValueType(collectionType, declaringClass, compositeType, layer, module);
            valueType = new CollectionType(Classes.RAW_CLASS.map(type), collectedType);
        } else {
            valueType = new CollectionType(Classes.RAW_CLASS.map(type), newValueType(Object.class, declaringClass, compositeType, layer, module));
        }
    } else if (MapType.isMap(type)) {
        if (type instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType) type;
            Type keyType = pt.getActualTypeArguments()[0];
            if (keyType instanceof TypeVariable) {
                TypeVariable keyTypeVariable = (TypeVariable) keyType;
                keyType = Classes.resolveTypeVariable(keyTypeVariable, declaringClass, compositeType);
            }
            ValueType keyedType = newValueType(keyType, declaringClass, compositeType, layer, module);
            Type valType = pt.getActualTypeArguments()[1];
            if (valType instanceof TypeVariable) {
                TypeVariable valueTypeVariable = (TypeVariable) valType;
                valType = Classes.resolveTypeVariable(valueTypeVariable, declaringClass, compositeType);
            }
            ValueType valuedType = newValueType(valType, declaringClass, compositeType, layer, module);
            valueType = new MapType(Classes.RAW_CLASS.map(type), keyedType, valuedType);
        } else {
            valueType = new MapType(Classes.RAW_CLASS.map(type), newValueType(Object.class, declaringClass, compositeType, layer, module), newValueType(Object.class, declaringClass, compositeType, layer, module));
        }
    } else if (ValueCompositeType.isValueComposite(type)) {
        // Find ValueModel in module/layer/used layers
        ValueModel model = new ValueFinder(layer, module, Classes.RAW_CLASS.map(type)).getFoundModel();
        if (model == null) {
            if (type.equals(ValueComposite.class)) {
                // Create default model
                MixinsModel mixinsModel = new MixinsModel();
                Iterable valueComposite = (Iterable) Iterables.iterable(ValueComposite.class);
                ValueStateModel valueStateModel = new ValueStateModel(new PropertiesModel(), new AssociationsModel(), new ManyAssociationsModel(), new NamedAssociationsModel());
                model = new ValueModel(valueComposite, Visibility.application, new MetaInfo(), mixinsModel, valueStateModel, new CompositeMethodsModel(mixinsModel));
            } else {
                throw new InvalidApplicationException("[" + module.name() + "] Could not find ValueComposite of type " + type);
            }
        }
        return model.valueType();
    } else if (EnumType.isEnum(type)) {
        valueType = new EnumType(Classes.RAW_CLASS.map(type));
    } else {
        valueType = new ValueType(Classes.RAW_CLASS.map(type));
    }
    return valueType;
}
Also used : MixinsModel(org.qi4j.runtime.composite.MixinsModel) ValueModel(org.qi4j.runtime.value.ValueModel) ValueType(org.qi4j.api.type.ValueType) NamedAssociationsModel(org.qi4j.runtime.association.NamedAssociationsModel) MetaInfo(org.qi4j.api.common.MetaInfo) ValueComposite(org.qi4j.api.value.ValueComposite) MapType(org.qi4j.api.type.MapType) ParameterizedType(java.lang.reflect.ParameterizedType) ManyAssociationsModel(org.qi4j.runtime.association.ManyAssociationsModel) ValueCompositeType(org.qi4j.api.type.ValueCompositeType) EnumType(org.qi4j.api.type.EnumType) MapType(org.qi4j.api.type.MapType) CollectionType(org.qi4j.api.type.CollectionType) ValueType(org.qi4j.api.type.ValueType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) CompositeMethodsModel(org.qi4j.runtime.composite.CompositeMethodsModel) TypeVariable(java.lang.reflect.TypeVariable) EnumType(org.qi4j.api.type.EnumType) CollectionType(org.qi4j.api.type.CollectionType) InvalidApplicationException(org.qi4j.api.common.InvalidApplicationException) ValueStateModel(org.qi4j.runtime.value.ValueStateModel) PropertiesModel(org.qi4j.runtime.property.PropertiesModel) ManyAssociationsModel(org.qi4j.runtime.association.ManyAssociationsModel) AssociationsModel(org.qi4j.runtime.association.AssociationsModel) NamedAssociationsModel(org.qi4j.runtime.association.NamedAssociationsModel)

Example 2 with CollectionType

use of org.qi4j.api.type.CollectionType in project qi4j-sdk by Qi4j.

the class AbstractCollectionSerializationTest method givenCollectionTypeWithFloatAndNullElementWhenSerializingAndDeserializingExpectEquals.

@Test
public void givenCollectionTypeWithFloatAndNullElementWhenSerializingAndDeserializingExpectEquals() throws Exception {
    String output = valueSerialization.serialize(floatCollection());
    CollectionType collectionType = new CollectionType(List.class, new ValueType(Float.class));
    List<Float> list = valueSerialization.deserialize(collectionType, output);
    assertEquals(floatCollection(), list);
}
Also used : ValueType(org.qi4j.api.type.ValueType) CollectionType(org.qi4j.api.type.CollectionType) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 3 with CollectionType

use of org.qi4j.api.type.CollectionType in project qi4j-sdk by Qi4j.

the class AbstractCollectionSerializationTest method givenCollectionTypeWithBigDecimalAndNullElementWhenSerializingAndDeserializingExpectEquals.

@Test
public void givenCollectionTypeWithBigDecimalAndNullElementWhenSerializingAndDeserializingExpectEquals() throws Exception {
    String output = valueSerialization.serialize(bigDecimalCollection());
    CollectionType collectionType = new CollectionType(Collection.class, new ValueType(BigDecimal.class));
    Collection<BigDecimal> collection = valueSerialization.deserialize(collectionType, output);
    assertEquals(bigDecimalCollection(), collection);
}
Also used : ValueType(org.qi4j.api.type.ValueType) CollectionType(org.qi4j.api.type.CollectionType) BigDecimal(java.math.BigDecimal) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 4 with CollectionType

use of org.qi4j.api.type.CollectionType in project qi4j-sdk by Qi4j.

the class AbstractCollectionSerializationTest method givenListOfValueCompositesAndNullElementWhenSerializingAndDeserializingExpectEquals.

@Test
public void givenListOfValueCompositesAndNullElementWhenSerializingAndDeserializingExpectEquals() throws Exception {
    String output = valueSerialization.serialize(valueCompositesList());
    ValueCompositeType valueType = module.valueDescriptor(SomeValue.class.getName()).valueType();
    CollectionType collectionType = new CollectionType(List.class, valueType);
    List<SomeValue> value = valueSerialization.deserialize(collectionType, output);
    assertEquals(valueCompositesList(), value);
}
Also used : CollectionType(org.qi4j.api.type.CollectionType) ValueCompositeType(org.qi4j.api.type.ValueCompositeType) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 5 with CollectionType

use of org.qi4j.api.type.CollectionType in project qi4j-sdk by Qi4j.

the class AbstractCollectionSerializationTest method givenCollectionTypeWithLongAndNullElementWhenSerializingAndDeserializingExpectEquals.

@Test
public void givenCollectionTypeWithLongAndNullElementWhenSerializingAndDeserializingExpectEquals() throws Exception {
    String output = valueSerialization.serialize(longCollection());
    CollectionType collectionType = new CollectionType(List.class, new ValueType(Long.class));
    List<Long> list = valueSerialization.deserialize(collectionType, output);
    assertEquals(longCollection(), list);
}
Also used : ValueType(org.qi4j.api.type.ValueType) CollectionType(org.qi4j.api.type.CollectionType) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Aggregations

CollectionType (org.qi4j.api.type.CollectionType)22 Test (org.junit.Test)16 ValueType (org.qi4j.api.type.ValueType)16 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)16 MapType (org.qi4j.api.type.MapType)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Map (java.util.Map)6 ValueCompositeType (org.qi4j.api.type.ValueCompositeType)6 LinkedHashMap (java.util.LinkedHashMap)5 Date (java.util.Date)3 HashMap (java.util.HashMap)3 LinkedHashSet (java.util.LinkedHashSet)3 Set (java.util.Set)3 EntityReference (org.qi4j.api.entity.EntityReference)3 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)3 EnumType (org.qi4j.api.type.EnumType)3 ValueComposite (org.qi4j.api.value.ValueComposite)3 BigInteger (java.math.BigInteger)2 Collection (java.util.Collection)2