summaryrefslogtreecommitdiffstats
path: root/winery/org.eclipse.winery.common/src/main/java/org/eclipse/winery/common/ids/elements/TOSCAElementId.java
blob: 139a567ef13233d17351f50c96952bfba652ec06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*******************************************************************************
 * Copyright (c) 2013 University of Stuttgart.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and the Apache License 2.0 which both accompany this distribution,
 * and are available at http://www.eclipse.org/legal/epl-v10.html
 * and http://www.apache.org/licenses/LICENSE-2.0
 *
 * Contributors:
 *     Oliver Kopp - initial API and implementation
 *******************************************************************************/
package org.eclipse.winery.common.ids.elements;

import org.eclipse.winery.common.ids.GenericId;
import org.eclipse.winery.common.ids.XMLId;

/**
 * Models an ID of a TOSCA element, which is NOT a TOSCAcomponentId
 * 
 * It has a parent and an xmlId
 */
public abstract class TOSCAElementId extends GenericId {
	
	private final GenericId parent;
	
	
	public TOSCAElementId(GenericId parent, XMLId xmlId) {
		super(xmlId);
		this.parent = parent;
	}
	
	@Override
	public GenericId getParent() {
		return this.parent;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof TOSCAElementId) {
			TOSCAElementId otherId = (TOSCAElementId) obj;
			// the XML id has to be equal and the parents have to be equal
			return (otherId.getXmlId().equals(this.getXmlId())) && (otherId.getParent().equals(this.getParent()));
		} else {
			return false;
		}
	}
	
	@Override
	public int compareTo(GenericId o1) {
		if (o1 instanceof TOSCAElementId) {
			TOSCAElementId o = (TOSCAElementId) o1;
			if (this.getParent().equals(o.getParent())) {
				return this.getXmlId().compareTo(o.getXmlId());
			} else {
				return this.getParent().compareTo(o.getParent());
			}
		} else {
			// comparing TOSCAcomponentIDs with non-TOSCAcomponentIDs is not
			// possible
			throw new IllegalStateException();
		}
	}
	
	@Override
	public int hashCode() {
		return this.getParent().hashCode() ^ this.getXmlId().hashCode();
	}
	
	@Override
	public String toString() {
		String res;
		res = this.getClass().toString() + " / " + this.getXmlId().getDecoded();
		res += "\n";
		res += "parent: " + this.getParent().toString();
		return res;
	}
}