Search in sources :

Example 1 with MapType

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

the class AbstractCollectionSerializationTest method givenListOfMapStringStringAndNullElementWhenSerializingAndDeserializingExpectEquals.

@Test
public void givenListOfMapStringStringAndNullElementWhenSerializingAndDeserializingExpectEquals() throws Exception {
    String output = valueSerialization.serialize(stringListOfMaps());
    ValueType stringType = new ValueType(String.class);
    CollectionType collectionType = new CollectionType(List.class, new MapType(Map.class, stringType, stringType));
    List<Map<String, String>> value = valueSerialization.deserialize(collectionType, output);
    assertEquals(stringListOfMaps(), value);
}
Also used : ValueType(org.qi4j.api.type.ValueType) CollectionType(org.qi4j.api.type.CollectionType) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) MapType(org.qi4j.api.type.MapType) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 2 with MapType

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

the class AbstractCollectionSerializationTest method givenMapOfStringByteAndNullElementWhenSerializingAndDeserializingExpectEquals.

@Test
public void givenMapOfStringByteAndNullElementWhenSerializingAndDeserializingExpectEquals() throws Exception {
    String output = valueSerialization.serialize(stringByteMap());
    MapType mapType = new MapType(Map.class, new ValueType(String.class), new ValueType(Byte.class));
    Map<String, Byte> value = valueSerialization.deserialize(mapType, output);
    assertEquals(stringByteMap(), value);
}
Also used : ValueType(org.qi4j.api.type.ValueType) MapType(org.qi4j.api.type.MapType) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 3 with MapType

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

the class ValueTypeFactory method newValueType.

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);
                model = new ValueModel(valueComposite, Visibility.application, new MetaInfo(), mixinsModel, new ValueStateModel(new PropertiesModel(), new AssociationsModel(), new ManyAssociationsModel()), 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) 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)

Example 4 with MapType

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

the class PropertyInstance method prepareBuilderState.

public void prepareBuilderState(PropertyModel propertyDescriptor) {
    // Check if state has to be modified
    if (propertyDescriptor.valueType() instanceof ValueCompositeType) {
        Object value = get();
        if (value != null) {
            ValueInstance.valueInstanceOf((ValueComposite) value).prepareBuilderState();
        }
    } else if (propertyDescriptor.valueType() instanceof CollectionType) {
        T value = get();
        if (value != null) {
            if (propertyDescriptor.isImmutable()) {
                if (value instanceof List) {
                    value = (T) Collections.unmodifiableList((List<? extends Object>) value);
                } else if (value instanceof Set) {
                    value = (T) Collections.unmodifiableSet((Set<? extends Object>) value);
                } else {
                    value = (T) Collections.unmodifiableCollection((Collection<? extends Object>) value);
                }
                this.value = value;
            }
            CollectionType collection = (CollectionType) propertyDescriptor.valueType();
            if (collection.collectedType() instanceof ValueCompositeType) {
                Collection coll = (Collection) value;
                for (Object instance : coll) {
                    ValueInstance.valueInstanceOf((ValueComposite) instance).prepareBuilderState();
                }
            }
        }
    } else if (propertyDescriptor.valueType() instanceof MapType) {
        T value = get();
        if (value != null) {
            MapType mapType = (MapType) propertyDescriptor.valueType();
            if (mapType.keyType() instanceof ValueCompositeType) {
                Map map = (Map) value;
                for (Object instance : map.keySet()) {
                    ValueInstance.valueInstanceOf((ValueComposite) instance).prepareBuilderState();
                }
            }
            if (mapType.valueType() instanceof ValueCompositeType) {
                Map map = (Map) value;
                for (Object instance : map.values()) {
                    ValueInstance.valueInstanceOf((ValueComposite) instance).prepareBuilderState();
                }
            }
            if (propertyDescriptor.isImmutable()) {
                value = (T) Collections.unmodifiableMap((Map<?, ?>) value);
            }
            this.value = value;
        }
    }
    model = propertyDescriptor;
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) CollectionType(org.qi4j.api.type.CollectionType) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) ValueComposite(org.qi4j.api.value.ValueComposite) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ValueCompositeType(org.qi4j.api.type.ValueCompositeType) MapType(org.qi4j.api.type.MapType)

Example 5 with MapType

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

the class PropertyInstance method prepareToBuild.

public void prepareToBuild(PropertyModel propertyDescriptor) {
    // Check if state has to be modified
    model = propertyDescriptor.getBuilderInfo();
    if (propertyDescriptor.valueType() instanceof ValueCompositeType) {
        Object value = get();
        if (value != null) {
            ValueInstance.valueInstanceOf((ValueComposite) value).prepareToBuild();
        }
    } else if (propertyDescriptor.valueType() instanceof CollectionType) {
        Object value = get();
        if (value != null) {
            if (value instanceof List) {
                value = new ArrayList((Collection) value);
            } else if (value instanceof Set) {
                value = new LinkedHashSet((Collection) value);
            }
            // Check if items are Values
            CollectionType collection = (CollectionType) propertyDescriptor.valueType();
            if (collection.collectedType() instanceof ValueCompositeType) {
                Collection coll = (Collection) value;
                for (Object instance : coll) {
                    ValueInstance.valueInstanceOf((ValueComposite) instance).prepareToBuild();
                }
            }
            set((T) value);
        }
    } else if (propertyDescriptor.valueType() instanceof MapType) {
        Object value = get();
        if (value != null) {
            Map map = new LinkedHashMap((Map) value);
            // Check if keys/values are Values
            MapType mapType = (MapType) propertyDescriptor.valueType();
            if (mapType.keyType() instanceof ValueCompositeType) {
                for (Object instance : map.keySet()) {
                    ValueInstance.valueInstanceOf((ValueComposite) instance).prepareToBuild();
                }
            }
            if (mapType.valueType() instanceof ValueCompositeType) {
                for (Object instance : map.values()) {
                    ValueInstance.valueInstanceOf((ValueComposite) instance).prepareToBuild();
                }
            }
            set((T) value);
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) ValueComposite(org.qi4j.api.value.ValueComposite) MapType(org.qi4j.api.type.MapType) LinkedHashMap(java.util.LinkedHashMap) CollectionType(org.qi4j.api.type.CollectionType) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ValueCompositeType(org.qi4j.api.type.ValueCompositeType)

Aggregations

MapType (org.qi4j.api.type.MapType)8 CollectionType (org.qi4j.api.type.CollectionType)7 ValueType (org.qi4j.api.type.ValueType)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 ValueCompositeType (org.qi4j.api.type.ValueCompositeType)5 Map (java.util.Map)4 LinkedHashMap (java.util.LinkedHashMap)3 Test (org.junit.Test)3 EnumType (org.qi4j.api.type.EnumType)3 ValueComposite (org.qi4j.api.value.ValueComposite)3 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)3 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 Set (java.util.Set)2 Preferences (java.util.prefs.Preferences)2 QualifiedName (org.qi4j.api.common.QualifiedName)2 EntityReference (org.qi4j.api.entity.EntityReference)2 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)2