1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.layer0.variable;
14 import java.util.ArrayList;
15 import java.util.List;
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;
23 * Utility for building RVI parts.
25 * @author toni.kalajainen
27 public class RVIBuilder {
32 public RVIBuilder(Binding rviBinding)
34 parts = new ArrayList<RVIPart>();
37 public RVIBuilder(RVI rvi)
42 public RVIBuilder set( RVI rvi )
44 this.rviBinding = rvi.getBinding();
45 parts = new ArrayList<RVIPart>( rvi.parts.length + 10 );
46 for ( RVIPart part : rvi.parts ) parts.add( part );
50 public RVIBuilder append( RVI rvi )
52 for ( RVIPart part : rvi.parts ) parts.add( part );
56 public RVIBuilder append( RVIPart part )
62 public RVIBuilder append( Role role, String string )
64 parts.add( new RVI.StringRVIPart(role, string) );
68 public RVIBuilder append( Role role, Resource resource )
70 parts.add( new RVI.ResourceRVIPart(role, resource) );
74 public RVIBuilder append( Role role, Resource resource, long mostSignificant, long leastSignificant)
76 parts.add( new RVI.GuidRVIPart(role, resource, mostSignificant, leastSignificant) );
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
85 public RVIBuilder removeLast(int n) {
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);
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);
106 return RVI.empty( rviBinding );
107 RVI rvi = new RVI( rviBinding );
108 rvi.parts = (RVIPart[]) parts.toArray( new RVIPart[ parts.size() ] );