-/*******************************************************************************\r
- * Copyright (c) 2013 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- * Semantum Oy - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.simulator.variable.impl;\r
-\r
-import java.util.ArrayList;\r
-import java.util.List;\r
-import java.util.Set;\r
-\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.databoard.binding.Binding;\r
-import org.simantics.databoard.binding.VariantBinding;\r
-import org.simantics.databoard.binding.error.BindingException;\r
-import org.simantics.databoard.binding.mutable.Variant;\r
-import org.simantics.databoard.type.Datatype;\r
-import org.simantics.simulator.variable.NodeManager;\r
-import org.simantics.simulator.variable.exceptions.NoSuchNodeException;\r
-import org.simantics.simulator.variable.exceptions.NodeManagerException;\r
-\r
-/**\r
- * AbstractNodeManager gives default implementations to some methods\r
- * of NodeManager.\r
- * \r
- * @author Hannu Niemistö\r
- */\r
-public abstract class AbstractNodeManager<Node> implements NodeManager<Node> {\r
- \r
-\r
- final static Binding NO_BINDING = new VariantBinding() {\r
-\r
- @Override\r
- public Object getContent(Object variant, Binding contentBinding) throws BindingException {\r
- throw new Error();\r
- }\r
-\r
- @Override\r
- public Object getContent(Object variant) throws BindingException {\r
- throw new Error();\r
- }\r
-\r
- @Override\r
- public Datatype getContentType(Object variant) throws BindingException {\r
- throw new Error();\r
- }\r
-\r
- @Override\r
- public Binding getContentBinding(Object variant) throws BindingException {\r
- throw new Error();\r
- }\r
-\r
- @Override\r
- public Object create(Binding contentBinding, Object content) throws BindingException {\r
- throw new Error();\r
- }\r
-\r
- @Override\r
- public void setContent(Object variant, Binding contentBinding, Object content) throws BindingException {\r
- throw new Error();\r
- }\r
-\r
- @Override\r
- public boolean isInstance(Object obj) {\r
- return true;\r
- }\r
-\r
- @Override\r
- public void assertInstaceIsValid(Object obj, Set<Object> validInstances) throws BindingException {\r
- throw new Error();\r
- }\r
- \r
- @Override\r
- public int compare(Object o1, Object o2) throws org.simantics.databoard.binding.error.RuntimeBindingException {\r
- if(o1 == null) {\r
- if(o2 == null) {\r
- return 0;\r
- } else {\r
- return - System.identityHashCode(o2);\r
- }\r
- } else {\r
- if(o2 == null) {\r
- return System.identityHashCode(o1);\r
- } else {\r
- if(o1.equals(o2)) return 0;\r
- return System.identityHashCode(o1) - System.identityHashCode(o2); \r
- }\r
- }\r
- }\r
-\r
- };\r
- \r
- @Override\r
- public List<String> getChildNames(Node node) throws NodeManagerException {\r
- List<Node> children = getChildren(node);\r
- ArrayList<String> names = new ArrayList<String>(children.size());\r
- for(Node child : children)\r
- names.add(getName(child));\r
- return names;\r
- }\r
- \r
- @Override\r
- public List<String> getPropertyNames(Node node) throws NodeManagerException {\r
- List<Node> properties = getProperties(node);\r
- ArrayList<String> names = new ArrayList<String>(properties.size());\r
- for(Node property : properties)\r
- names.add(getName(property));\r
- return names;\r
- }\r
- \r
- @Override\r
- public Object getValue(Node node, String propertyName, Binding binding)\r
- throws NodeManagerException, BindingException {\r
- Node property = getProperty(node, propertyName);\r
- if(property == null)\r
- throw new NoSuchNodeException("Didn't find a property " + propertyName);\r
- return getValue(property, binding);\r
- }\r
- \r
- @Override\r
- public void setValue(Node node, String propertyName, Object value,\r
- Binding binding) throws NodeManagerException, BindingException {\r
- Node property = getProperty(node, propertyName);\r
- if(property == null)\r
- throw new NoSuchNodeException("Didn't find a property " + propertyName);\r
- setValue(property, value, binding);\r
- }\r
- \r
- @Override\r
- public Variant getValue(Node node) throws NodeManagerException {\r
- Datatype datatype = getDatatype(node);\r
- Binding binding = datatype != null ? Bindings.getBinding(datatype) : NO_BINDING;\r
- try {\r
- Object value = getValue(node, binding);\r
- if (value instanceof Variant)\r
- return (Variant)value;\r
- else\r
- return new Variant(binding, value);\r
- } catch (BindingException e) {\r
- // Shouldn't really happen\r
- assert(false);\r
- return new Variant();\r
- }\r
- }\r
- \r
- @Override\r
- public Variant getValue(Node node, String propertyName)\r
- throws NodeManagerException {\r
- Node property = getProperty(node, propertyName);\r
- if(property == null)\r
- throw new NoSuchNodeException("Didn't find a property " + propertyName);\r
- return getValue(property);\r
- }\r
- \r
- @Override\r
- public String getPropertyURI(Node parent, Node property) {\r
- return null;\r
- } \r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2013 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ * Semantum Oy - initial API and implementation
+ *******************************************************************************/
+package org.simantics.simulator.variable.impl;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.databoard.binding.Binding;
+import org.simantics.databoard.binding.VariantBinding;
+import org.simantics.databoard.binding.error.BindingException;
+import org.simantics.databoard.binding.mutable.Variant;
+import org.simantics.databoard.type.Datatype;
+import org.simantics.simulator.variable.NodeManager;
+import org.simantics.simulator.variable.exceptions.NoSuchNodeException;
+import org.simantics.simulator.variable.exceptions.NodeManagerException;
+
+/**
+ * AbstractNodeManager gives default implementations to some methods
+ * of NodeManager.
+ *
+ * @author Hannu Niemistö
+ */
+public abstract class AbstractNodeManager<Node> implements NodeManager<Node> {
+
+
+ final static Binding NO_BINDING = new VariantBinding() {
+
+ @Override
+ public Object getContent(Object variant, Binding contentBinding) throws BindingException {
+ throw new Error();
+ }
+
+ @Override
+ public Object getContent(Object variant) throws BindingException {
+ throw new Error();
+ }
+
+ @Override
+ public Datatype getContentType(Object variant) throws BindingException {
+ throw new Error();
+ }
+
+ @Override
+ public Binding getContentBinding(Object variant) throws BindingException {
+ throw new Error();
+ }
+
+ @Override
+ public Object create(Binding contentBinding, Object content) throws BindingException {
+ throw new Error();
+ }
+
+ @Override
+ public void setContent(Object variant, Binding contentBinding, Object content) throws BindingException {
+ throw new Error();
+ }
+
+ @Override
+ public boolean isInstance(Object obj) {
+ return true;
+ }
+
+ @Override
+ public void assertInstaceIsValid(Object obj, Set<Object> validInstances) throws BindingException {
+ throw new Error();
+ }
+
+ @Override
+ public int compare(Object o1, Object o2) throws org.simantics.databoard.binding.error.RuntimeBindingException {
+ if(o1 == null) {
+ if(o2 == null) {
+ return 0;
+ } else {
+ return - System.identityHashCode(o2);
+ }
+ } else {
+ if(o2 == null) {
+ return System.identityHashCode(o1);
+ } else {
+ if(o1.equals(o2)) return 0;
+ return System.identityHashCode(o1) - System.identityHashCode(o2);
+ }
+ }
+ }
+
+ };
+
+ @Override
+ public List<String> getChildNames(Node node) throws NodeManagerException {
+ List<Node> children = getChildren(node);
+ ArrayList<String> names = new ArrayList<String>(children.size());
+ for(Node child : children)
+ names.add(getName(child));
+ return names;
+ }
+
+ @Override
+ public List<String> getPropertyNames(Node node) throws NodeManagerException {
+ List<Node> properties = getProperties(node);
+ ArrayList<String> names = new ArrayList<String>(properties.size());
+ for(Node property : properties)
+ names.add(getName(property));
+ return names;
+ }
+
+ @Override
+ public Object getValue(Node node, String propertyName, Binding binding)
+ throws NodeManagerException, BindingException {
+ Node property = getProperty(node, propertyName);
+ if(property == null)
+ throw new NoSuchNodeException("Didn't find a property " + propertyName);
+ return getValue(property, binding);
+ }
+
+ @Override
+ public void setValue(Node node, String propertyName, Object value,
+ Binding binding) throws NodeManagerException, BindingException {
+ Node property = getProperty(node, propertyName);
+ if(property == null)
+ throw new NoSuchNodeException("Didn't find a property " + propertyName);
+ setValue(property, value, binding);
+ }
+
+ @Override
+ public Variant getValue(Node node) throws NodeManagerException {
+ Datatype datatype = getDatatype(node);
+ Binding binding = datatype != null ? Bindings.getBinding(datatype) : NO_BINDING;
+ try {
+ Object value = getValue(node, binding);
+ if (value instanceof Variant)
+ return (Variant)value;
+ else
+ return new Variant(binding, value);
+ } catch (BindingException e) {
+ // Shouldn't really happen
+ assert(false);
+ return new Variant();
+ }
+ }
+
+ @Override
+ public Variant getValue(Node node, String propertyName)
+ throws NodeManagerException {
+ Node property = getProperty(node, propertyName);
+ if(property == null)
+ throw new NoSuchNodeException("Didn't find a property " + propertyName);
+ return getValue(property);
+ }
+
+ @Override
+ public String getPropertyURI(Node parent, Node property) {
+ return null;
+ }
+
+}