]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/RVIBuilder.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / RVIBuilder.java
diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/RVIBuilder.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/RVIBuilder.java
new file mode 100644 (file)
index 0000000..96f1be4
--- /dev/null
@@ -0,0 +1,112 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.layer0.variable;\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.layer0.variable.RVI.RVIPart;\r
+import org.simantics.db.layer0.variable.Variables.Role;\r
+\r
+/**\r
+ * Utility for building RVI parts.\r
+ * \r
+ * @author toni.kalajainen\r
+ */\r
+public class RVIBuilder {\r
+\r
+       Binding rviBinding;\r
+    List<RVIPart> parts;\r
+\r
+    public RVIBuilder(Binding rviBinding)\r
+    {\r
+        parts = new ArrayList<RVIPart>();\r
+    }\r
+\r
+    public RVIBuilder(RVI rvi)\r
+    {\r
+        set(rvi);\r
+    }\r
+\r
+    public RVIBuilder set( RVI rvi ) \r
+    {\r
+       this.rviBinding = rvi.getBinding();\r
+        parts = new ArrayList<RVIPart>( rvi.parts.length + 10 );\r
+        for ( RVIPart part : rvi.parts ) parts.add( part );\r
+        return this;\r
+    }\r
+\r
+    public RVIBuilder append( RVI rvi ) \r
+    {\r
+        for ( RVIPart part : rvi.parts ) parts.add( part );\r
+        return this;\r
+    }\r
+\r
+    public RVIBuilder append( RVIPart part )\r
+    {\r
+        parts.add( part );\r
+        return this;\r
+    }\r
+\r
+    public RVIBuilder append( Role role, String string )\r
+    {\r
+        parts.add( new RVI.StringRVIPart(role, string) );\r
+        return this;\r
+    }\r
+\r
+    public RVIBuilder append( Role role, Resource resource )\r
+    {\r
+        parts.add( new RVI.ResourceRVIPart(role, resource) );\r
+        return this;\r
+    }\r
+    \r
+    public RVIBuilder append( Role role, Resource resource, long mostSignificant, long leastSignificant)\r
+    {\r
+        parts.add( new RVI.GuidRVIPart(role, resource, mostSignificant, leastSignificant) );\r
+        return this;\r
+    }\r
+\r
+    /**\r
+     * @param rvi the RVI to truncate\r
+     * @param n number of parts to remove from the end of the RVI\r
+     * @return truncated RVI\r
+     */\r
+    public RVIBuilder removeLast(int n) {\r
+        if (n > parts.size())\r
+            throw new IllegalArgumentException("Requested to remove " + n + " parts from RVI, only " + parts.size()\r
+                    + " parts exist in '" + parts + "'");\r
+        parts = parts.subList(0, parts.size() - n);\r
+        return this;\r
+    }\r
+\r
+    public RVIBuilder removeFromFirstRole(Role role) {\r
+        for (int i = 0; i < parts.size(); ++i) {\r
+            if (parts.get(i).getRole() == role) {\r
+                parts = parts.subList(0, i);\r
+                return this;\r
+            }\r
+        }\r
+        return this;\r
+    }\r
+\r
+    public RVI toRVI()\r
+    {\r
+        if (parts.isEmpty())\r
+            return RVI.empty( rviBinding );\r
+        RVI rvi = new RVI( rviBinding );\r
+        rvi.parts = (RVIPart[]) parts.toArray( new RVIPart[ parts.size() ] );\r
+        return rvi;\r
+    }\r
+\r
+}\r