]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/RVIBuilder.java
Merge commit 'ad8333027322fda6b9a8a524c7a7e15a54c52f38'
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / RVIBuilder.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.layer0.variable;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.List;\r
16 \r
17 import org.simantics.databoard.binding.Binding;\r
18 import org.simantics.db.Resource;\r
19 import org.simantics.db.layer0.variable.RVI.RVIPart;\r
20 import org.simantics.db.layer0.variable.Variables.Role;\r
21 \r
22 /**\r
23  * Utility for building RVI parts.\r
24  * \r
25  * @author toni.kalajainen\r
26  */\r
27 public class RVIBuilder {\r
28 \r
29         Binding rviBinding;\r
30     List<RVIPart> parts;\r
31 \r
32     public RVIBuilder(Binding rviBinding)\r
33     {\r
34         parts = new ArrayList<RVIPart>();\r
35     }\r
36 \r
37     public RVIBuilder(RVI rvi)\r
38     {\r
39         set(rvi);\r
40     }\r
41 \r
42     public RVIBuilder set( RVI rvi ) \r
43     {\r
44         this.rviBinding = rvi.getBinding();\r
45         parts = new ArrayList<RVIPart>( rvi.parts.length + 10 );\r
46         for ( RVIPart part : rvi.parts ) parts.add( part );\r
47         return this;\r
48     }\r
49 \r
50     public RVIBuilder append( RVI rvi ) \r
51     {\r
52         for ( RVIPart part : rvi.parts ) parts.add( part );\r
53         return this;\r
54     }\r
55 \r
56     public RVIBuilder append( RVIPart part )\r
57     {\r
58         parts.add( part );\r
59         return this;\r
60     }\r
61 \r
62     public RVIBuilder append( Role role, String string )\r
63     {\r
64         parts.add( new RVI.StringRVIPart(role, string) );\r
65         return this;\r
66     }\r
67 \r
68     public RVIBuilder append( Role role, Resource resource )\r
69     {\r
70         parts.add( new RVI.ResourceRVIPart(role, resource) );\r
71         return this;\r
72     }\r
73     \r
74     public RVIBuilder append( Role role, Resource resource, long mostSignificant, long leastSignificant)\r
75     {\r
76         parts.add( new RVI.GuidRVIPart(role, resource, mostSignificant, leastSignificant) );\r
77         return this;\r
78     }\r
79 \r
80     /**\r
81      * @param rvi the RVI to truncate\r
82      * @param n number of parts to remove from the end of the RVI\r
83      * @return truncated RVI\r
84      */\r
85     public RVIBuilder removeLast(int n) {\r
86         if (n > parts.size())\r
87             throw new IllegalArgumentException("Requested to remove " + n + " parts from RVI, only " + parts.size()\r
88                     + " parts exist in '" + parts + "'");\r
89         parts = parts.subList(0, parts.size() - n);\r
90         return this;\r
91     }\r
92 \r
93     public RVIBuilder removeFromFirstRole(Role role) {\r
94         for (int i = 0; i < parts.size(); ++i) {\r
95             if (parts.get(i).getRole() == role) {\r
96                 parts = parts.subList(0, i);\r
97                 return this;\r
98             }\r
99         }\r
100         return this;\r
101     }\r
102 \r
103     public RVI toRVI()\r
104     {\r
105         if (parts.isEmpty())\r
106             return RVI.empty( rviBinding );\r
107         RVI rvi = new RVI( rviBinding );\r
108         rvi.parts = (RVIPart[]) parts.toArray( new RVIPart[ parts.size() ] );\r
109         return rvi;\r
110     }\r
111 \r
112 }\r