]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.common/src/org/simantics/db/common/utils/ListUtils.java
ListUtils.create(g,elements) creates a list without element inverses
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / utils / ListUtils.java
index 4d6518524d2ad1c31ebe98f890d17d975743ce4c..656dfeb27550b8acfe8e1f1c8801cbebefe6e879 100644 (file)
@@ -1,6 +1,7 @@
 package org.simantics.db.common.utils;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 import java.util.Set;
 
@@ -53,15 +54,15 @@ public class ListUtils {
      * Creates a list containing the given {@code elements}.
      */
     public static Resource create(WriteGraph g, Iterable<Resource> elements) throws DatabaseException {
-        Layer0 L0 = Layer0.getInstance(g);
-        
-        Resource list = g.newResource();
-        g.claim(list, L0.InstanceOf, L0.List);
-        
-        insertBetween(g, L0, list, list, list, elements);
-        return list;
+       Layer0 L0 = Layer0.getInstance(g);
+       return ListUtils.create(g,L0.List, L0.List_Element, null, elements);
     }
-
+    
+    public static Resource createWithInverses(WriteGraph g, Iterable<Resource> elements) throws DatabaseException {
+       Layer0 L0 = Layer0.getInstance(g);
+       return ListUtils.create(g,L0.ListWithInverses, L0.List_ElementWithInverse, L0.List_ElementWithInverse_Inverse, elements);
+    }
+    
     /**
      * Creates a list of the given list type containing the given {@code elements}.
      */
@@ -84,7 +85,21 @@ public class ListUtils {
         insertBetween(g, L0, list, list, list, elements);
         return list;
     }
-    
+
+    public static Resource create(WriteOnlyGraph g, Resource type, Resource elementPredicate, Resource elementPredicateInverse, Iterable<Resource> elements) throws DatabaseException {
+        Layer0 L0 = g.getService(Layer0.class);
+        Resource list = g.newResource();
+        g.claim(list, L0.InstanceOf, null, type);
+        if (!elementPredicate.equals(L0.List_Element))
+               g.claim(list, L0.List_ElementPredicate, L0.List_ElementPredicate_Inverse, elementPredicate);
+        createExisting(g, list, elementPredicate, elementPredicateInverse, elements);
+        return list;
+    }
+
+    public static Resource create(WriteOnlyGraph g, Resource type, Resource elementPredicate, Resource elementPredicateInverse, Resource... elements) throws DatabaseException {
+        return create(g, type, elementPredicate, elementPredicateInverse, Arrays.asList(elements));
+    }
+
     /**
      * Inserts given {@code elements} into the front of the list.
      */
@@ -92,33 +107,41 @@ public class ListUtils {
         if (!elements.iterator().hasNext())
             return;
         Layer0 L0 = Layer0.getInstance(g);
-        
-        Resource first = g.getSingleObject(list, L0.List_Next);        
-        g.deny(list, L0.List_Next, L0.List_Previous, first);        
+
+        Resource first = g.getSingleObject(list, L0.List_Next);
+        g.deny(list, L0.List_Next, L0.List_Previous, first);
         insertBetween(g, L0, list, list, first, elements);
     }
-    
+
     public static void createExisting(WriteOnlyGraph g, Resource list, Iterable<Resource> elements) throws DatabaseException {
-       createExisting(g, list, false, elements);
+        createExisting(g, list, false, elements);
     }
 
     public static void createExisting(WriteOnlyGraph g, Resource list, boolean withInverses, Iterable<Resource> elements) throws DatabaseException {
-    
         Layer0 L0 = g.getService(Layer0.class);
-        Resource before = list;
         Resource elementPredicate = withInverses ? L0.List_ElementWithInverse : L0.List_Element;
+        Resource elementPredicateInverse = withInverses ? L0.List_ElementWithInverse_Inverse : null;
+        createExisting(g, list, elementPredicate, elementPredicateInverse, elements);
+    }
+
+    public static void createExisting(WriteOnlyGraph g, Resource list, Resource elementPredicate, Resource elementPredicateInverse, Iterable<Resource> elements) throws DatabaseException {
+        Layer0 L0 = g.getService(Layer0.class);
+        Resource before = list;
         for(Resource item : elements) {
             Resource cur = g.newResource();
             g.claim(cur, L0.InstanceOf, null, L0.List_Entry);
             g.claim(cur, L0.IsOwnedBy, L0.IsComposedOf, list);
             g.claim(before, L0.List_Next, L0.List_Previous, cur);
-            g.claim(cur, elementPredicate, null, item);
+            g.claim(cur, elementPredicate, elementPredicateInverse, item);
             before = cur;
         }  
         g.claim(before, L0.List_Next, L0.List_Previous, list);
-       
     }
-    
+
+    public static void createExisting(WriteOnlyGraph g, Resource list, Resource elementPredicate, Resource elementPredicateInverse, Resource... elements) throws DatabaseException {
+        createExisting(g, list, elementPredicate, elementPredicateInverse, Arrays.asList(elements));
+    }
+
     /**
      * Inserts given {@code elements} into the back of the list.
      */
@@ -308,14 +331,13 @@ public class ListUtils {
         Layer0 L0 = Layer0.getInstance(g);
         Resource predicate = g.getPossibleObject(list, L0.List_ElementPredicate);
         if(predicate != null) return predicate;
-       return g.isInstanceOf(list, L0.ListWithInverses) ?
-                       L0.List_ElementWithInverse : L0.List_Element; 
+        return g.isInstanceOf(list, L0.ListWithInverses) ?
+                L0.List_ElementWithInverse : L0.List_Element; 
     }
 
     public static Resource getListElementList(ReadGraph g, Resource element) throws DatabaseException {
         Layer0 L0 = Layer0.getInstance(g);
-       return g.getSingleObject(element, L0.IsOwnedBy);
+        return g.getSingleObject(element, L0.IsOwnedBy);
     }
-    
-    
+
 }