use of com.vaadin.flow.internal.nodefeature.ModelMap in project flow by vaadin.
the class TemplateModelUtil method resolveBeanAndRun.
/**
* Resolves a bean model type and model map based on a model instance and
* passes those values to the provided callback.
*
* @param <R>
* the return value type
* @param model
* the model instance for which to resolve a type and a map, not
* <code>null</code>
* @param modelPath
* the model path to resolve, not <code>null</code>
* @param callback
* the callback to run with the resolved bean type and the model
* map, not <code>null</code>
* @return the value returned by the callback
*/
public static <R> R resolveBeanAndRun(TemplateModel model, String modelPath, BiFunction<BeanModelType<?>, ModelMap, R> callback) {
assert model != null;
assert modelPath != null;
assert callback != null;
BeanModelType<?> modelType = TemplateModelProxyHandler.getModelTypeForProxy(model);
ModelType beanType = modelType.resolveType(modelPath);
if (beanType instanceof BeanModelType<?>) {
StateNode stateNode = TemplateModelProxyHandler.getStateNodeForProxy(model);
ModelMap modelMap = ModelMap.get(stateNode);
ModelMap beanMap = modelMap.resolveModelMap(modelPath);
return callback.apply((BeanModelType<?>) beanType, beanMap);
} else {
throw new IllegalArgumentException(modelPath + " does not resolve to a bean");
}
}
Aggregations