]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.simulator.variable/src/org/simantics/simulator/variable/impl/AbstractNodeManager.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.simulator.variable / src / org / simantics / simulator / variable / impl / AbstractNodeManager.java
1 /*******************************************************************************
2  * Copyright (c) 2013 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
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;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Set;
18
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;
28
29 /**
30  * AbstractNodeManager gives default implementations to some methods
31  * of NodeManager.
32  * 
33  * @author Hannu Niemistö
34  */
35 public abstract class AbstractNodeManager<Node> implements NodeManager<Node> {
36         
37
38     final static Binding NO_BINDING = new VariantBinding() {
39
40         @Override
41         public Object getContent(Object variant, Binding contentBinding) throws BindingException {
42             throw new Error();
43         }
44
45         @Override
46         public Object getContent(Object variant) throws BindingException {
47             throw new Error();
48         }
49
50         @Override
51         public Datatype getContentType(Object variant) throws BindingException {
52             throw new Error();
53         }
54
55         @Override
56         public Binding getContentBinding(Object variant) throws BindingException {
57             throw new Error();
58         }
59
60         @Override
61         public Object create(Binding contentBinding, Object content) throws BindingException {
62             throw new Error();
63         }
64
65         @Override
66         public void setContent(Object variant, Binding contentBinding, Object content) throws BindingException {
67             throw new Error();
68         }
69
70         @Override
71         public boolean isInstance(Object obj) {
72             return true;
73         }
74
75         @Override
76         public void assertInstaceIsValid(Object obj, Set<Object> validInstances) throws BindingException {
77             throw new Error();
78         }
79         
80         @Override
81         public int compare(Object o1, Object o2) throws org.simantics.databoard.binding.error.RuntimeBindingException {
82                 if(o1 == null) {
83                         if(o2 == null) {
84                                 return 0;
85                         } else {
86                                 return  - System.identityHashCode(o2);
87                         }
88                 } else {
89                         if(o2 == null) {
90                                 return  System.identityHashCode(o1);
91                         } else {
92                                 if(o1.equals(o2)) return 0;
93                                 return System.identityHashCode(o1) - System.identityHashCode(o2);       
94                         }
95                 }
96         }
97
98     };
99     
100         @Override
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));
106                 return names;
107         }
108         
109         @Override
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));
115                 return names;
116         }
117         
118         @Override
119         public Object getValue(Node node, String propertyName, Binding binding)
120                         throws NodeManagerException, BindingException {
121                 Node property = getProperty(node, propertyName);
122                 if(property == null)
123                         throw new NoSuchNodeException("Didn't find a property " + propertyName);
124                 return getValue(property, binding);
125         }
126         
127         @Override
128         public void setValue(Node node, String propertyName, Object value,
129                         Binding binding) throws NodeManagerException, BindingException {
130                 Node property = getProperty(node, propertyName);
131                 if(property == null)
132                         throw new NoSuchNodeException("Didn't find a property " + propertyName);
133                 setValue(property, value, binding);
134         }
135         
136         @Override
137         public Variant getValue(Node node) throws NodeManagerException {
138                 Datatype datatype = getDatatype(node);
139                 Binding binding = datatype != null ? Bindings.getBinding(datatype) : NO_BINDING;
140                 try {
141                         Object value = getValue(node, binding);
142                         if (value instanceof Variant)
143                                 return (Variant)value;
144                         else
145                                 return new Variant(binding, value);
146                 } catch (BindingException e) {
147                         // Shouldn't really happen
148                         assert(false);
149                         return new Variant();
150                 }
151         }
152         
153         @Override
154         public Variant getValue(Node node, String propertyName)
155                         throws NodeManagerException {
156                 Node property = getProperty(node, propertyName);
157                 if(property == null)
158                         throw new NoSuchNodeException("Didn't find a property " + propertyName);
159                 return getValue(property);
160         }
161         
162     @Override
163     public String getPropertyURI(Node parent, Node property) {
164         return null;
165     }   
166
167 }