]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural.synchronization.client/src/org/simantics/structural/synchronization/base/ComponentBase.java
Sync git svn branch with SVN repository r33366.
[simantics/platform.git] / bundles / org.simantics.structural.synchronization.client / src / org / simantics / structural / synchronization / base / ComponentBase.java
1 package org.simantics.structural.synchronization.base;\r
2 \r
3 import gnu.trove.map.hash.THashMap;\r
4 import gnu.trove.procedure.TObjectObjectProcedure;\r
5 import gnu.trove.procedure.TObjectProcedure;\r
6 \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
11 \r
12 import org.simantics.databoard.annotations.Optional;\r
13 \r
14 abstract public class ComponentBase<T extends ComponentBase<T>> {\r
15 \r
16     public int componentId;\r
17         \r
18     public String uid;\r
19     protected transient T parent;\r
20     \r
21     @Optional public String solverComponentName;\r
22     public boolean attached;\r
23     \r
24     public ComponentBase() {\r
25     }\r
26 \r
27     public ComponentBase(String uid) {\r
28         this.uid = uid;\r
29     }\r
30     \r
31     public ComponentBase(String uid, int moduleId) {\r
32         this.uid = uid;\r
33         this.componentId = moduleId;\r
34     }\r
35 \r
36     public ComponentBase(int moduleId) {\r
37         this.componentId = moduleId;\r
38     }\r
39     \r
40     /**\r
41      * Detaches a child by its UID.\r
42      */\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
46             @Override\r
47             public boolean execute(String name, T conf) {\r
48                 if(conf.uid.equals(uid)) {\r
49                     nameRef.set(name);\r
50                     return false;\r
51                 }\r
52                 else\r
53                     return true;\r
54             }\r
55         });\r
56         String name = nameRef.get();\r
57         getChildMap().remove(name);\r
58     }\r
59     \r
60     public Collection<T> getChildren() {\r
61         if(getChildMap() == null)\r
62             return Collections.emptyList();\r
63         else\r
64             return getChildMap().values();\r
65     }\r
66     \r
67         public T getChild(String name) {\r
68         if(getChildMap() == null)\r
69             return null;\r
70         else\r
71             return getChildMap().get(name);\r
72     }\r
73 \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
78         return oldChildMap;\r
79     }\r
80     \r
81     public int getModuleId() {\r
82         return componentId;\r
83     }\r
84 \r
85     public String getUid() {\r
86         return uid;\r
87     }\r
88     \r
89     public void setModuleId(int moduleId) {\r
90         this.componentId = moduleId;\r
91     }\r
92     \r
93     public void setUid(String uid) {\r
94         this.uid = uid;\r
95     }\r
96     \r
97     public boolean isComposite() {\r
98         return getChildMap() != null;\r
99     }\r
100 \r
101     public void printConfiguration(final int indentation) {\r
102         printConfiguration(new PrintWriter(System.out), indentation);\r
103     }\r
104 \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
109                 @Override\r
110                 public boolean execute(String a, T b) {\r
111                     for(int i=0;i<indentation;++i)\r
112                         out.print("    ");\r
113                     out.print(a + " ");\r
114                     b.printConfiguration(out, indentation+1);\r
115                     return true;\r
116                 }\r
117             });\r
118     }\r
119     \r
120     public void clearParent() {\r
121         this.parent = null;\r
122     }\r
123     \r
124     public T getParent() {\r
125         return parent;\r
126     }\r
127     \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
133                 @Override\r
134                 public boolean execute(T component) {\r
135                     component.mapModuleIds(idMap);\r
136                     return true;\r
137                 }\r
138             });\r
139     }\r
140         \r
141     abstract public THashMap<String,T> getChildMap();\r
142     abstract public void setChildMap(THashMap<String, T> newChildMap);\r
143 \r
144     public int componentHashCode() {\r
145         return hashCode();\r
146 //        final int prime = 31;\r
147 //        int result = 1;\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
151 //        return result;\r
152     }\r
153 \r
154     public boolean componentEquals(Object obj) {\r
155         return this.equals(obj);\r
156 //        if (this == obj)\r
157 //            return true;\r
158 //        if (obj == null)\r
159 //            return false;\r
160 //        if (getClass() != obj.getClass())\r
161 //            return false;\r
162 //        ComponentBase<?> other = (ComponentBase<?>) obj;\r
163 //        if (parent == null) {\r
164 //            if (other.parent != null)\r
165 //                return false;\r
166 //        } else if (!parent.equals(other.parent))\r
167 //            return false;\r
168 //        if (uid == null) {\r
169 //            if (other.uid != null)\r
170 //                return false;\r
171 //        } else if (!uid.equals(other.uid))\r
172 //            return false;\r
173 //        \r
174 //        return getChildren().equals(other.getChildren());\r
175 //        \r
176 //        //return true;\r
177 //        \r
178     }\r
179 \r
180 }\r