Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5a87d76b866a85aac17069b28557cbdf4f5e5e53 (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
/*******************************************************************************
 * Copyright (c) 2012, 2014 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.tcf.te.launch.core.selection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.tcf.te.launch.core.selection.interfaces.ILaunchSelection;
import org.eclipse.tcf.te.launch.core.selection.interfaces.ISelectionContext;

/**
 * Launch selection implementation.
 */
public class LaunchSelection implements ILaunchSelection {
	// The launch mode the selection has been created for
	private String mode;
	// The selection contexts
	private ISelectionContext[] contexts = null;

	/**
	 * Constructor.
	 *
	 * @param mode The launch mode or <code>null</code>.
	 * @param context The selection context or <code>null</code>.
	 */
	public LaunchSelection(String mode, ISelectionContext context) {
		this(mode, context != null ? new ISelectionContext[] { context } : null);
	}

	/**
	 * Constructor.
	 *
	 * @param mode The launch mode or <code>null</code>.
	 */
	public LaunchSelection(String mode, ISelectionContext[] contexts) {
		this.mode = mode;
		this.contexts = contexts != null ? Arrays.copyOf(contexts, contexts.length) : null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.launch.core.selection.interfaces.ILaunchSelection#getLaunchMode()
	 */
	@Override
    public final String getLaunchMode() {
		return mode;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.launch.core.selection.interfaces.ILaunchSelection#getSelectedContexts()
	 */
	@Override
    public ISelectionContext[] getSelectedContexts() {
		return contexts != null ? Arrays.copyOf(contexts, contexts.length) : new ISelectionContext[0];
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.launch.core.selection.interfaces.ILaunchSelection#getSelectedContexts(java.lang.Class)
	 */
	@Override
    public ISelectionContext[] getSelectedContexts(Class<?> type) {
		List<ISelectionContext> contexts = new ArrayList<ISelectionContext>();

		for (ISelectionContext selectionContext : getSelectedContexts()) {
			if (type.isInstance(selectionContext)) {
				contexts.add(selectionContext);
			}
		}

		return contexts.toArray(new ISelectionContext[contexts.size()]);
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		boolean equals = obj instanceof ILaunchSelection;

		if (equals) {
			ILaunchSelection otherSelection = (ILaunchSelection)obj;
			// Launch mode must be the same
			equals &= mode == null && otherSelection.getLaunchMode() == null || mode != null && mode.equals(otherSelection.getLaunchMode());

			// And all selection contexts must be the same
			ISelectionContext[] c1 = getSelectedContexts();
			ISelectionContext[] c2 = otherSelection.getSelectedContexts();

			equals &= c1.length == c2.length;

			int i = 0;
			while (equals && i < c1.length) {
				equals &= c1[i].equals(c2[i]);
				i++;
			}
		}

		return equals;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		int hashCode = 0;

		if (mode != null) hashCode = mode.hashCode() << 16;

		for (ISelectionContext context : getSelectedContexts()) {
			hashCode = hashCode ^ context.hashCode();
		}

		return hashCode;
	}
}

Back to the top