Skip to content

Commit be6e2f9

Browse files
committed
Add stream support to IStructuredSelection
Streams offer much more flexibility as they can be filtered, mapped and finally collected to any useful representation of the data. This adds a new stream() method to IStructuredSelection so consumers of selections can benefit from this without any additional casting or boilerplate code (example of code simplification included in this PR), implement it as a default method for backward compatibility of implementors and adjust the implementation to return a slightly more efficient variant.
1 parent 9cddca3 commit be6e2f9

File tree

5 files changed

+44
-4
lines changed

5 files changed

+44
-4
lines changed

bundles/org.eclipse.jface/.settings/.api_filters

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
22
<component id="org.eclipse.jface" version="2">
3+
<resource path="META-INF/MANIFEST.MF">
4+
<filter id="926941240">
5+
<message_arguments>
6+
<message_argument value="3.32.0"/>
7+
<message_argument value="3.31.0"/>
8+
</message_arguments>
9+
</filter>
10+
</resource>
11+
<resource path="src/org/eclipse/jface/viewers/IStructuredSelection.java" type="org.eclipse.jface.viewers.IStructuredSelection">
12+
<filter id="404000815">
13+
<message_arguments>
14+
<message_argument value="org.eclipse.jface.viewers.IStructuredSelection"/>
15+
<message_argument value="stream()"/>
16+
</message_arguments>
17+
</filter>
18+
</resource>
319
<resource path="src/org/eclipse/jface/viewers/ViewerCell.java" type="org.eclipse.jface.viewers.ViewerCell">
420
<filter comment="constants should be final" id="388100214">
521
<message_arguments>

bundles/org.eclipse.jface/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %pluginName
44
Bundle-SymbolicName: org.eclipse.jface;singleton:=true
5-
Bundle-Version: 3.31.100.qualifier
5+
Bundle-Version: 3.32.0.qualifier
66
Bundle-Vendor: %providerName
77
Bundle-Localization: plugin
88
Export-Package: org.eclipse.jface,

bundles/org.eclipse.jface/src/org/eclipse/jface/dialogs/AbstractSelectionDialog.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ protected void setResult(ISelection selection, Class<T> target) {
220220
List<T> selected = null;
221221
if (selection instanceof IStructuredSelection && target != null) {
222222
IStructuredSelection structured = (IStructuredSelection) selection;
223-
selected = ((List<?>) structured.toList()).stream().filter(target::isInstance)
223+
selected = structured.stream().filter(target::isInstance)
224224
.map(target::cast).collect(Collectors.toList());
225225
}
226226
setResult(selected);

bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/IStructuredSelection.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2015 IBM Corporation and others.
2+
* Copyright (c) 2000, 2023 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -10,11 +10,14 @@
1010
*
1111
* Contributors:
1212
* IBM Corporation - initial API and implementation
13+
* Christoph Läubrich - add support for stream() method
1314
*******************************************************************************/
1415
package org.eclipse.jface.viewers;
1516

1617
import java.util.Iterator;
1718
import java.util.List;
19+
import java.util.stream.Stream;
20+
import java.util.stream.StreamSupport;
1821

1922
/**
2023
* A selection containing elements.
@@ -57,4 +60,15 @@ public interface IStructuredSelection extends ISelection, Iterable {
5760
* @return the selected elements as a list
5861
*/
5962
public List toList();
63+
64+
/**
65+
* Returns the elements in this selection as a <code>Stream</code>.
66+
*
67+
* @return the selected elements as a stream
68+
* @since 3.32
69+
*/
70+
@SuppressWarnings("unchecked")
71+
default Stream<Object> stream() {
72+
return StreamSupport.stream(spliterator(), false);
73+
}
6074
}

bundles/org.eclipse.jface/src/org/eclipse/jface/viewers/StructuredSelection.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Iterator;
1919
import java.util.List;
2020
import java.util.Objects;
21+
import java.util.stream.Stream;
2122

2223
import org.eclipse.core.runtime.Assert;
2324
import org.eclipse.jface.resource.JFaceResources;
@@ -207,6 +208,14 @@ public List toList() {
207208
return Arrays.asList(elements == null ? new Object[0] : elements);
208209
}
209210

211+
@Override
212+
public Stream<Object> stream() {
213+
if (isEmpty()) {
214+
return Stream.empty();
215+
}
216+
return Arrays.stream(elements);
217+
}
218+
210219
/**
211220
* Internal method which returns a string representation of this
212221
* selection suitable for debug purposes only.
@@ -215,6 +224,7 @@ public List toList() {
215224
*/
216225
@Override
217226
public String toString() {
218-
return isEmpty() ? JFaceResources.getString("<empty_selection>") : toList().toString(); //$NON-NLS-1$
227+
return isEmpty() ? JFaceResources.getString("<empty_selection>") //$NON-NLS-1$
228+
: Arrays.toString(elements);
219229
}
220230
}

0 commit comments

Comments
 (0)