Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0326558745bf98906b0fb77d3b29b6578d912087 (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
/*******************************************************************************
 * 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.Arrays;

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

/**
 * Abstract selection context implementation.
 */
public abstract class AbstractSelectionContext implements ISelectionContext {
	// The context
	protected final Object context;
	// The selection context type
	protected String type = null;
	// The selected objects
	private Object[] selections;
	// Preferred selection context marker
	private boolean isPreferred = false;

	/**
	 * Constructor.
	 *
	 * @param The context object.
	 * @param selections The selected objects or <code>null</code>.
	 * @param isPreferred <code>True</code> to mark the selection context the preferred context,
	 *            <code>false</code> otherwise.
	 */
	public AbstractSelectionContext(Object context, Object[] selections, boolean isPreferred) {
		this.context = context;
		this.selections = selections != null ? selections : new Object[0];
		this.isPreferred = isPreferred;
	}

	/**
	 * Constructor.
	 *
	 * @param The context object.
	 * @param type The selection context type or <code>null</code>.
	 * @param selections The selected objects or <code>null</code>.
	 * @param isPreferred <code>True</code> to mark the selection context the preferred context,
	 *            <code>false</code> otherwise.
	 */
	public AbstractSelectionContext(Object context, String type, Object[] selections, boolean isPreferred) {
		this.context = context;
		this.type = type;
		this.selections = selections != null ? selections : new Object[0];
		this.isPreferred = isPreferred;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.launch.core.selection.interfaces.ISelectionContext#getContext()
	 */
	@Override
	public Object getContext() {
		return context;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.launch.core.selection.interfaces.ISelectionContext#getType()
	 */
	@Override
	public String getType() {
		return type;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.launch.core.selection.interfaces.ISelectionContext#getSelections()
	 */
	@Override
	public Object[] getSelections() {
		return selections;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.launch.core.selection.interfaces.ISelectionContext#setIsPreferredContext(boolean)
	 */
	@Override
	public void setIsPreferredContext(boolean isPreferred) {
		this.isPreferred = isPreferred;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.launch.core.selection.interfaces.ISelectionContext#isPreferredContext()
	 */
	@Override
	public boolean isPreferredContext() {
		return isPreferred;
	}

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

		if (equals) {
			ISelectionContext otherContext = (ISelectionContext)obj;

			equals &= type == null && otherContext.getType() == null || type != null && type.equals(otherContext.getType());
			equals &= isPreferred == otherContext.isPreferredContext();

			Object[] otherSelections = otherContext.getSelections();
			equals &= selections == null && otherSelections == null || selections != null && otherSelections != null;

			int i = 0;
			int length = selections != null ? selections.length : -1;
			int otherLength = otherSelections != null ? otherSelections.length : -1;
			equals &= (length == otherLength);

			while (equals && i < length) {
				equals &= selections[i] == null && otherSelections[i] == null || selections[i] != null && selections[i].equals(otherSelections[i]);
				i++;
			}
		}

		return equals;
	}

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

		if (type != null) {
			hashCode ^= type.hashCode() << 8;
		}
		hashCode ^= Boolean.valueOf(isPreferred).hashCode();

		if (selections != null) {
			hashCode ^= Arrays.hashCode(selections);
		}

		return hashCode;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		StringBuffer toString = new StringBuffer();

		if (type != null){
			toString.append(type);
		}

		return toString.toString();
	}
}

Back to the top