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);
}
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);
}
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;
}
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;
}
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);
}
}
}
Aggregations