Skip to content

Commit b47f9cd

Browse files
committed
Continue #2027: use Optional find instead of nullable get`
* see #2027 (comment)
1 parent de16eb0 commit b47f9cd

File tree

6 files changed

+50
-86
lines changed

6 files changed

+50
-86
lines changed

terminal/bundles/org.eclipse.terminal.view.ui/src/org/eclipse/terminal/view/ui/internal/LauncherDelegateManager.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.HashMap;
2020
import java.util.List;
2121
import java.util.Map;
22+
import java.util.Optional;
2223

2324
import org.eclipse.core.expressions.EvaluationContext;
2425
import org.eclipse.core.expressions.EvaluationResult;
@@ -243,16 +244,15 @@ public List<ILauncherDelegate> getLauncherDelegates(boolean unique) {
243244
}
244245

245246
/**
246-
* Returns the terminal launcher delegate identified by its unique id. If no terminal
247-
* launcher delegate with the specified id is registered, <code>null</code> is returned.
247+
* Lookup a terminal launcher delegate identified by its unique id.
248248
*
249249
* @param id The unique id of the terminal launcher delegate or <code>null</code>
250250
* @param unique If <code>true</code>, the method returns new instances of the terminal launcher delegate contribution.
251251
*
252-
* @return The terminal launcher delegate instance or <code>null</code>.
252+
* @return The terminal launcher delegate instance or an empty optional if not found.
253253
*/
254254
@Override
255-
public ILauncherDelegate getLauncherDelegate(String id, boolean unique) {
255+
public Optional<ILauncherDelegate> findLauncherDelegate(String id, boolean unique) {
256256
ILauncherDelegate contribution = null;
257257
Map<String, Proxy> extensions = getExtensions();
258258
if (extensions.containsKey(id)) {
@@ -261,7 +261,7 @@ public ILauncherDelegate getLauncherDelegate(String id, boolean unique) {
261261
contribution = unique ? proxy.newInstance() : proxy.getInstance();
262262
}
263263

264-
return contribution;
264+
return Optional.ofNullable(contribution);
265265
}
266266

267267
/**

terminal/bundles/org.eclipse.terminal.view.ui/src/org/eclipse/terminal/view/ui/internal/TerminalService.java

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import java.util.HashMap;
1616
import java.util.Map;
17+
import java.util.Optional;
1718

1819
import org.eclipse.core.runtime.Assert;
1920
import org.eclipse.core.runtime.ISafeRunnable;
@@ -28,7 +29,6 @@
2829
import org.eclipse.terminal.view.core.ITerminalsConnectorConstants;
2930
import org.eclipse.terminal.view.ui.IUIConstants;
3031
import org.eclipse.terminal.view.ui.TerminalViewId;
31-
import org.eclipse.terminal.view.ui.launcher.ILauncherDelegate;
3232
import org.eclipse.terminal.view.ui.launcher.ITerminalConsoleViewManager;
3333
import org.eclipse.ui.PlatformUI;
3434
import org.osgi.service.component.annotations.Activate;
@@ -240,22 +240,10 @@ protected String normalizeTitle(String title, Object data) {
240240
*/
241241
protected ITerminalConnector createTerminalConnector(Map<String, Object> properties) {
242242
Assert.isNotNull(properties);
243-
244-
// The terminal connector result object
245-
ITerminalConnector connector = null;
246-
247-
// Get the launcher delegate id from the properties
248-
String delegateId = (String) properties.get(ITerminalsConnectorConstants.PROP_DELEGATE_ID);
249-
if (delegateId != null) {
250-
// Get the launcher delegate
251-
ILauncherDelegate delegate = UIPlugin.getLaunchDelegateManager().getLauncherDelegate(delegateId, false);
252-
if (delegate != null) {
253-
// Create the terminal connector
254-
connector = delegate.createTerminalConnector(properties);
255-
}
256-
}
257-
258-
return connector;
243+
return Optional.of(properties).map(map -> map.get(ITerminalsConnectorConstants.PROP_DELEGATE_ID))
244+
.filter(String.class::isInstance).map(String.class::cast)
245+
.flatMap(id -> UIPlugin.getLaunchDelegateManager().findLauncherDelegate(id, false))
246+
.map(d -> d.createTerminalConnector(properties)).orElse(null);
259247
}
260248

261249
@Override

terminal/bundles/org.eclipse.terminal.view.ui/src/org/eclipse/terminal/view/ui/internal/handler/LaunchTerminalCommandHandler.java

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
import java.util.HashMap;
1818
import java.util.List;
1919
import java.util.Map;
20+
import java.util.Optional;
2021

2122
import org.eclipse.core.commands.AbstractHandler;
2223
import org.eclipse.core.commands.ExecutionEvent;
2324
import org.eclipse.core.commands.ExecutionException;
24-
import org.eclipse.core.runtime.Assert;
2525
import org.eclipse.jface.viewers.ISelection;
2626
import org.eclipse.jface.viewers.IStructuredSelection;
2727
import org.eclipse.jface.window.Window;
@@ -61,16 +61,7 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
6161
if (commandId.equals("org.eclipse.terminal.view.ui.command.launchConsole")) { //$NON-NLS-1$
6262
LaunchTerminalSettingsDialog dialog = new LaunchTerminalSettingsDialog(shell, start);
6363
if (dialog.open() == Window.OK) {
64-
// Get the terminal settings from the dialog
65-
Map<String, Object> properties = dialog.getSettings();
66-
if (properties != null) {
67-
String delegateId = (String) properties.get(ITerminalsConnectorConstants.PROP_DELEGATE_ID);
68-
Assert.isNotNull(delegateId);
69-
ILauncherDelegate delegate = UIPlugin.getLaunchDelegateManager().getLauncherDelegate(delegateId,
70-
false);
71-
Assert.isNotNull(delegateId);
72-
return delegate.createTerminalConnector(properties);
73-
}
64+
return findDelegate(dialog).map(delegate -> delegate.createTerminalConnector(dialog.getSettings()));
7465
}
7566
return null;
7667
}
@@ -87,16 +78,7 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
8778
dialog.setSelection(selection);
8879
}
8980
if (dialog.open() == Window.OK) {
90-
// Get the terminal settings from the dialog
91-
Map<String, Object> properties = dialog.getSettings();
92-
if (properties != null) {
93-
String delegateId = (String) properties.get(ITerminalsConnectorConstants.PROP_DELEGATE_ID);
94-
Assert.isNotNull(delegateId);
95-
ILauncherDelegate delegate = UIPlugin.getLaunchDelegateManager().getLauncherDelegate(delegateId,
96-
false);
97-
Assert.isNotNull(delegateId);
98-
delegate.execute(properties, null);
99-
}
81+
findDelegate(dialog).ifPresent(delegate -> delegate.execute(dialog.getSettings(), null));
10082
}
10183
} else {
10284
if (UIPlugin.getTraceHandler().isSlotEnabled(0, ITraceIds.TRACE_LAUNCH_TERMINAL_COMMAND_HANDLER)) {
@@ -128,16 +110,7 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
128110
dialog.setSelection(selection);
129111
}
130112
if (dialog.open() == Window.OK) {
131-
// Get the terminal settings from the dialog
132-
Map<String, Object> properties = dialog.getSettings();
133-
if (properties != null) {
134-
String delegateId = (String) properties.get(ITerminalsConnectorConstants.PROP_DELEGATE_ID);
135-
Assert.isNotNull(delegateId);
136-
ILauncherDelegate delegate = UIPlugin.getLaunchDelegateManager().getLauncherDelegate(delegateId,
137-
false);
138-
Assert.isNotNull(delegateId);
139-
delegate.execute(properties, null);
140-
}
113+
findDelegate(dialog).ifPresent(delegate -> delegate.execute(dialog.getSettings(), null));
141114
}
142115
} else if (delegates.size() == 1) {
143116
ILauncherDelegate delegate = delegates.get(0);
@@ -156,6 +129,14 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
156129
return null;
157130
}
158131

