Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ff33aa42360d1588c9f645a2c12742294ecf0dcf (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
/*******************************************************************************
 * Copyright (c) 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.bindings.internal;

import java.util.Arrays;

import org.eclipse.core.runtime.Assert;
import org.eclipse.tcf.te.launch.core.bindings.interfaces.ILaunchBinding;


/**
 * Launch configuration type binding element implementation.
*/
public class LaunchBinding implements ILaunchBinding {
	// The id of the bound element
	private String id;
	// The launch modes the binding applies to
	private final String[] modes;

	/**
	 * Constructor.
	 *
	 * @param id The id of the element to bind. Must not be <code>null</code>.
	 * @param modes The launch modes or <code>null</code>
	 */
	public LaunchBinding(String id, String modes) {
		Assert.isNotNull(id);
		this.id = id;

		if (modes != null) this.modes = modes.trim().split("( )*,( )*"); //$NON-NLS-1$
		else this.modes = new String[0];
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.launch.core.bindings.interfaces.ILaunchBinding#getId()
	 */
	@Override
    public final String getId() {
		return id;
	}

	/**
	 * Returns the list of the launch modes this binding applies to.
	 *
	 * @return The list of launch modes or an empty list.
	 */
	public final String[] getModes() {
		return Arrays.copyOf(modes, modes.length);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.launch.core.bindings.interfaces.ILaunchBinding#isValidLaunchMode(java.lang.String)
	 */
	@Override
    public boolean isValidLaunchMode(String launchMode) {
		if (launchMode == null || launchMode.length() == 0)
			return true;

		for (String candidate : getModes())
			if (launchMode.equals(candidate)) return true;

		return getModes().length == 0;
	}

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

		toString.append("LaunchBinding("); //$NON-NLS-1$
		toString.append(id);
		toString.append(", launchModes"); //$NON-NLS-1$
		toString.append(toString(getModes()));
		toString.append(")"); //$NON-NLS-1$

		return toString.toString();
	}

	/**
	 * Creates a string representation of the given string array.
	 *
	 * @param strings The string array or <code>null</code>.
	 * @return The string representation of the array.
	 */
	protected String toString(String[] strings) {
		StringBuffer toString = new StringBuffer();
		toString.append('[');
		if (strings != null) {
			for (int i = 0; i < strings.length; i++) {
				if (i > 0) toString.append(',');
				toString.append(strings[i]);
			}
		}
		toString.append(']');
		return toString.toString();
	}
}

Back to the top