X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.db.common%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fcommon%2Futils%2FListUtils.java;h=656dfeb27550b8acfe8e1f1c8801cbebefe6e879;hp=4d6518524d2ad1c31ebe98f890d17d975743ce4c;hb=b28a9d30e47693246404bac50c7e031c1146c273;hpb=4be719091f987e1377657949584f28665c852ce3 diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/ListUtils.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/ListUtils.java index 4d6518524..656dfeb27 100644 --- a/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/ListUtils.java +++ b/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/ListUtils.java @@ -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 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 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 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 elements) throws DatabaseException { - createExisting(g, list, false, elements); + createExisting(g, list, false, elements); } public static void createExisting(WriteOnlyGraph g, Resource list, boolean withInverses, Iterable 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 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); } - - + }