]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/utils/ListUtils.java
LabelDecorator.decorateLabel can return null
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / utils / ListUtils.java
1 package org.simantics.db.common.utils;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Set;
6
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.WriteGraph;
10 import org.simantics.db.WriteOnlyGraph;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.layer0.Layer0;
13
14 public class ListUtils {
15
16     /**
17      * Inserts {@code elements} between list nodes 
18      * {@code before} and {@code after}.
19      */
20     private static void insertBetween(
21             WriteGraph g, Layer0 L0, Resource list,
22             Resource before, Resource after, 
23             Iterable<Resource> elements) throws DatabaseException {
24         for(Resource item : elements) {
25             Resource cur = g.newResource();
26             g.claim(cur, L0.InstanceOf, null, L0.List_Entry);
27             g.claim(cur, L0.IsOwnedBy, L0.IsComposedOf, list);
28             g.claim(before, L0.List_Next, L0.List_Previous, cur);
29             g.claim(cur, L0.List_Element, item);
30             before = cur;
31         }  
32         g.claim(before, L0.List_Next, L0.List_Previous, after);
33     }
34
35     private static void insertBetween(
36             WriteGraph g, Layer0 L0, Resource list, 
37             Resource before, Resource after, 
38             Resource[] elements) throws DatabaseException {
39         for(Resource item : elements) {
40             Resource cur = g.newResource();
41             g.claim(cur, L0.InstanceOf, null, L0.List_Entry);
42             g.claim(cur, L0.IsOwnedBy, L0.IsComposedOf, list);
43             g.claim(before, L0.List_Next, L0.List_Previous, cur);
44             g.claim(cur, L0.List_Element, item);
45             before = cur;
46         }  
47         g.claim(before, L0.List_Next, L0.List_Previous, after);
48     }
49     
50     /**
51      * Creates a list containing the givens {@code elements}.
52      */
53     public static Resource create(WriteGraph g, Iterable<Resource> elements) throws DatabaseException {
54         Layer0 L0 = Layer0.getInstance(g);
55         
56         Resource list = g.newResource();
57         g.claim(list, L0.InstanceOf, L0.List);
58         
59         insertBetween(g, L0, list, list, list, elements);
60         return list;
61     }
62
63     /**
64      * Creates a list containing the givens {@code elements}.
65      */
66     public static Resource create(WriteGraph g, Resource type, Iterable<Resource> elements) throws DatabaseException {
67         Layer0 L0 = Layer0.getInstance(g);
68         
69         Resource list = g.newResource();
70         g.claim(list, L0.InstanceOf, null, type);
71         
72         insertBetween(g, L0, list, list, list, elements);
73         return list;
74     }
75
76     public static Resource create(WriteGraph g, Resource type, Resource ... elements) throws DatabaseException {
77         Layer0 L0 = Layer0.getInstance(g);
78         
79         Resource list = g.newResource();
80         g.claim(list, L0.InstanceOf, null, type);
81         
82         insertBetween(g, L0, list, list, list, elements);
83         return list;
84     }
85     
86     /**
87      * Inserts given {@code elements} into the front of the list.
88      */
89     public static void insertFront(WriteGraph g, Resource list, Iterable<Resource> elements) throws DatabaseException {
90         if (!elements.iterator().hasNext())
91             return;
92         Layer0 L0 = Layer0.getInstance(g);
93         
94         Resource first = g.getSingleObject(list, L0.List_Next);        
95         g.deny(list, L0.List_Next, L0.List_Previous, first);        
96         insertBetween(g, L0, list, list, first, elements);
97     }
98     
99     public static void createExisting(WriteOnlyGraph g, Resource list, Iterable<Resource> elements) throws DatabaseException {
100     
101         Layer0 L0 = g.getService(Layer0.class);
102         Resource before = list;
103         for(Resource item : elements) {
104             Resource cur = g.newResource();
105             g.claim(cur, L0.InstanceOf, null, L0.List_Entry);
106             g.claim(cur, L0.IsOwnedBy, L0.IsComposedOf, list);
107             g.claim(before, L0.List_Next, L0.List_Previous, cur);
108             g.claim(cur, L0.List_Element, null, item);
109             before = cur;
110         }  
111         g.claim(before, L0.List_Next, L0.List_Previous, list);
112         
113     }
114     
115     /**
116      * Inserts given {@code elements} into the back of the list.
117      */
118     public static void insertBack(WriteGraph g, Resource list, Iterable<Resource> elements) throws DatabaseException {
119         if (!elements.iterator().hasNext())
120             return;
121         Layer0 L0 = Layer0.getInstance(g);
122         
123         Resource last = g.getSingleObject(list, L0.List_Previous);        
124         g.deny(last, L0.List_Next, L0.List_Previous, list);        
125         insertBetween(g, L0, list, last, list, elements);
126     }
127     
128     /**
129      * Replaces a given element in the a given list
130      * @param g WriteGraph
131      * @param list List resource
132      * @param element Element to be replaced
133      * @param replacement Resource that replaces element
134      * @return true if successful replacement, false otherwise
135      * @throws DatabaseException
136      */
137     public static boolean replace(WriteGraph g, Resource list, Resource element, Resource replacement) throws DatabaseException {
138         if(list == null || element == null || replacement == null)
139             return false;
140         
141         Layer0 L0 = Layer0.getInstance(g);
142
143         Resource node = getNode(g, list, element);
144         if(node != null) {
145             g.deny(node, L0.List_Element);
146             g.claim(node, L0.List_Element, replacement);
147             return true;
148         } else {
149             return false;
150         }
151     }
152     
153     private static void toList(ReadGraph g, List<Resource> result, Resource list) throws DatabaseException {
154         
155         Layer0 L0 = Layer0.getInstance(g);
156         
157         Resource cur = g.getSingleObject(list, L0.List_Next);
158         while(!cur.equals(list)) {
159             Resource el = g.getPossibleObject(cur, L0.List_Element);
160             if(el != null)
161                 result.add(el);
162             cur = g.getSingleObject(cur, L0.List_Next);
163         }
164         
165     }
166     
167     /**
168      * Converts a list in the graph to a Java List.
169      */
170     public static List<Resource> toList(ReadGraph g, Resource list) throws DatabaseException {
171         ArrayList<Resource> result = new ArrayList<Resource>();
172         toList(g, result, list);
173         return result;
174     }
175
176     public static List<Resource> toPossibleList(ReadGraph g, Resource list) throws DatabaseException {
177         try {
178                 return toList(g, list);
179         } catch (DatabaseException e) {
180                 return null;
181         }
182     }
183
184     /**
185      * Removes an element from a list. Returns true if actually removed something.
186      */
187     public static boolean removeElement(WriteGraph g, Resource list, Resource element) throws DatabaseException {
188         Layer0 L0 = Layer0.getInstance(g);
189         Resource prev = list;
190         Resource cur = g.getSingleObject(list, L0.List_Next);
191         while(!cur.equals(list)) {
192             Resource el = g.getPossibleObject(cur, L0.List_Element);
193             Resource next = g.getSingleObject(cur, L0.List_Next);
194             if(element.equals(el)) {                
195                 g.deny(cur);
196                 g.claim(prev, L0.List_Next, next);
197                 return true;
198             }
199             prev = cur;
200             cur = next;
201         }
202         return false;
203     }
204     
205     /**
206      * Removes an elements from a list. Returns true if actually removed something.
207      */
208     public static boolean removeElements(WriteGraph g, Resource list, Set<Resource> elements) throws DatabaseException {
209         Layer0 L0 = Layer0.getInstance(g);
210         Resource prev = list;
211         Resource cur = g.getSingleObject(list, L0.List_Next);
212         boolean removed = false;
213         while(!cur.equals(list)) {
214             Resource el = g.getPossibleObject(cur, L0.List_Element);
215             Resource next = g.getSingleObject(cur, L0.List_Next);
216             if(elements.contains(el)) {                
217                 g.deny(cur);
218                 g.claim(prev, L0.List_Next, next);
219                 removed = true;
220             }
221             else
222                 prev = cur;
223             cur = next;
224         }
225         return removed;
226     }
227     
228     private static void getListNodes(ReadGraph g, List<Resource> result, Resource list) throws DatabaseException {
229         
230         Layer0 L0 = Layer0.getInstance(g);
231         
232         Resource cur = g.getSingleObject(list, L0.List_Next);
233         while(!cur.equals(list)) {
234             result.add(cur);
235             cur = g.getSingleObject(cur, L0.List_Next);
236         }
237         
238     }
239     
240     public static List<Resource> getListNodes(ReadGraph g, Resource list) throws DatabaseException {
241         ArrayList<Resource> result = new ArrayList<Resource>();
242         getListNodes(g, result, list);
243         return result;
244     }
245     
246     public static Resource getNode(ReadGraph g, Resource list, Resource element) throws DatabaseException {
247         Layer0 L0 = Layer0.getInstance(g);
248         
249         Resource cur = g.getSingleObject(list, L0.List_Next);
250         while(!cur.equals(list)) {
251             Resource el = g.getPossibleObject(cur, L0.List_Element);
252             if(element.equals(el))
253                 return cur;
254             cur = g.getSingleObject(cur, L0.List_Next);
255         }
256         return null;
257     }     
258     
259     public static boolean swapWithPrevious(WriteGraph g, Resource list, Resource element) throws DatabaseException {
260         Resource node = getNode(g, list, element);
261         if(node == null)
262             return false;
263         Layer0 L0 = Layer0.getInstance(g);
264         Resource prev = g.getSingleObject(node, L0.List_Previous);
265         if(list.equals(prev))
266             return false;
267         swap(g, node, prev);
268         return true;
269     }
270     
271     public static boolean swapWithNext(WriteGraph g, Resource list, Resource element) throws DatabaseException {
272         Resource node = getNode(g, list, element);
273         if(node == null)
274             return false;
275         Layer0 L0 = Layer0.getInstance(g);
276         Resource next = g.getSingleObject(node, L0.List_Next);
277         if(list.equals(next))
278             return false;
279         swap(g, node, next);
280         return true;
281     }
282
283     private static void swap(WriteGraph g, Resource a, Resource b) throws DatabaseException {
284         Layer0 L0 = Layer0.getInstance(g);
285         Resource ea = g.getPossibleObject(a, L0.List_Element);
286         Resource eb = g.getPossibleObject(b, L0.List_Element);
287         
288         g.deny(a, L0.List_Element);
289         g.deny(b, L0.List_Element);
290         
291         if(eb != null)
292             g.claim(a, L0.List_Element, eb);
293         if(ea != null)
294             g.claim(b, L0.List_Element, ea);
295     }
296
297     
298 }