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