Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Minimize 'locatePom()'-calls in PolyglotModelManager #242

Merged
merged 1 commit into from
Aug 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -42,17 +41,12 @@ public void addMapping(final Mapping mapping) {
mappings.add(mapping);
}

private static final Comparator<Mapping> DESCENDING_PRIORITY = Comparator.comparingDouble(Mapping::getPriority).reversed();

public List<Mapping> getSortedMappings() {
List<Mapping> sortedMappings = new ArrayList<Mapping>(mappings);

Collections.sort(sortedMappings, Collections.reverseOrder(new Comparator<Mapping>() {
@Override
public int compare(Mapping o1, Mapping o2) {
return Float.compare(o1.getPriority(), o2.getPriority());
}
}));

return sortedMappings;
List<Mapping> sortedMappings = new ArrayList<>(mappings);
sortedMappings.sort(DESCENDING_PRIORITY);
return sortedMappings;
}

public ModelReader getReaderFor(final Map<String, ?> options) {
Expand Down Expand Up @@ -84,36 +78,27 @@ public ModelWriter getWriterFor(final Map<String, ?> options) {
throw new RuntimeException("Unable to determine model output format; options=" + options);
}

public File findPom(final File dir) {
public File findPom(final File dir) {
assert dir != null;

File pomFile = null;
float currentPriority = Float.MIN_VALUE;
for (Mapping mapping : mappings) {
for (Mapping mapping : getSortedMappings()) {
File file = mapping.locatePom(dir);
if (file != null && (pomFile == null || mapping.getPriority() > currentPriority)) {
pomFile = file;
currentPriority = mapping.getPriority();
if (file != null) {
return file;
}
}

return pomFile;
return null;
}

public String determineFlavourFromPom(final File dir) {
assert dir != null;

String flavour = null;
float mappingPriority = Float.MIN_VALUE;
for (Mapping mapping : mappings) {
for (Mapping mapping : getSortedMappings()) {
File file = mapping.locatePom(dir);
if (file != null && (flavour == null || mappingPriority < mapping.getPriority())) {
flavour = mapping.getFlavour();
mappingPriority = mapping.getPriority();
if (file != null) {
return mapping.getFlavour();
}
}

return flavour;
return null;
}

public String getFlavourFor(final Map<String, ?> options) {
Expand Down