From f9dedd8f9b5bf7a395e024fef725561e6222d464 Mon Sep 17 00:00:00 2001 From: Simon Barner <barner@fortiss.org> Date: Fri, 5 Oct 2018 11:35:53 +0200 Subject: [PATCH] selectAll(): make private and return empty list instead of null * Return empty list instead of null on unexpected input (null or wrong selection type) * Fixes NPE in setSelection() Issue-Ref: 3545 Issue-Url: https://af3-developer.fortiss.org/issues/3545 Signed-off-by: Simon Barner <barner@fortiss.org> --- .../fortiss/tooling/kernel/ui/util/.ratings | 2 +- .../kernel/ui/util/SelectionUtils.java | 23 ++++++++----------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/util/.ratings b/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/util/.ratings index 9066153dc..08d20c02e 100644 --- a/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/util/.ratings +++ b/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/util/.ratings @@ -7,7 +7,7 @@ EObjectSelectionUtils.java 128cf8f96c6b9478171dff3deda662d5934f5f44 GREEN KernelUIUtils.java 46d3279ef3523b104f89a6c526109f72d36f72f2 GREEN ObservableUtils.java 34abfd1dfaf9c0acbb31caf1f525e7b39416c116 GREEN PropertiesConstantUtils.java 59b1a1e4d594bb98db3aa396f2ff6474ba405920 GREEN -SelectionUtils.java 3d20f87eaaee04173686ef62b09ca6971702cd00 GREEN +SelectionUtils.java 33aec7cccccb28e5568140cf8e5443ce0f9f59f7 YELLOW TutorialUIServiceUtils.java 093a8a3549c6952d44ea508e66691434b17a95b5 GREEN UndoRedoImpl.java f218500875bda0ef52f4cc2ccdf452825e6751f7 GREEN WidgetsFactory.java 5be121cc81e93731f4d0ab11e7707417fa950c2c GREEN diff --git a/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/util/SelectionUtils.java b/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/util/SelectionUtils.java index 3d20f87ea..33aec7ccc 100644 --- a/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/util/SelectionUtils.java +++ b/org.fortiss.tooling.kernel.ui/src/org/fortiss/tooling/kernel/ui/util/SelectionUtils.java @@ -15,9 +15,11 @@ +--------------------------------------------------------------------------*/ package org.fortiss.tooling.kernel.ui.util; +import static java.util.Collections.emptyList; import static org.conqat.ide.commons.ui.ui.WorkbenchUtils.getActiveEditor; +import static org.fortiss.tooling.common.util.LambdaUtils.filterByType; -import java.util.ArrayList; +import java.util.Collection; import java.util.List; import org.eclipse.emf.ecore.EObject; @@ -90,29 +92,24 @@ public class SelectionUtils { } /** - * Returns a list of all the selected elements that have the given type or - * null if selection is null or is not an IStructuredSelection. + * Returns a {@link Collection} of all the selected elements that have the given type (possibly + * an empty list, in particular if {@code selection} is {@code null} or is not an + * {@link IStructuredSelection}. * * @param selection * the current selection * @param type * the type of elements to be returned - * @return the list of elements of the given type + * @return the {@link Collection} of elements of the given type */ @SuppressWarnings("unchecked") - public static <T> List<T> selectAll(ISelection selection, Class<T> type) { + private static <T> Collection<T> selectAll(ISelection selection, Class<T> type) { if(selection == null || !(selection instanceof IStructuredSelection)) { - return null; + return emptyList(); } IStructuredSelection structuredSelection = (IStructuredSelection)selection; - List<T> elements = new ArrayList<T>(); - for(Object element : structuredSelection.toList()) { - if(type.isInstance(element)) { - elements.add((T)element); - } - } - return elements; + return filterByType(structuredSelection.toList(), type); } /** -- GitLab