Skip to content

Commit

Permalink
[FIXUP] Slightly simplify collectInOriginalOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Sep 17, 2023
1 parent b413b96 commit 6c680dd
Showing 1 changed file with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -72,10 +72,10 @@ public static void setClasspath(IProject project, IPluginModelBase model) throws

public static IClasspathEntry[] getClasspath(IProject project, IPluginModelBase model, Map<String, IPath> sourceLibraryMap, boolean clear, boolean overrideCompliance) throws CoreException {
IJavaProject javaProject = JavaCore.create(project);
IClasspathEntry[] originalClasspath = clear ? new IClasspathEntry[0] : javaProject.getRawClasspath();
List<IClasspathEntry> originalClasspath = clear ? List.of() : Arrays.asList(javaProject.getRawClasspath());
ClasspathConfiguration context = new ClasspathConfiguration(model, javaProject,
hasTestPluginName(project), getClasspathAttributes(project, model),
mapFirstSeenByPath(Arrays.stream(originalClasspath)), new ArrayList<>());
mapFirstSeenByPath(originalClasspath.stream()), new ArrayList<>());

// add JRE and set compliance options
String ee = getExecutionEnvironment(model.getBundleDescription());
Expand All @@ -97,24 +97,23 @@ public static IClasspathEntry[] getClasspath(IProject project, IPluginModelBase
return entries;
}

private static IClasspathEntry[] collectInOriginalOrder(IClasspathEntry[] originalClasspath,
private static IClasspathEntry[] collectInOriginalOrder(List<IClasspathEntry> originalClasspath,
List<IClasspathEntry> reloaded) {
// preserve original entries which eventually weren't reloaded
var reloadedPlusOriginal = Stream.concat(reloaded.stream(), Stream.of(originalClasspath));
Map<IPath, IClasspathEntry> resultByPath = mapFirstSeenByPath(reloadedPlusOriginal);
List<IClasspathEntry> result = new ArrayList<>(resultByPath.size());
// using the original order, collect the resulting entries, and remove
// from the map to prevent from any duplicates (even original ones)
Arrays.stream(originalClasspath).map(e -> resultByPath.remove(pathWithoutEE(e.getPath())))
.filter(Objects::nonNull).forEachOrdered(result::add);
Map<IPath, IClasspathEntry> resultingReloadedByPath = mapFirstSeenByPath(reloaded.stream());
List<IClasspathEntry> result = new ArrayList<>(originalClasspath);
result.replaceAll(e -> {
IClasspathEntry replacement = resultingReloadedByPath.remove(pathWithoutEE(e.getPath()));
return replacement != null ? replacement : e;
});
// using the order of reloading, append new entries (in the map still)
reloaded.stream().filter(e -> resultByPath.remove(pathWithoutEE(e.getPath())) != null)
.forEachOrdered(result::add);
result.addAll(resultingReloadedByPath.values());
return result.toArray(IClasspathEntry[]::new);
}

private static Map<IPath, IClasspathEntry> mapFirstSeenByPath(Stream<IClasspathEntry> entryStream) {
return entryStream.collect(Collectors.toMap(e -> pathWithoutEE(e.getPath()), e -> e, (first, dupe) -> first));
return entryStream.collect(
Collectors.toMap(e -> pathWithoutEE(e.getPath()), e -> e, (first, dupe) -> first, LinkedHashMap::new));
}

private static IPath pathWithoutEE(IPath path) {
Expand Down

0 comments on commit 6c680dd

Please sign in to comment.