Skip to content

Commit 1c9d3ea

Browse files
committed
[eclipse-platform#1668] API types to simplify work with launch configuration attributes
* identify launch attribute * connect it with preference metadata (to supply defaults/label/description) * read attribute from configuration * write attribute to configuration working copy
1 parent ca5ed8a commit 1c9d3ea

File tree

5 files changed

+254
-0
lines changed

5 files changed

+254
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 ArSysOp.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Alexander Fedorov (ArSysOp) - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.debug.core;
15+
16+
/**
17+
* Default implementation for {@link LaunchAttributeIdentity}
18+
*
19+
* @since 3.22
20+
*/
21+
public record LauchAttributeIdentityRecord(String id) implements LaunchAttributeIdentity {
22+
23+
/**
24+
* Convenience way to compose full qualified name for launch attribute
25+
*
26+
* @param qualifier usually corresponds to Bundle-Symbolic-Name
27+
* @param key short key to name this very attribute in the scope of
28+
* qualifier
29+
*/
30+
public LauchAttributeIdentityRecord(String qualifier, String key) {
31+
this(qualifier + "." + key); //$NON-NLS-1$
32+
}
33+
34+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 ArSysOp.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Alexander Fedorov (ArSysOp) - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.debug.core;
15+
16+
import org.eclipse.core.runtime.preferences.PreferenceMetadata;
17+
18+
/**
19+
*
20+
* The definition of {@link ILaunchConfiguration} attribute convenience to:
21+
* <ul>
22+
* <li>{@link ILaunchConfiguration#getAttribute(String, String)} and similar
23+
* operations</li>
24+
* <li>{@link ILaunchConfigurationWorkingCopy#setAttribute(String, String)} and
25+
* similar operations</li>
26+
* <li>Connecting {@link ILaunchConfiguration} attributes with preferences</li>
27+
* <li>Representing {@link ILaunchConfiguration} attributes in UI</li>
28+
* </ul>
29+
*
30+
* @see LaunchAttributeRead
31+
* @see LaunchAttributeWrite
32+
*
33+
* @since 3.22
34+
*/
35+
public interface LaunchAttributeDefined<V> {
36+
37+
/**
38+
*
39+
* @return identity for defined attribute
40+
*/
41+
LaunchAttributeIdentity identity();
42+
43+
/**
44+
*
45+
* @return preference metadata for defined attribute
46+
*/
47+
PreferenceMetadata<V> metadata();
48+
49+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 ArSysOp.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Alexander Fedorov (ArSysOp) - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.debug.core;
15+
16+
/**
17+
* Identifies an attribute in {@link ILaunchConfiguration}
18+
*
19+
* @since 3.22
20+
*/
21+
public interface LaunchAttributeIdentity {
22+
23+
/**
24+
* String id of {@link ILaunchConfiguration} attribute for "low-level"
25+
* operations
26+
*
27+
* @return id of attribute
28+
*/
29+
String id();
30+
31+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 ArSysOp.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Alexander Fedorov (ArSysOp) - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.debug.core;
15+
16+
import java.util.List;
17+
import java.util.Map;
18+
import java.util.Objects;
19+
import java.util.Set;
20+
import java.util.function.Supplier;
21+
22+
import org.eclipse.core.runtime.CoreException;
23+
import org.eclipse.core.runtime.Status;
24+
25+
/**
26+
*
27+
* Reads the value of launch attribute
28+
*
29+
* @since 3.22
30+
*/
31+
public final class LaunchAttributeRead<V> {
32+
33+
private final ILaunchConfiguration configuration;
34+
private final String id;
35+
private final Class<V> type;
36+
private final Supplier<V> value;
37+
38+
public LaunchAttributeRead(ILaunchConfiguration configuration, LaunchAttributeDefined<V> defined) {
39+
this(configuration, defined.identity().id(), defined.metadata().valueClass(), defined.metadata()::defaultValue);
40+
}
41+
42+
public LaunchAttributeRead(ILaunchConfiguration configuration, String id, Class<V> type, Supplier<V> value) {
43+
this.configuration = Objects.requireNonNull(configuration);
44+
this.id = Objects.requireNonNull(id);
45+
this.type = Objects.requireNonNull(type);
46+
this.value = Objects.requireNonNull(value);
47+
}
48+
49+
public V get() throws CoreException {
50+
if (String.class.equals(type)) {
51+
return type.cast(configuration.getAttribute(id, String.class.cast(value.get())));
52+
}
53+
if (Boolean.class.equals(type)) {
54+
return type.cast(configuration.getAttribute(id, Boolean.class.cast(value.get())));
55+
}
56+
if (Integer.class.equals(type)) {
57+
return type.cast(configuration.getAttribute(id, Integer.class.cast(value.get())));
58+
}
59+
if (List.class.equals(type)) {
60+
return type.cast(configuration.getAttribute(id, List.class.cast(value.get())));
61+
}
62+
if (Map.class.equals(type)) {
63+
return type.cast(configuration.getAttribute(id, Map.class.cast(value.get())));
64+
}
65+
if (Set.class.equals(type)) {
66+
return type.cast(configuration.getAttribute(id, Set.class.cast(value.get())));
67+
}
68+
throw new CoreException(Status.error(id, new ClassCastException()));
69+
}
70+
71+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 ArSysOp.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Alexander Fedorov (ArSysOp) - initial API and implementation
13+
*******************************************************************************/
14+
15+
package org.eclipse.debug.core;
16+
17+
import java.util.List;
18+
import java.util.Map;
19+
import java.util.Objects;
20+
import java.util.Set;
21+
import java.util.function.Consumer;
22+
import java.util.function.Supplier;
23+
24+
/**
25+
*
26+
* Writes the value of launch attribute. Useful in conjunction with UI.
27+
*
28+
* @since 3.22
29+
*/
30+
public final class LaunchAttributeWrite<V> implements Consumer<ILaunchConfigurationWorkingCopy> {
31+
32+
private final String id;
33+
private final Class<V> type;
34+
private final Supplier<V> value;
35+
36+
public LaunchAttributeWrite(LaunchAttributeDefined<V> defined) {
37+
this(defined.identity().id(), defined.metadata().valueClass(), defined.metadata()::defaultValue);
38+
}
39+
40+
public LaunchAttributeWrite(LaunchAttributeDefined<V> defined, Supplier<V> value) {
41+
this(defined.identity().id(), defined.metadata().valueClass(), value);
42+
}
43+
44+
public LaunchAttributeWrite(String id, Class<V> type, Supplier<V> value) {
45+
this.id = Objects.requireNonNull(id);
46+
this.type = Objects.requireNonNull(type);
47+
this.value = Objects.requireNonNull(value);
48+
}
49+
50+
@Override
51+
public void accept(ILaunchConfigurationWorkingCopy working) {
52+
if (String.class.equals(type)) {
53+
working.setAttribute(id, String.class.cast(value.get()));
54+
} else if (Integer.class.equals(type)) {
55+
working.setAttribute(id, Integer.class.cast(value.get()).intValue());
56+
} else if (Boolean.class.equals(type)) {
57+
working.setAttribute(id, Boolean.class.cast(value.get()).booleanValue());
58+
} else if (List.class.equals(type)) {
59+
working.setAttribute(id, List.class.cast(value.get()));
60+
} else if (Map.class.equals(type)) {
61+
working.setAttribute(id, Map.class.cast(value.get()));
62+
} else if (Set.class.equals(type)) {
63+
working.setAttribute(id, Set.class.cast(value.get()));
64+
} else {
65+
working.setAttribute(id, value.get());
66+
}
67+
}
68+
69+
}

0 commit comments

Comments
 (0)