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