Add winery source code
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.topologymodeler / src / main / java / org / eclipse / winery / topologymodeler / WineryUtil.java
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.topologymodeler;
13
14 import java.util.List;
15 import java.util.SortedMap;
16 import java.util.SortedSet;
17 import java.util.TreeMap;
18 import java.util.TreeSet;
19
20 import org.eclipse.winery.common.interfaces.QNameWithName;
21
22 public class WineryUtil {
23         
24         /**
25          * LocalName is the ID of the element, whereas Name is the speaking name
26          */
27         public static class LocalNameNamePair implements Comparable<LocalNameNamePair> {
28                 
29                 String localName;
30                 String name;
31                 
32                 
33                 public LocalNameNamePair(String localName, String name) {
34                         this.localName = localName;
35                         this.name = name;
36                 }
37                 
38                 public String getLocalName() {
39                         return this.localName;
40                 }
41                 
42                 public String getName() {
43                         return this.name;
44                 }
45                 
46                 /**
47                  * Ordering according to name
48                  */
49                 @Override
50                 public int compareTo(LocalNameNamePair otherPair) {
51                         return this.name.compareTo(otherPair.name);
52                 }
53                 
54                 @Override
55                 public int hashCode() {
56                         return this.localName.hashCode();
57                 }
58                 
59                 @Override
60                 public boolean equals(Object o) {
61                         if (o instanceof LocalNameNamePair) {
62                                 return this.localName.equals(((LocalNameNamePair) o).getLocalName());
63                         } else {
64                                 return false;
65                         }
66                 }
67         }
68         
69         
70         public static SortedMap<String, SortedSet<LocalNameNamePair>> convertQNameWithNameListToNamespaceToLocalNameNamePairList(List<QNameWithName> list) {
71                 if (list == null) {
72                         throw new IllegalArgumentException("list may not be null");
73                 }
74                 SortedMap<String, SortedSet<LocalNameNamePair>> res = new TreeMap<>();
75                 for (QNameWithName qnameWithName : list) {
76                         SortedSet<LocalNameNamePair> localNameNamePairSet = res.get(qnameWithName.qname.getNamespaceURI());
77                         if (localNameNamePairSet == null) {
78                                 localNameNamePairSet = new TreeSet<>();
79                                 res.put(qnameWithName.qname.getNamespaceURI(), localNameNamePairSet);
80                         }
81                         LocalNameNamePair pair = new LocalNameNamePair(qnameWithName.qname.getLocalPart(), qnameWithName.name);
82                         localNameNamePairSet.add(pair);
83                 }
84                 return res;
85         }
86         
87 }