-package org.simantics.structural.synchronization.base;\r
-\r
-import gnu.trove.map.hash.THashMap;\r
-import gnu.trove.procedure.TObjectObjectProcedure;\r
-import gnu.trove.procedure.TObjectProcedure;\r
-\r
-import java.io.PrintWriter;\r
-import java.util.Collection;\r
-import java.util.Collections;\r
-import java.util.concurrent.atomic.AtomicReference;\r
-\r
-import org.simantics.databoard.annotations.Optional;\r
-\r
-abstract public class ComponentBase<T extends ComponentBase<T>> {\r
-\r
- public int componentId;\r
- \r
- public String uid;\r
- protected transient T parent;\r
- \r
- @Optional public String solverComponentName;\r
- public boolean attached;\r
- \r
- public ComponentBase() {\r
- }\r
-\r
- public ComponentBase(String uid) {\r
- this.uid = uid;\r
- }\r
- \r
- public ComponentBase(String uid, int moduleId) {\r
- this.uid = uid;\r
- this.componentId = moduleId;\r
- }\r
-\r
- public ComponentBase(int moduleId) {\r
- this.componentId = moduleId;\r
- }\r
- \r
- /**\r
- * Detaches a child by its UID.\r
- */\r
- public void detachByUid(final String uid) {\r
- final AtomicReference<String> nameRef = new AtomicReference<String>();\r
- getChildMap().forEachEntry(new TObjectObjectProcedure<String, T>() {\r
- @Override\r
- public boolean execute(String name, T conf) {\r
- if(conf.uid.equals(uid)) {\r
- nameRef.set(name);\r
- return false;\r
- }\r
- else\r
- return true;\r
- }\r
- });\r
- String name = nameRef.get();\r
- getChildMap().remove(name);\r
- }\r
- \r
- public Collection<T> getChildren() {\r
- if(getChildMap() == null)\r
- return Collections.emptyList();\r
- else\r
- return getChildMap().values();\r
- }\r
- \r
- public T getChild(String name) {\r
- if(getChildMap() == null)\r
- return null;\r
- else\r
- return getChildMap().get(name);\r
- }\r
-\r
- public THashMap<String, T> setChildMapAndReturnOld(\r
- THashMap<String, T> newChildMap) {\r
- THashMap<String, T> oldChildMap = getChildMap();\r
- setChildMap(newChildMap);\r
- return oldChildMap;\r
- }\r
- \r
- public int getModuleId() {\r
- return componentId;\r
- }\r
-\r
- public String getUid() {\r
- return uid;\r
- }\r
- \r
- public void setModuleId(int moduleId) {\r
- this.componentId = moduleId;\r
- }\r
- \r
- public void setUid(String uid) {\r
- this.uid = uid;\r
- }\r
- \r
- public boolean isComposite() {\r
- return getChildMap() != null;\r
- }\r
-\r
- public void printConfiguration(final int indentation) {\r
- printConfiguration(new PrintWriter(System.out), indentation);\r
- }\r
-\r
- public void printConfiguration(final PrintWriter out, final int indentation) {\r
- out.println(uid + " " + solverComponentName + "(" + componentId + ")");\r
- if(getChildMap() != null)\r
- getChildMap().forEachEntry(new TObjectObjectProcedure<String, T>() {\r
- @Override\r
- public boolean execute(String a, T b) {\r
- for(int i=0;i<indentation;++i)\r
- out.print(" ");\r
- out.print(a + " ");\r
- b.printConfiguration(out, indentation+1);\r
- return true;\r
- }\r
- });\r
- }\r
- \r
- public void clearParent() {\r
- this.parent = null;\r
- }\r
- \r
- public T getParent() {\r
- return parent;\r
- }\r
- \r
- public void mapModuleIds(final int[] idMap) {\r
- if(componentId > 0)\r
- componentId = idMap[componentId];\r
- if(getChildMap() != null)\r
- getChildMap().forEachValue(new TObjectProcedure<T>() {\r
- @Override\r
- public boolean execute(T component) {\r
- component.mapModuleIds(idMap);\r
- return true;\r
- }\r
- });\r
- }\r
- \r
- abstract public THashMap<String,T> getChildMap();\r
- abstract public void setChildMap(THashMap<String, T> newChildMap);\r
-\r
- public int componentHashCode() {\r
- return hashCode();\r
-// final int prime = 31;\r
-// int result = 1;\r
-// result = prime * result + ((parent == null) ? 0 : parent.hashCode());\r
-// result = prime * result + ((uid == null) ? 0 : uid.hashCode());\r
-// result = prime * result + getChildren().hashCode();\r
-// return result;\r
- }\r
-\r
- public boolean componentEquals(Object obj) {\r
- return this.equals(obj);\r
-// if (this == obj)\r
-// return true;\r
-// if (obj == null)\r
-// return false;\r
-// if (getClass() != obj.getClass())\r
-// return false;\r
-// ComponentBase<?> other = (ComponentBase<?>) obj;\r
-// if (parent == null) {\r
-// if (other.parent != null)\r
-// return false;\r
-// } else if (!parent.equals(other.parent))\r
-// return false;\r
-// if (uid == null) {\r
-// if (other.uid != null)\r
-// return false;\r
-// } else if (!uid.equals(other.uid))\r
-// return false;\r
-// \r
-// return getChildren().equals(other.getChildren());\r
-// \r
-// //return true;\r
-// \r
- }\r
-\r
-}\r
+package org.simantics.structural.synchronization.base;
+
+import gnu.trove.map.hash.THashMap;
+import gnu.trove.procedure.TObjectObjectProcedure;
+import gnu.trove.procedure.TObjectProcedure;
+
+import java.io.PrintWriter;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.simantics.databoard.annotations.Optional;
+
+abstract public class ComponentBase<T extends ComponentBase<T>> {
+
+ public int componentId;
+
+ public String uid;
+ protected transient T parent;
+
+ @Optional public String solverComponentName;
+ public boolean attached;
+
+ public ComponentBase() {
+ }
+
+ public ComponentBase(String uid) {
+ this.uid = uid;
+ }
+
+ public ComponentBase(String uid, int moduleId) {
+ this.uid = uid;
+ this.componentId = moduleId;
+ }
+
+ public ComponentBase(int moduleId) {
+ this.componentId = moduleId;
+ }
+
+ /**
+ * Detaches a child by its UID.
+ */
+ public void detachByUid(final String uid) {
+ final AtomicReference<String> nameRef = new AtomicReference<String>();
+ getChildMap().forEachEntry(new TObjectObjectProcedure<String, T>() {
+ @Override
+ public boolean execute(String name, T conf) {
+ if(conf.uid.equals(uid)) {
+ nameRef.set(name);
+ return false;
+ }
+ else
+ return true;
+ }
+ });
+ String name = nameRef.get();
+ getChildMap().remove(name);
+ }
+
+ public Collection<T> getChildren() {
+ if(getChildMap() == null)
+ return Collections.emptyList();
+ else
+ return getChildMap().values();
+ }
+
+ public T getChild(String name) {
+ if(getChildMap() == null)
+ return null;
+ else
+ return getChildMap().get(name);
+ }
+
+ public THashMap<String, T> setChildMapAndReturnOld(
+ THashMap<String, T> newChildMap) {
+ THashMap<String, T> oldChildMap = getChildMap();
+ setChildMap(newChildMap);
+ return oldChildMap;
+ }
+
+ public int getModuleId() {
+ return componentId;
+ }
+
+ public String getUid() {
+ return uid;
+ }
+
+ public void setModuleId(int moduleId) {
+ this.componentId = moduleId;
+ }
+
+ public void setUid(String uid) {
+ this.uid = uid;
+ }
+
+ public boolean isComposite() {
+ return getChildMap() != null;
+ }
+
+ public void printConfiguration(final int indentation) {
+ printConfiguration(new PrintWriter(System.out), indentation);
+ }
+
+ public void printConfiguration(final PrintWriter out, final int indentation) {
+ out.println(uid + " " + solverComponentName + "(" + componentId + ")");
+ if(getChildMap() != null)
+ getChildMap().forEachEntry(new TObjectObjectProcedure<String, T>() {
+ @Override
+ public boolean execute(String a, T b) {
+ for(int i=0;i<indentation;++i)
+ out.print(" ");
+ out.print(a + " ");
+ b.printConfiguration(out, indentation+1);
+ return true;
+ }
+ });
+ }
+
+ public void clearParent() {
+ this.parent = null;
+ }
+
+ public T getParent() {
+ return parent;
+ }
+
+ public void mapModuleIds(final int[] idMap) {
+ if(componentId > 0)
+ componentId = idMap[componentId];
+ if(getChildMap() != null)
+ getChildMap().forEachValue(new TObjectProcedure<T>() {
+ @Override
+ public boolean execute(T component) {
+ component.mapModuleIds(idMap);
+ return true;
+ }
+ });
+ }
+
+ abstract public THashMap<String,T> getChildMap();
+ abstract public void setChildMap(THashMap<String, T> newChildMap);
+
+ public int componentHashCode() {
+ return hashCode();
+// final int prime = 31;
+// int result = 1;
+// result = prime * result + ((parent == null) ? 0 : parent.hashCode());
+// result = prime * result + ((uid == null) ? 0 : uid.hashCode());
+// result = prime * result + getChildren().hashCode();
+// return result;
+ }
+
+ public boolean componentEquals(Object obj) {
+ return this.equals(obj);
+// if (this == obj)
+// return true;
+// if (obj == null)
+// return false;
+// if (getClass() != obj.getClass())
+// return false;
+// ComponentBase<?> other = (ComponentBase<?>) obj;
+// if (parent == null) {
+// if (other.parent != null)
+// return false;
+// } else if (!parent.equals(other.parent))
+// return false;
+// if (uid == null) {
+// if (other.uid != null)
+// return false;
+// } else if (!uid.equals(other.uid))
+// return false;
+//
+// return getChildren().equals(other.getChildren());
+//
+// //return true;
+//
+ }
+
+}