132+
private final Optional<ILauncherDelegate> findDelegate(LaunchTerminalSettingsDialog dialog) {
133+
return Optional.ofNullable(dialog.getSettings())
134+
.map(map -> map.get(ITerminalsConnectorConstants.PROP_DELEGATE_ID)).filter(String.class::isInstance)
135+
.map(String.class::cast)
136+
.flatMap(id -> UIPlugin.getLaunchDelegateManager().findLauncherDelegate(id, false));
137+
138+
}
139+
159140
private boolean isValidSelection(ISelection selection) {
160141
if (selection instanceof IStructuredSelection && !selection.isEmpty()) {
161142
Object element = ((IStructuredSelection) selection).getFirstElement();

terminal/bundles/org.eclipse.terminal.view.ui/src/org/eclipse/terminal/view/ui/internal/local/showin/DynamicContributionItems.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2014, 2021 Wind River Systems, Inc. and others. All rights reserved.
2+
* Copyright (c) 2014, 2025 Wind River Systems, Inc. and others. All rights reserved.
33
* This program and the accompanying materials are made available under the terms
44
* of the Eclipse Public License 2.0 which accompanies this distribution, and is
55
* available at https://www.eclipse.org/legal/epl-2.0/
@@ -8,6 +8,7 @@
88
*
99
* Contributors:
1010
* Wind River Systems - initial API and implementation
11+
* Alexander Fedorov (ArSysOp) - further evolution
1112
*******************************************************************************/
1213
package org.eclipse.terminal.view.ui.internal.local.showin;
1314

@@ -49,7 +50,7 @@ public void initialize(IServiceLocator serviceLocator) {
4950

5051
// Get the local terminal launcher delegate
5152
delegate = UIPlugin.getLaunchDelegateManager()
52-
.getLauncherDelegate("org.eclipse.terminal.connector.local.launcher.local", false); //$NON-NLS-1$
53+
.findLauncherDelegate("org.eclipse.terminal.connector.local.launcher.local", false).orElse(null); //$NON-NLS-1$
5354
}
5455

5556
@Override

terminal/bundles/org.eclipse.terminal.view.ui/src/org/eclipse/terminal/view/ui/internal/view/TerminalsViewMementoHandler.java

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2012, 2018 Wind River Systems, Inc. and others. All rights reserved.
2+
* Copyright (c) 2012, 2025 Wind River Systems, Inc. and others. All rights reserved.
33
* This program and the accompanying materials are made available under the terms
44
* of the Eclipse Public License 2.0 which accompanies this distribution, and is
55
* available at https://www.eclipse.org/legal/epl-2.0/
@@ -8,6 +8,7 @@
88
*
99
* Contributors:
1010
* Wind River Systems - initial API and implementation
11+
* Alexander Fedorov (ArSysOp) - further evolution
1112
*******************************************************************************/
1213
package org.eclipse.terminal.view.ui.internal.view;
1314

@@ -81,18 +82,13 @@ public void saveState(TerminalsView view, IMemento memento) {
8182
}
8283

8384
// Get the terminal launcher delegate
84-
String delegateId = (String) properties.get(ITerminalsConnectorConstants.PROP_DELEGATE_ID);
85-
ILauncherDelegate delegate = delegateId != null
86-
? UIPlugin.getLaunchDelegateManager().getLauncherDelegate(delegateId, false)
87-
: null;
88-
IMementoHandler mementoHandler = delegate != null
89-
? (IMementoHandler) delegate.getAdapter(IMementoHandler.class)
90-
: null;
91-
if (mementoHandler != null) {
85+
Optional<IMementoHandler> mementoHandler = findDelegate(properties).flatMap(this::mementoHandler);
86+
if (mementoHandler.isPresent()) {
9287
// Create terminal connection child memento
9388
IMemento connectionMemento = memento.createChild("connection"); //$NON-NLS-1$
9489
Assert.isNotNull(connectionMemento);
9590
// Store the common attributes
91+
String delegateId = (String) properties.get(ITerminalsConnectorConstants.PROP_DELEGATE_ID);
9692
connectionMemento.putString(ITerminalsConnectorConstants.PROP_DELEGATE_ID, delegateId);
9793

9894
String terminalConnectorId = (String) properties
@@ -129,7 +125,7 @@ public void saveState(TerminalsView view, IMemento memento) {
129125
}
130126

131127
// Pass on to the memento handler
132-
mementoHandler.saveState(connectionMemento, properties);
128+
mementoHandler.get().saveState(connectionMemento, properties);
133129
}
134130
}
135131
}
@@ -185,28 +181,26 @@ protected void restoreState(final TerminalsView view, IMemento memento) {
185181
properties.put(ITerminalsConnectorConstants.PROP_PROCESS_WORKING_DIR,
186182
connection.getString(ITerminalsConnectorConstants.PROP_PROCESS_WORKING_DIR));
187183
}
188-
189-
// Get the terminal launcher delegate
190-
String delegateId = (String) properties.get(ITerminalsConnectorConstants.PROP_DELEGATE_ID);
191-
ILauncherDelegate delegate = delegateId != null
192-
? UIPlugin.getLaunchDelegateManager().getLauncherDelegate(delegateId, false)
193-
: null;
194-
IMementoHandler mementoHandler = delegate != null
195-
? (IMementoHandler) delegate.getAdapter(IMementoHandler.class)
196-
: null;
197-
if (mementoHandler != null) {
198-
// Pass on to the memento handler
199-
mementoHandler.restoreState(connection, properties);
200-
}
201-
184+
Optional<ILauncherDelegate> delegate = findDelegate(properties);
185+
// Pass on to the memento handler
186+
delegate.map(d -> d.getAdapter(IMementoHandler.class))
187+
.ifPresent(mh -> mh.restoreState(connection, properties));
202188
// Restore the terminal connection
203-
if (delegate != null && !properties.isEmpty()) {
204-
delegate.execute(properties, null);
205-
}
189+
delegate.ifPresent(d -> d.execute(properties, null));
206190
}
207191
}
208192
}
209193

