Search in sources :

Example 1 with ValueModel

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);
}
Also used : NoSuchValueException(org.qi4j.api.value.NoSuchValueException) ValueModel(org.qi4j.runtime.value.ValueModel) ValueBuilderWithState(org.qi4j.runtime.value.ValueBuilderWithState) FunctionStateResolver(org.qi4j.runtime.composite.FunctionStateResolver) StateResolver(org.qi4j.runtime.composite.StateResolver) FunctionStateResolver(org.qi4j.runtime.composite.FunctionStateResolver)

Example 2 with ValueModel

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);
}
Also used : ValueBuilderInstance(org.qi4j.runtime.value.ValueBuilderInstance) NoSuchValueException(org.qi4j.api.value.NoSuchValueException) ValueModel(org.qi4j.runtime.value.ValueModel) FunctionStateResolver(org.qi4j.runtime.composite.FunctionStateResolver) StateResolver(org.qi4j.runtime.composite.StateResolver)

Example 3 with ValueModel

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);
}
Also used : NoSuchValueException(org.qi4j.api.value.NoSuchValueException) Composite(org.qi4j.api.composite.Composite) ValueComposite(org.qi4j.api.value.ValueComposite) EntityComposite(org.qi4j.api.entity.EntityComposite) ValueModel(org.qi4j.runtime.value.ValueModel) ValueBuilderWithPrototype(org.qi4j.runtime.value.ValueBuilderWithPrototype) ValueInstance(org.qi4j.runtime.value.ValueInstance)

Example 4 with ValueModel

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;
}
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 5 with ValueModel

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);
    }
}
Also used : NoSuchValueException(org.qi4j.api.value.NoSuchValueException) ValueModel(org.qi4j.runtime.value.ValueModel) ValueSerializationException(org.qi4j.api.value.ValueSerializationException) ConstructionException(org.qi4j.api.common.ConstructionException)

Aggregations

ValueModel (org.qi4j.runtime.value.ValueModel)7 NoSuchValueException (org.qi4j.api.value.NoSuchValueException)4 InvalidApplicationException (org.qi4j.api.common.InvalidApplicationException)2 MetaInfo (org.qi4j.api.common.MetaInfo)2 ValueComposite (org.qi4j.api.value.ValueComposite)2 AssociationsModel (org.qi4j.runtime.association.AssociationsModel)2 ManyAssociationsModel (org.qi4j.runtime.association.ManyAssociationsModel)2 FunctionStateResolver (org.qi4j.runtime.composite.FunctionStateResolver)2 StateResolver (org.qi4j.runtime.composite.StateResolver)2 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 TypeVariable (java.lang.reflect.TypeVariable)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 ConstructionException (org.qi4j.api.common.ConstructionException)1 Composite (org.qi4j.api.composite.Composite)1 EntityComposite (org.qi4j.api.entity.EntityComposite)1 DuplicateServiceIdentityException (org.qi4j.api.service.DuplicateServiceIdentityException)1 Module (org.qi4j.api.structure.Module)1 CollectionType (org.qi4j.api.type.CollectionType)1