139a567ef13233d17351f50c96952bfba652ec06
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.common / src / main / java / org / eclipse / winery / common / ids / elements / TOSCAElementId.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.elements;
13
14 import org.eclipse.winery.common.ids.GenericId;
15 import org.eclipse.winery.common.ids.XMLId;
16
17 /**
18  * Models an ID of a TOSCA element, which is NOT a TOSCAcomponentId
19  * 
20  * It has a parent and an xmlId
21  */
22 public abstract class TOSCAElementId extends GenericId {
23         
24         private final GenericId parent;
25         
26         
27         public TOSCAElementId(GenericId parent, XMLId xmlId) {
28                 super(xmlId);
29                 this.parent = parent;
30         }
31         
32         @Override
33         public GenericId getParent() {
34                 return this.parent;
35         }
36         
37         @Override
38         public boolean equals(Object obj) {
39                 if (obj instanceof TOSCAElementId) {
40                         TOSCAElementId otherId = (TOSCAElementId) obj;
41                         // the XML id has to be equal and the parents have to be equal
42                         return (otherId.getXmlId().equals(this.getXmlId())) && (otherId.getParent().equals(this.getParent()));
43                 } else {
44                         return false;
45                 }
46         }
47         
48         @Override
49         public int compareTo(GenericId o1) {
50                 if (o1 instanceof TOSCAElementId) {
51                         TOSCAElementId o = (TOSCAElementId) o1;
52                         if (this.getParent().equals(o.getParent())) {
53                                 return this.getXmlId().compareTo(o.getXmlId());
54                         } else {
55                                 return this.getParent().compareTo(o.getParent());
56                         }
57                 } else {
58                         // comparing TOSCAcomponentIDs with non-TOSCAcomponentIDs is not
59                         // possible
60                         throw new IllegalStateException();
61                 }
62         }
63         
64         @Override
65         public int hashCode() {
66                 return this.getParent().hashCode() ^ this.getXmlId().hashCode();
67         }
68         
69         @Override
70         public String toString() {
71                 String res;
72                 res = this.getClass().toString() + " / " + this.getXmlId().getDecoded();
73                 res += "\n";
74                 res += "parent: " + this.getParent().toString();
75                 return res;
76         }
77 }