Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0189611d113a6228ace4965f0b53d58cd72b54a5 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*******************************************************************************
 * Copyright (c) 2011 Wind River Systems, Inc. and others. 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tm.te.runtime.model;

import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.tm.te.runtime.events.EventManager;
import org.eclipse.tm.te.runtime.model.interfaces.IContainerModelNode;
import org.eclipse.tm.te.runtime.model.interfaces.IModelNode;

/**
 * A common (data) model container node implementation.
 * <p>
 * <b>Note:</b> The (data) model node implementation is not thread-safe. Clients requiring
 *              a thread-safe implementation should subclass the properties container and
 *              overwrite {@link #checkThreadAccess()}.
 */
public class ContainerModelNode extends ModelNode implements IContainerModelNode {
	// Note: Do _not_ use sorted sets/trees here! The trees get not resorted if the element state
	// changes. We may loose the possibility to find the element again within the tree!
	private final List<IModelNode> childList = new ArrayList<IModelNode>();

	// Lock to use for synchronization purpose
	private final Lock childListLock = new ReentrantLock();

	// empty array
	public static final IModelNode[] EMPTY_MODEL_NODE_ARRAY = new IModelNode[0];

	/**
	 * Constructor.
	 */
	public ContainerModelNode() {
		super();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tm.te.runtime.interfaces.nodes.IContainerModelNode#getChildren()
	 */
	@Override
	public IModelNode[] getChildren() {
		return internalGetChildren();
	}

	/**
	 * Return the real child list.
	 */
	protected final IModelNode[] internalGetChildren() {
		// Create the list that will hold the copy (non-deep copy)
		List<IModelNode> children = new ArrayList<IModelNode>();
		try {
			// Acquire the lock while copying the child references
			childListLock.lock();
			// Add the children to the returned list copy
			children.addAll(childList);
		} finally {
			// Release the lock
			childListLock.unlock();
		}
		return children.toArray(new IModelNode[children.size()]);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tm.te.runtime.interfaces.nodes.IContainerModelNode#getChildren(java.lang.Class)
	 */
	@Override
	@SuppressWarnings("unchecked")
	public <T> List<T> getChildren(Class<T> instanceOf) {
		// Create the list that will hold the found children being
		// a instance of the given class
		List<T> children = new ArrayList<T>();
		try {
			// Acquire the lock while copying the child references
			childListLock.lock();
			// Walk through all the children and check for the class
			for (IModelNode child : childList) {
				if (instanceOf.isInstance(child)) {
					children.add((T)child);
				}
			}
		} finally {
			// Release the look
			childListLock.unlock();
		}

		return children;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tm.te.runtime.interfaces.nodes.IContainerModelNode#hasChildren()
	 */
	@Override
	public boolean hasChildren() {
		boolean hasChildren = false;
		try { childListLock.lock(); hasChildren = !childList.isEmpty(); } finally { childListLock.unlock(); }
		return hasChildren;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tm.te.runtime.interfaces.nodes.IContainerModelNode#add(org.eclipse.tm.te.runtime.interfaces.nodes.IModelNode)
	 */
	@Override
	public boolean add(IModelNode node) {
		if (node != null) {
			try {
				childListLock.lock();
				// set the parent if not set otherwise before.
				if (node.getParent() == null) {
					node.setParent(this);
				}
				else {
					assert node.getParent() == this : "Attempt to add child node to " + getName() + " with this != node.getParent()!!!"; //$NON-NLS-1$ //$NON-NLS-2$
				}
				childList.add(node);
			} finally {
				childListLock.unlock();
			}

			EventObject event = newEvent(this, NOTIFY_ADDED, null, new IModelNode[] { node });
			if (event != null) EventManager.getInstance().fireEvent(event);

			return true;
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tm.te.runtime.interfaces.nodes.IContainerModelNode#remove(org.eclipse.tm.te.runtime.interfaces.nodes.IModelNode, boolean)
	 */
	@Override
	public boolean remove(IModelNode node, boolean recursive) {
		if (node instanceof IContainerModelNode && recursive) {
			IContainerModelNode container = (IContainerModelNode)node;
			container.clear();
		}

		boolean removed = false;
		// Removes the given node from this container
		try { childListLock.lock(); removed = childList.remove(node); } finally { childListLock.unlock(); }
		// Unlink the parent and fire the removed notification if the element got removed
		if (removed) {
			EventObject event = newEvent(this, NOTIFY_REMOVED, new IModelNode[] { node }, null);
			if (event != null) EventManager.getInstance().fireEvent(event);
		}

		return removed;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tm.te.runtime.interfaces.nodes.IContainerModelNode#removeAll(java.lang.Class)
	 */
	@Override
	public <T> boolean removeAll(Class<T> nodeType) {
		boolean removed = false;
		List<T> children;

		try {
			childListLock.lock();
			children = getChildren(nodeType);
			removed |= childList.removeAll(children);
		} finally {
			childListLock.unlock();
		}

		if (removed) {
			EventObject event = newEvent(this, NOTIFY_REMOVED, children, null);
			if (event != null) EventManager.getInstance().fireEvent(event);
		}

		return removed;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tm.te.runtime.interfaces.nodes.IContainerModelNode#clear()
	 */
	@Override
	public boolean clear() {
		boolean removed = false;

		boolean changed = setChangeEventsEnabled(false);

		try {
			childListLock.lock();
			IModelNode[] children = internalGetChildren();
			for (IModelNode element : children) {
				removed |= remove(element, true);
			}
		} finally {
			childListLock.unlock();
		}

		if (changed) setChangeEventsEnabled(true);

		return removed;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tm.te.runtime.interfaces.nodes.IContainerModelNode#size()
	 */
	@Override
	public int size() {
		return childList.size();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tm.te.runtime.interfaces.nodes.IContainerModelNode#contains(org.eclipse.tm.te.runtime.interfaces.nodes.IModelNode)
	 */
	@Override
	public boolean contains(IModelNode node) {
		if (node != null) {
			try {
				childListLock.lock();
				return childList.contains(node);
			} finally {
				childListLock.unlock();
			}
		}
		return false;
	}

	/**
	 * Returns weather the scheduling rule of container model nodes is considering
	 * children or not. If recursive scheduling rule locking is enabled, than any job
	 * having a child of this container model node as scheduling rule, will conflict
	 * with this container model node.
	 *
	 * @return <code>True</code> if recursive scheduling rule locking is enabled, <code>false</code> otherwise.
	 */
	protected boolean isSchedulingLockedRecursivly() {
		return true;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tm.te.runtime.nodes.ModelNode#contains(org.eclipse.core.runtime.jobs.ISchedulingRule)
	 */
	@Override
	public boolean contains(ISchedulingRule rule) {
		// We deal only with scheduling rules we know about (as the interface
		// declaration of ISchedulingRule#contains requests) and if recursive
		// scheduling rule locking is on.
		if (isSchedulingLockedRecursivly() && rule instanceof IModelNode) {
			// Iterate through the children and if one of the children
			// contains the given scheduling rule, this container model
			// node contains the given scheduling rule as well.
			try {
				childListLock.lock();
				IModelNode[] children = internalGetChildren();
				for (IModelNode child : children) {
					if (child.contains(rule)) {
						return true;
					}
				}
			} finally {
				childListLock.unlock();
			}
		}

		return super.contains(rule);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tm.te.runtime.nodes.ModelNode#isConflicting(org.eclipse.core.runtime.jobs.ISchedulingRule)
	 */
	@Override
	public boolean isConflicting(ISchedulingRule rule) {
		// We deal only with scheduling rules we know about (as the interface
		// declaration of ISchedulingRule#contains requests) and if recursive
		// scheduling rule locking is on.
		if (isSchedulingLockedRecursivly() && rule instanceof IModelNode) {
			// Iterate through the children and if one of the children
			// is conflicting with the given scheduling rule, this
			// container model node is conflicting the given scheduling
			// rule as well.
			try {
				childListLock.lock();
				IModelNode[] children = internalGetChildren();
				for (IModelNode child : children) {
					if (child.isConflicting(rule)) {
						return true;
					}
				}
			} finally {
				childListLock.unlock();
			}
		}

		return super.isConflicting(rule);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tm.te.runtime.nodes.ModelNode#find(java.util.UUID)
	 */
	@Override
	public IModelNode find(UUID uuid) {
		IModelNode find = super.find(uuid);
		if (find != null) return find;

		for (IModelNode child : childList) {
			find = child.find(uuid);
			if (find != null) {
				return find;
			}
		}

		return find;
	}
}

Back to the top