From 783a0777af582e10bace64cc8edd475acd3970f8 Mon Sep 17 00:00:00 2001 From: Hannes Wellmann Date: Sun, 28 Jul 2024 09:47:43 +0200 Subject: [PATCH] Return a SortedSet in getAllJavaSourceVersionsSupportedByCompiler() The list current returned by JavaCore.getAllJavaSourceVersionsSupportedByCompiler() is mainly/only used to either fetch the first or last element or test if a version-String is contained. Especially for the last operation a Set is a better choice regarding runtime. A SortedSet additionally also offers the methods first()/last(), which are semantically more expressive than calling List.get(0)/List.get(List.size()-1). Furthermore using a SortedSet would also better reflect the general semantic of the returned collection and element-access is usually not of interest. --- .../model/org/eclipse/jdt/core/JavaCore.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/core/JavaCore.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/core/JavaCore.java index 5d09e34d7ae..f43def56fec 100644 --- a/org.eclipse.jdt.core/model/org/eclipse/jdt/core/JavaCore.java +++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/core/JavaCore.java @@ -121,6 +121,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Hashtable; @@ -128,6 +129,8 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; import java.util.stream.Collectors; import java.util.stream.Stream; import java.util.zip.ZipFile; @@ -3336,12 +3339,19 @@ public final class JavaCore extends Plugin { * Ordered set (from oldest to latest) of all Java source versions supported by compiler. * The values are from {@link JavaCore}{@code #VERSION_*}. */ - private static final List SUPPORTED_VERSIONS; + private static final SortedSet SUPPORTED_VERSIONS; static { - ArrayList temp = new ArrayList<>(); + Comparator byVersion = Comparator.comparingDouble((String v) -> { + try { + return Double.parseDouble(v); + } catch (RuntimeException e) { + return 0; + } + }).thenComparing(Comparator.naturalOrder()); + SortedSet temp = new TreeSet<>(byVersion); temp.addAll(allVersions); temp.removeAll(UNSUPPORTED_VERSIONS); - SUPPORTED_VERSIONS = Collections.unmodifiableList(temp); + SUPPORTED_VERSIONS = Collections.unmodifiableSortedSet(temp); } /** @@ -3367,7 +3377,7 @@ public static List getAllVersions() { * @see #getFirstJavaSourceVersionSupportedByCompiler() * @since 3.39 */ - public static List getAllJavaSourceVersionsSupportedByCompiler() { + public static SortedSet getAllJavaSourceVersionsSupportedByCompiler() { return SUPPORTED_VERSIONS; } @@ -6472,7 +6482,7 @@ public static String latestSupportedJavaVersion() { * @since 3.39 */ public static String getFirstJavaSourceVersionSupportedByCompiler() { - return SUPPORTED_VERSIONS.get(0); + return SUPPORTED_VERSIONS.first(); } /**