Skip to main content
summaryrefslogtreecommitdiffstats
blob: e5d24ef1c5f6e7b066835622b3de8321971b4bd0 (plain) (blame)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
 * Copyright (c) 2017 CEA LIST.
 * 
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *  
 *  Contributors:
 *  Maged Elaasar - Initial API and implementation
 *  
 * 
 */
package org.eclipse.papyrus.infra.core.architecture.merged;

import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

import org.eclipse.papyrus.infra.core.architecture.ADElement;
import org.eclipse.papyrus.infra.core.architecture.ArchitectureContext;

/**
 * An element that represents a merged collection of {@link org.eclipse.papyrus.infra.core.
 * architecture.ADElement}s that are instances of the same EClass. This allows the definition 
 * of architecture elements to be split across several architectural models (*.architecture).
 * 
 * All merged elements is assumed to have the same name and qualified name values. However, only
 * one of those elements (the main merge increment) has values for the single-valued properties.
 * On the other hand, all multi-valued properties of the elements are merged.
 * 
 * @see org.eclipse.papyrus.infra.core.architecture.ADElement
 * @since 1.0
 */
public class MergedADElement {

	/**
	 * The merged parent of this element
	 */
	protected MergedADElement parent;
	
	/**
	 * the architecture elements that represent merge increments of this element
	 */
	protected Set<ADElement> elements;
	
	/**
	 * Create a new '<em><b>Merged AD Element</b></em>'.
	 *
	 * @param parent the merged parent of this element
	 */
	public MergedADElement(MergedADElement parent) {
		this.parent = parent;
		this.elements = new LinkedHashSet<ADElement>();
	}

	/**
	 * Get the element's parent
	 * 
	 * @return the parent element
	 */
	public MergedADElement getParent() {
		return parent;
	}

	/**
	 * Gets the context's id
	 * 
	 * @return an id
	 */
	public String getId() {
		for (ADElement element : elements) {
			if (element.getId() != null)
				return element.getId();
		}
		return null;
	}

	/**
	 * Get the element's name
	 * 
	 * @return a name
	 */
	public String getName() {
		for (ADElement element : elements) {
			if (element.getName() != null)
				return element.getName();
		}
		return null;
	}

	/**
	 * Get the element's qualified name
	 * 
	 * @return a qualified name
	 */
	public String getQualifiedName() {
		for (ADElement element : elements) {
			if (element.getQualifiedName() != null)
				return element.getQualifiedName();
		}
		return null;
	}
	
	/**
	 * Get the element's description
	 * 
	 * @return a description
	 */
	public String getDescription() {
		for (ADElement element : elements) {
			if (element.getDescription() != null)
				return element.getDescription();
		}
		return null;
	}

	/**
	 * Gets the context's icon path
	 * 
	 * @return an icon path
	 */
	public String getIcon() {
		Object obj = getImageObject();
		if (obj instanceof ADElement)
			return ((ADElement)obj).getIcon();
		return null;
	}

	/**
	 * Get a merge increment whose image represents that of the merged element   
	 * 
	 * By default, any one of the merge increments will be returned. Subclasses may override.
	 * 
	 * @return a merge increment
	 */
	public Object getImageObject() {
		for (ADElement element : elements) {
			if (element.getIcon() != null)
				return element;
		}
		return null;
	}

	@Override
	public int hashCode() {
		int hash = 0;
		for (ADElement element : elements) {
			hash += element.hashCode();
		}
		return hash;
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof MergedADElement))
			return false;
		MergedADElement other = (MergedADElement) obj;
		if (other.parent != this.parent)
			return false;
		Set<ADElement> copy = new HashSet<ADElement>(this.elements);
		copy.retainAll(other.elements);
		return copy.size() == this.elements.size();
	}

	@Override
	public String toString() {
		Iterator<ADElement> i = elements.iterator();
		if (i.hasNext())
			return i.next().toString();
		return super.toString();
	}

	/*
	 * Adds the given element to the collection of merged elements
	 */
	void merge(ADElement element) {
		elements.add(element);
	}

	
}

Back to the top