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