1 package org.simantics.structural.synchronization.base;
\r
3 import gnu.trove.map.hash.THashMap;
\r
4 import gnu.trove.procedure.TObjectObjectProcedure;
\r
5 import gnu.trove.procedure.TObjectProcedure;
\r
7 import java.io.PrintWriter;
\r
8 import java.util.Collection;
\r
9 import java.util.Collections;
\r
10 import java.util.concurrent.atomic.AtomicReference;
\r
12 import org.simantics.databoard.annotations.Optional;
\r
14 abstract public class ComponentBase<T extends ComponentBase<T>> {
\r
16 public int componentId;
\r
19 protected transient T parent;
\r
21 @Optional public String solverComponentName;
\r
22 public boolean attached;
\r
24 public ComponentBase() {
\r
27 public ComponentBase(String uid) {
\r
31 public ComponentBase(String uid, int moduleId) {
\r
33 this.componentId = moduleId;
\r
36 public ComponentBase(int moduleId) {
\r
37 this.componentId = moduleId;
\r
41 * Detaches a child by its UID.
\r
43 public void detachByUid(final String uid) {
\r
44 final AtomicReference<String> nameRef = new AtomicReference<String>();
\r
45 getChildMap().forEachEntry(new TObjectObjectProcedure<String, T>() {
\r
47 public boolean execute(String name, T conf) {
\r
48 if(conf.uid.equals(uid)) {
\r
56 String name = nameRef.get();
\r
57 getChildMap().remove(name);
\r
60 public Collection<T> getChildren() {
\r
61 if(getChildMap() == null)
\r
62 return Collections.emptyList();
\r
64 return getChildMap().values();
\r
67 public T getChild(String name) {
\r
68 if(getChildMap() == null)
\r
71 return getChildMap().get(name);
\r
74 public THashMap<String, T> setChildMapAndReturnOld(
\r
75 THashMap<String, T> newChildMap) {
\r
76 THashMap<String, T> oldChildMap = getChildMap();
\r
77 setChildMap(newChildMap);
\r
81 public int getModuleId() {
\r
85 public String getUid() {
\r
89 public void setModuleId(int moduleId) {
\r
90 this.componentId = moduleId;
\r
93 public void setUid(String uid) {
\r
97 public boolean isComposite() {
\r
98 return getChildMap() != null;
\r
101 public void printConfiguration(final int indentation) {
\r
102 printConfiguration(new PrintWriter(System.out), indentation);
\r
105 public void printConfiguration(final PrintWriter out, final int indentation) {
\r
106 out.println(uid + " " + solverComponentName + "(" + componentId + ")");
\r
107 if(getChildMap() != null)
\r
108 getChildMap().forEachEntry(new TObjectObjectProcedure<String, T>() {
\r
110 public boolean execute(String a, T b) {
\r
111 for(int i=0;i<indentation;++i)
\r
113 out.print(a + " ");
\r
114 b.printConfiguration(out, indentation+1);
\r
120 public void clearParent() {
\r
121 this.parent = null;
\r
124 public T getParent() {
\r
128 public void mapModuleIds(final int[] idMap) {
\r
129 if(componentId > 0)
\r
130 componentId = idMap[componentId];
\r
131 if(getChildMap() != null)
\r
132 getChildMap().forEachValue(new TObjectProcedure<T>() {
\r
134 public boolean execute(T component) {
\r
135 component.mapModuleIds(idMap);
\r
141 abstract public THashMap<String,T> getChildMap();
\r
142 abstract public void setChildMap(THashMap<String, T> newChildMap);
\r
144 public int componentHashCode() {
\r
146 // final int prime = 31;
\r
148 // result = prime * result + ((parent == null) ? 0 : parent.hashCode());
\r
149 // result = prime * result + ((uid == null) ? 0 : uid.hashCode());
\r
150 // result = prime * result + getChildren().hashCode();
\r
154 public boolean componentEquals(Object obj) {
\r
155 return this.equals(obj);
\r
156 // if (this == obj)
\r
158 // if (obj == null)
\r
160 // if (getClass() != obj.getClass())
\r
162 // ComponentBase<?> other = (ComponentBase<?>) obj;
\r
163 // if (parent == null) {
\r
164 // if (other.parent != null)
\r
166 // } else if (!parent.equals(other.parent))
\r
168 // if (uid == null) {
\r
169 // if (other.uid != null)
\r
171 // } else if (!uid.equals(other.uid))
\r
174 // return getChildren().equals(other.getChildren());
\r