1 package org.simantics.db.common.utils;
3 import java.util.ArrayList;
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;
14 public class ListUtils {
17 * Inserts {@code elements} between list nodes
18 * {@code before} and {@code after}.
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);
32 g.claim(before, L0.List_Next, L0.List_Previous, after);
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);
47 g.claim(before, L0.List_Next, L0.List_Previous, after);
51 * Creates a list containing the givens {@code elements}.
53 public static Resource create(WriteGraph g, Iterable<Resource> elements) throws DatabaseException {
54 Layer0 L0 = Layer0.getInstance(g);
56 Resource list = g.newResource();
57 g.claim(list, L0.InstanceOf, L0.List);
59 insertBetween(g, L0, list, list, list, elements);
64 * Creates a list containing the givens {@code elements}.
66 public static Resource create(WriteGraph g, Resource type, Iterable<Resource> elements) throws DatabaseException {
67 Layer0 L0 = Layer0.getInstance(g);
69 Resource list = g.newResource();
70 g.claim(list, L0.InstanceOf, null, type);
72 insertBetween(g, L0, list, list, list, elements);
76 public static Resource create(WriteGraph g, Resource type, Resource ... elements) throws DatabaseException {
77 Layer0 L0 = Layer0.getInstance(g);
79 Resource list = g.newResource();
80 g.claim(list, L0.InstanceOf, null, type);
82 insertBetween(g, L0, list, list, list, elements);
87 * Inserts given {@code elements} into the front of the list.
89 public static void insertFront(WriteGraph g, Resource list, Iterable<Resource> elements) throws DatabaseException {
90 if (!elements.iterator().hasNext())
92 Layer0 L0 = Layer0.getInstance(g);
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);
99 public static void createExisting(WriteOnlyGraph g, Resource list, Iterable<Resource> elements) throws DatabaseException {
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);
111 g.claim(before, L0.List_Next, L0.List_Previous, list);
116 * Inserts given {@code elements} into the back of the list.
118 public static void insertBack(WriteGraph g, Resource list, Iterable<Resource> elements) throws DatabaseException {
119 if (!elements.iterator().hasNext())
121 Layer0 L0 = Layer0.getInstance(g);
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);
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
137 public static boolean replace(WriteGraph g, Resource list, Resource element, Resource replacement) throws DatabaseException {
138 if(list == null || element == null || replacement == null)
141 Layer0 L0 = Layer0.getInstance(g);
143 Resource node = getNode(g, list, element);
145 g.deny(node, L0.List_Element);
146 g.claim(node, L0.List_Element, replacement);
153 private static void toList(ReadGraph g, List<Resource> result, Resource list) throws DatabaseException {
155 Layer0 L0 = Layer0.getInstance(g);
157 Resource cur = g.getSingleObject(list, L0.List_Next);
158 while(!cur.equals(list)) {
159 Resource el = g.getPossibleObject(cur, L0.List_Element);
162 cur = g.getSingleObject(cur, L0.List_Next);
168 * Converts a list in the graph to a Java List.
170 public static List<Resource> toList(ReadGraph g, Resource list) throws DatabaseException {
171 ArrayList<Resource> result = new ArrayList<Resource>();
172 toList(g, result, list);
176 public static List<Resource> toPossibleList(ReadGraph g, Resource list) throws DatabaseException {
178 return toList(g, list);
179 } catch (DatabaseException e) {
185 * Removes an element from a list. Returns true if actually removed something.
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)) {
196 g.claim(prev, L0.List_Next, next);
206 * Removes an elements from a list. Returns true if actually removed something.
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)) {
218 g.claim(prev, L0.List_Next, next);
228 private static void getListNodes(ReadGraph g, List<Resource> result, Resource list) throws DatabaseException {
230 Layer0 L0 = Layer0.getInstance(g);
232 Resource cur = g.getSingleObject(list, L0.List_Next);
233 while(!cur.equals(list)) {
235 cur = g.getSingleObject(cur, L0.List_Next);
240 public static List<Resource> getListNodes(ReadGraph g, Resource list) throws DatabaseException {
241 ArrayList<Resource> result = new ArrayList<Resource>();
242 getListNodes(g, result, list);
246 public static Resource getNode(ReadGraph g, Resource list, Resource element) throws DatabaseException {
247 Layer0 L0 = Layer0.getInstance(g);
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))
254 cur = g.getSingleObject(cur, L0.List_Next);
259 public static boolean swapWithPrevious(WriteGraph g, Resource list, Resource element) throws DatabaseException {
260 Resource node = getNode(g, list, element);
263 Layer0 L0 = Layer0.getInstance(g);
264 Resource prev = g.getSingleObject(node, L0.List_Previous);
265 if(list.equals(prev))
271 public static boolean swapWithNext(WriteGraph g, Resource list, Resource element) throws DatabaseException {
272 Resource node = getNode(g, list, element);
275 Layer0 L0 = Layer0.getInstance(g);
276 Resource next = g.getSingleObject(node, L0.List_Next);
277 if(list.equals(next))
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);
288 g.deny(a, L0.List_Element);
289 g.deny(b, L0.List_Element);
292 g.claim(a, L0.List_Element, eb);
294 g.claim(b, L0.List_Element, ea);