1 /*******************************************************************************
2 * Copyright (c) 2013 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 * Semantum Oy - initial API and implementation
12 *******************************************************************************/
13 package org.simantics.simulator.variable.impl;
15 import java.util.ArrayList;
16 import java.util.List;
19 import org.simantics.databoard.Bindings;
20 import org.simantics.databoard.binding.Binding;
21 import org.simantics.databoard.binding.VariantBinding;
22 import org.simantics.databoard.binding.error.BindingException;
23 import org.simantics.databoard.binding.mutable.Variant;
24 import org.simantics.databoard.type.Datatype;
25 import org.simantics.simulator.variable.NodeManager;
26 import org.simantics.simulator.variable.exceptions.NoSuchNodeException;
27 import org.simantics.simulator.variable.exceptions.NodeManagerException;
30 * AbstractNodeManager gives default implementations to some methods
33 * @author Hannu Niemistö
35 public abstract class AbstractNodeManager<Node> implements NodeManager<Node> {
38 final static Binding NO_BINDING = new VariantBinding() {
41 public Object getContent(Object variant, Binding contentBinding) throws BindingException {
46 public Object getContent(Object variant) throws BindingException {
51 public Datatype getContentType(Object variant) throws BindingException {
56 public Binding getContentBinding(Object variant) throws BindingException {
61 public Object create(Binding contentBinding, Object content) throws BindingException {
66 public void setContent(Object variant, Binding contentBinding, Object content) throws BindingException {
71 public boolean isInstance(Object obj) {
76 public void assertInstaceIsValid(Object obj, Set<Object> validInstances) throws BindingException {
81 public int compare(Object o1, Object o2) throws org.simantics.databoard.binding.error.RuntimeBindingException {
86 return - System.identityHashCode(o2);
90 return System.identityHashCode(o1);
92 if(o1.equals(o2)) return 0;
93 return System.identityHashCode(o1) - System.identityHashCode(o2);
101 public List<String> getChildNames(Node node) throws NodeManagerException {
102 List<Node> children = getChildren(node);
103 ArrayList<String> names = new ArrayList<String>(children.size());
104 for(Node child : children)
105 names.add(getName(child));
110 public List<String> getPropertyNames(Node node) throws NodeManagerException {
111 List<Node> properties = getProperties(node);
112 ArrayList<String> names = new ArrayList<String>(properties.size());
113 for(Node property : properties)
114 names.add(getName(property));
119 public Object getValue(Node node, String propertyName, Binding binding)
120 throws NodeManagerException, BindingException {
121 Node property = getProperty(node, propertyName);
123 throw new NoSuchNodeException("Didn't find a property " + propertyName);
124 return getValue(property, binding);
128 public void setValue(Node node, String propertyName, Object value,
129 Binding binding) throws NodeManagerException, BindingException {
130 Node property = getProperty(node, propertyName);
132 throw new NoSuchNodeException("Didn't find a property " + propertyName);
133 setValue(property, value, binding);
137 public Variant getValue(Node node) throws NodeManagerException {
138 Datatype datatype = getDatatype(node);
139 Binding binding = datatype != null ? Bindings.getBinding(datatype) : NO_BINDING;
141 Object value = getValue(node, binding);
142 if (value instanceof Variant)
143 return (Variant)value;
145 return new Variant(binding, value);
146 } catch (BindingException e) {
147 // Shouldn't really happen
149 return new Variant();
154 public Variant getValue(Node node, String propertyName)
155 throws NodeManagerException {
156 Node property = getProperty(node, propertyName);
158 throw new NoSuchNodeException("Didn't find a property " + propertyName);
159 return getValue(property);
163 public String getPropertyURI(Node parent, Node property) {