Add winery source code
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.common / src / main / java / org / eclipse / winery / common / ids / definitions / TOSCAComponentId.java
1 /*******************************************************************************
2  * Copyright (c) 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.common.ids.definitions;
13
14 import javax.xml.namespace.QName;
15
16 import org.eclipse.winery.common.ids.GenericId;
17 import org.eclipse.winery.common.ids.Namespace;
18 import org.eclipse.winery.common.ids.XMLId;
19
20 /**
21  * Identifies a TOSCA component. Each component is required to be identified
22  * subclasses this class
23  * 
24  * A TOSCAcomponentId has a namespace and an id within that namespace. In XML,
25  * the ID might be serialized as NCName (in the case of EntityTypes and
26  * EntityTemplates) and as xs:id (in the case of EntityTypeImplementations)
27  * 
28  * Components are elements, which may appear directly nested in TDefinitions:
29  * <ul>
30  * <li>ServiceTemplates,</li>
31  * <li>EntityTypes,</li
32  * <li>EntityTypeImplementations,</li>
33  * <li>EntityTemplates</li>
34  * </ul>
35  */
36 public abstract class TOSCAComponentId extends GenericId {
37         
38         private final Namespace namespace;
39         
40         
41         public TOSCAComponentId(Namespace namespace, XMLId xmlId) {
42                 super(xmlId);
43                 this.namespace = namespace;
44         }
45         
46         /**
47          * Creates a new id based on strings. This constructor is required for
48          * {@link AbstractComponentsResource}
49          * 
50          * @param ns the namespace to be used
51          * @param id the id to be used
52          * @param URLencoded true: both Strings are URLencoded, false: both Strings
53          *            are not URLencoded
54          */
55         public TOSCAComponentId(String ns, String id, boolean URLencoded) {
56                 this(new Namespace(ns, URLencoded), new XMLId(id, URLencoded));
57         }
58         
59         public TOSCAComponentId(QName qname) {
60                 this(qname.getNamespaceURI(), qname.getLocalPart(), false);
61         }
62         
63         public QName getQName() {
64                 QName qname = new QName(this.getNamespace().getDecoded(), this.getXmlId().getDecoded());
65                 return qname;
66         }
67         
68         public Namespace getNamespace() {
69                 return this.namespace;
70         }
71         
72         @Override
73         public int hashCode() {
74                 return this.namespace.hashCode() ^ this.getXmlId().hashCode();
75         }
76         
77         @Override
78         public boolean equals(Object obj) {
79                 if (this == obj) {
80                         return true;
81                 }
82                 if (!(obj instanceof TOSCAComponentId)) {
83                         return false;
84                 } else {
85                         TOSCAComponentId other = (TOSCAComponentId) obj;
86                         return this.getXmlId().equals(other.getXmlId()) && this.namespace.equals(other.namespace);
87                 }
88         }
89         
90         @Override
91         public String toString() {
92                 QName qn = this.getQName();
93                 return this.getClass().toString() + " / " + qn.toString();
94         }
95         
96         @Override
97         public GenericId getParent() {
98                 return null;
99         }
100         
101         @Override
102         public int compareTo(GenericId o1) {
103                 if (o1 instanceof TOSCAComponentId) {
104                         TOSCAComponentId o = (TOSCAComponentId) o1;
105                         int res = this.getXmlId().compareTo(o.getXmlId());
106                         if (res == 0) {
107                                 res = this.getNamespace().compareTo(o.getNamespace());
108                         }
109                         return res;
110                 } else {
111                         // comparing TOSCAcomponentIDs with non-TOSCAcomponentIDs is not
112                         // possible
113                         throw new IllegalStateException();
114                 }
115         }
116 }