194+
private Optional<ILauncherDelegate> findDelegate(Map<String, Object> properties) {
195+
return Optional.of(properties).map(map -> map.get(ITerminalsConnectorConstants.PROP_DELEGATE_ID))
196+
.filter(String.class::isInstance).map(String.class::cast)
197+
.flatMap(id -> UIPlugin.getLaunchDelegateManager().findLauncherDelegate(id, false));
198+
}
199+
200+
private Optional<IMementoHandler> mementoHandler(ILauncherDelegate delegate) {
201+
return Optional.ofNullable(delegate.getAdapter(IMementoHandler.class));
202+
}
203+
210204
/**
211205
* Executes the given runnable asynchronously in the display thread.
212206
*

terminal/bundles/org.eclipse.terminal.view.ui/src/org/eclipse/terminal/view/ui/launcher/ILaunchDelegateManager.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package org.eclipse.terminal.view.ui.launcher;
1515

1616
import java.util.List;
17+
import java.util.Optional;
1718

1819
import org.eclipse.jface.viewers.ISelection;
1920

@@ -30,15 +31,14 @@ public interface ILaunchDelegateManager {
3031
List<ILauncherDelegate> getLauncherDelegates(boolean unique);
3132

3233
/**
33-
* Returns the terminal launcher delegate identified by its unique id. If no terminal
34-
* launcher delegate with the specified id is registered, <code>null</code> is returned.
34+
* Lookup a terminal launcher delegate identified by its unique id.
3535
*
3636
* @param id The unique id of the terminal launcher delegate or <code>null</code>
3737
* @param unique If <code>true</code>, the method returns new instances of the terminal launcher delegate contribution.
3838
*
39-
* @return The terminal launcher delegate instance or <code>null</code>.
39+
* @return The terminal launcher delegate instance or an empty optional if not found.
4040
*/
41-
ILauncherDelegate getLauncherDelegate(String id, boolean unique);
41+
Optional<ILauncherDelegate> findLauncherDelegate(String id, boolean unique);
4242

4343
/**
4444
* Returns the applicable terminal launcher delegates for the given selection.

0 commit comments

Comments
 (0)