609d4d64e90577b002c1829c221d0d958b31dc61
[vfc/nfvo/wfengine.git] /
1 /*******************************************************************************
2  * Copyright (c) 2012-2013 University of Stuttgart.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * and the Apache License 2.0 which both accompany this distribution,
6  * and are available at http://www.eclipse.org/legal/epl-v10.html
7  * and http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Contributors:
10  *     Oliver Kopp - initial API and implementation
11  *******************************************************************************/
12 package org.eclipse.winery.repository.export;
13
14 import java.util.ArrayDeque;
15 import java.util.Collection;
16 import java.util.HashSet;
17 import java.util.Queue;
18
19 import org.eclipse.winery.common.ids.definitions.TOSCAComponentId;
20
21 /**
22  * Holds the state of ids regarding the export <br />
23  * 
24  * Required as we do not know at the entry point (usually a service template),
25  * which other components are linked <br />
26  * 
27  * Users can call flagAsExportRequired more than once for the same id. If an id
28  * is already exported, it is not flagged as exported again
29  */
30 public class ExportedState {
31         
32         private final Collection<TOSCAComponentId> exported = new HashSet<>();
33         private final Queue<TOSCAComponentId> exportRequired = new ArrayDeque<>();
34         
35         
36         /**
37          * @return the first tosca component id to be exported, null if no more
38          *         elements are in the queue
39          */
40         public TOSCAComponentId pop() {
41                 return this.exportRequired.poll();
42         }
43         
44         public void flagAsExported(TOSCAComponentId id) {
45                 this.exportRequired.remove(id);
46                 this.exported.add(id);
47         }
48         
49         /**
50          * Flags the given id as required for export, if not already exported
51          * 
52          * @param id the id to flag
53          */
54         public void flagAsExportRequired(TOSCAComponentId id) {
55                 if (!this.exported.contains(id)) {
56                         this.exportRequired.add(id);
57                 }
58         }
59         
60         public void flagAsExportRequired(Collection<TOSCAComponentId> ids) {
61                 for (TOSCAComponentId id : ids) {
62                         if ((!this.exported.contains(id)) && (!this.exportRequired.contains(id))) {
63                                 this.exportRequired.add(id);
64                         }
65                 }
66         }
67 }