Sort supported languages by name by default, so that we have

a consistent order.
This commit is contained in:
Andreas Dangel
2017-02-24 11:31:22 +01:00
parent f677427ae8
commit 00540a8e45

View File

@ -5,7 +5,9 @@ package net.sourceforge.pmd.lang;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.ServiceLoader; import java.util.ServiceLoader;
@ -20,9 +22,24 @@ public final class LanguageRegistry {
private Map<String, Language> languages; private Map<String, Language> languages;
private LanguageRegistry() { private LanguageRegistry() {
languages = new HashMap<>(); List<Language> languagesList = new ArrayList<>();
ServiceLoader<Language> languageLoader = ServiceLoader.load(Language.class); ServiceLoader<Language> languageLoader = ServiceLoader.load(Language.class);
for (Language language : languageLoader) { for (Language language : languageLoader) {
languagesList.add(language);
}
// sort languages by name. Avoiding differences in the order of languages
// across JVM versions / OS.
Collections.sort(languagesList, new Comparator<Language>() {
@Override
public int compare(Language o1, Language o2) {
return o1.getName().compareToIgnoreCase(o2.getName());
}
});
// using a linked hash map to maintain insertion order
languages = new LinkedHashMap<>();
for (Language language : languagesList) {
languages.put(language.getName(), language); languages.put(language.getName(), language);
} }
} }