@SuppressWarnings("unchecked") public O fromPropertyMap(Map<PropertyPath, Object> properties) { return (O) new ReadContext(schemaRoot, properties).getObject(); }
@Override public void bind(PropertyTree propertyTree, Object object, ReadContext context) throws Exception { @SuppressWarnings("unchecked") Set<Object> set = (Set<Object>) object; List<Object> elements = new ArrayList<>(); forEachElement(propertyTree, elementPath -> elements.add(context.getObject(elementPath))); context.bindAll(); set.addAll(elements); }
@Override public Object instantiate(PropertyTree propertyTree, Object value, ReadContext context) throws Exception { PropertyPath targetPath = targetRoot.keyOrIndex(value); if (context.getProperty(targetPath) != null) { return context.getObject(targetPath); } return null; }
@Override public Object instantiate(PropertyTree propertyTree, Object value, ReadContext context) throws Exception { Collection<PropertyTree> children = propertyTree.getChildren(); List<Object> list = new ArrayList<>(children.size()); for (PropertyTree child : children) { list.add(context.getObject(child)); } return list; }
public Object getProperty(PropertyTree propertyTree) { return getProperty(propertyTree.path); }
public Object getObject() { if (rootNode == null) { return null; } try { Object value = properties.get(rootNode.path); ValueType valueType = schemaRoot.getValue(); Object result = valueType.instantiate(rootNode, value, this); objects.put(rootNode.path, result); valueType.bind(rootNode, result, this); bindAll(); return result; } catch (Exception e) { throw new RuntimeException(e); } }
public Object getObject(PropertyPath path) { return getObject(rootNode.get(path)); }
@Override public Object instantiate(PropertyTree propertyTree, Object constant, ReadContext context) throws Exception { Map<Object, Object> map = newMap(propertyTree.getChildren().size()); for (PropertyTree entryPath : propertyTree.getChildren()) { Object key = keyType.fromNodeId(entryPath.path.getNodeId(), context); Object value = null; if (!NULL.equals(context.getProperty(entryPath))) { value = context.getObject(entryPath); } map.put(key, value); } return map; }
@Override public Object instantiate(PropertyTree propertyTree, Object valueObject, ReadContext context) throws Exception { Object[] params = constructor.newParametersArray(); int i=0; for (String param : constructor.getParameters()) { PropertyTree child = propertyTree.get(NodeId.property(param)); Object value = child != null ? context.getObject(child) : null; params[i++] = value; } if (constructor.hasParameters()) { // Ensure that constructor parameters are bound context.bindAll(); } Object object = constructor.newInstance(params); if (identifier != null && !constructor.hasParameter(identifier.name) && identifier.property.isWritable()) { PropertyTree child = propertyTree.get(NodeId.property(identifier.name)); if (child != null) { Object value = context.getObject(child); if (value != null) { identifier.property.set(object, value); } } } return object; }
@Override public Object instantiate(PropertyTree propertyTree, Object value, ReadContext context) throws Exception { SortedMap<NodeId, PropertyTree> children = propertyTree.getChildrenMap(); List<Object> list = new ArrayList<>(children.size()); for (Map.Entry<NodeId, PropertyTree> entry : children.entrySet()) { NodeId nodeId = entry.getKey(); if (nodeId.isIndex()) { int index = (int) nodeId.getIndex(); assert index >= list.size(); Object element = context.getObject(entry.getValue()); if (element != null) { ensureSize(list, index); list.add(index, element); } } } return list; }
@Override public void bind(PropertyTree propertyTree, Object object, ReadContext context) throws Exception { for (PropertyTree child : propertyTree.getChildren()) { NodeId nodeId = child.getNodeId(); if (nodeId.isKey() && !constructor.hasParameter(nodeId.getKey())) { AccessibleProperty property = properties.get(nodeId.getKey()); if (property != null && property.isWritable()) { Object value = context.getObject(child); property.set(object, value); } } } }