use of org.qi4j.runtime.value.ValueModel in project qi4j-sdk by Qi4j.
the class ModuleInstance method newValueBuilderWithState.
@Override
public <T> ValueBuilder<T> newValueBuilderWithState(Class<T> mixinType, Function<PropertyDescriptor, Object> propertyFunction, Function<AssociationDescriptor, EntityReference> associationFunction, Function<AssociationDescriptor, Iterable<EntityReference>> manyAssociationFunction, Function<AssociationDescriptor, Map<String, EntityReference>> namedAssociationFunction) {
NullArgumentException.validateNotNull("propertyFunction", propertyFunction);
NullArgumentException.validateNotNull("associationFunction", associationFunction);
NullArgumentException.validateNotNull("manyAssociationFunction", manyAssociationFunction);
NullArgumentException.validateNotNull("namedAssociationFunction", namedAssociationFunction);
ModelModule<ValueModel> compositeModelModule = typeLookup.lookupValueModel(mixinType);
if (compositeModelModule == null) {
throw new NoSuchValueException(mixinType.getName(), name());
}
StateResolver stateResolver = new FunctionStateResolver(propertyFunction, associationFunction, manyAssociationFunction, namedAssociationFunction);
return new ValueBuilderWithState<>(compositeModelModule, this, stateResolver);
}
use of org.qi4j.runtime.value.ValueModel in project qi4j-sdk by Qi4j.
the class ModuleInstance method newValueBuilder.
@Override
public <T> ValueBuilder<T> newValueBuilder(Class<T> mixinType) throws NoSuchValueException {
NullArgumentException.validateNotNull("mixinType", mixinType);
ModelModule<ValueModel> compositeModelModule = typeLookup.lookupValueModel(mixinType);
if (compositeModelModule == null) {
throw new NoSuchValueException(mixinType.getName(), name());
}
StateResolver stateResolver = new InitialStateResolver(compositeModelModule.module());
return new ValueBuilderInstance<>(compositeModelModule, this, stateResolver);
}
use of org.qi4j.runtime.value.ValueModel in project qi4j-sdk by Qi4j.
the class ModuleInstance method newValueBuilderWithPrototype.
@Override
public <T> ValueBuilder<T> newValueBuilderWithPrototype(T prototype) {
NullArgumentException.validateNotNull("prototype", prototype);
ValueInstance valueInstance = ValueInstance.valueInstanceOf((ValueComposite) prototype);
Class<Composite> valueType = (Class<Composite>) first(valueInstance.types());
ModelModule<ValueModel> modelModule = typeLookup.lookupValueModel(valueType);
if (modelModule == null) {
throw new NoSuchValueException(valueType.getName(), name());
}
return new ValueBuilderWithPrototype<T>(modelModule, this, prototype);
}
use of org.qi4j.runtime.value.ValueModel 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.runtime.value.ValueModel in project qi4j-sdk by Qi4j.
the class ModuleInstance method newValueFromSerializedState.
@Override
public <T> T newValueFromSerializedState(Class<T> mixinType, String serializedState) throws NoSuchValueException, ConstructionException {
NullArgumentException.validateNotNull("mixinType", mixinType);
ModelModule<ValueModel> modelModule = typeLookup.lookupValueModel(mixinType);
if (modelModule == null) {
throw new NoSuchValueException(mixinType.getName(), name());
}
try {
return valueSerialization().deserialize(modelModule.model().valueType(), serializedState);
} catch (ValueSerializationException ex) {
throw new ConstructionException("Could not create value from serialized state", ex);
}
}
Aggregations