Add winery source code
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / java / org / eclipse / winery / repository / datatypes / select2 / Select2DataWithOptGroups.java
1 /*******************************************************************************
2  * Copyright (c) 2015 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.datatypes.select2;
13
14 import java.util.HashMap;
15 import java.util.Map;
16 import java.util.SortedSet;
17 import java.util.TreeSet;
18
19 public class Select2DataWithOptGroups {
20         
21         Map<String, Select2OptGroup> idx = new HashMap<>();
22         
23         
24         /**
25          * Add an item to a group
26          * 
27          * @param group the group
28          * @param id the id of the item
29          * @param text the text of the item {@inheritDoc}
30          */
31         public void add(String group, String id, String text) {
32                 Select2OptGroup optGroup = this.idx.get(group);
33                 if (optGroup == null) {
34                         optGroup = new Select2OptGroup(group);
35                         this.idx.put(group, optGroup);
36                 }
37                 
38                 Select2DataItem item = new Select2DataItem(id, text);
39                 optGroup.addItem(item);
40         }
41         
42         public SortedSet<Select2OptGroup> asSortedSet() {
43                 // convert the index to the real result
44                 SortedSet<Select2OptGroup> res = new TreeSet<>();
45                 for (Select2OptGroup optGroup : this.idx.values()) {
46                         res.add(optGroup);
47                 }
48                 
49                 return res;
50                 
51         }
52 }