Simplify comparable behavior of processing stages

Instead of overriding compareTo we can just use an
external comparator
This commit is contained in:
Clément Fournier
2020-01-27 10:33:30 +01:00
parent 7cd64e4374
commit 2db32f8ea6
6 changed files with 23 additions and 26 deletions

View File

@ -72,7 +72,7 @@ public class RulesetStageDependencyHelper {
/** Builds a sorted list of the dependencies of the given ruleset. */
private List<AstProcessingStage<?>> buildDependencyList(RuleSets ruleSets, LanguageVersion languageVersion) {
List<AstProcessingStage<?>> stages = new ArrayList<>(languageVersion.getLanguageVersionHandler().getProcessingStages());
SortedSet<AstProcessingStage<?>> result = new TreeSet<>();
SortedSet<AstProcessingStage<?>> result = new TreeSet<>(AstProcessingStage.COMPARATOR);
// this loops runs until either all stages have already been
// picked or there are no rules left, whichever comes first

View File

@ -6,6 +6,7 @@ package net.sourceforge.pmd.lang.ast;
import static java.util.Collections.emptyList;
import java.util.Comparator;
import java.util.List;
import net.sourceforge.pmd.RuleSets;
@ -58,6 +59,11 @@ import net.sourceforge.pmd.lang.LanguageVersionHandler;
@Experimental
public interface AstProcessingStage<T extends AstProcessingStage<T>> extends Comparable<T> {
/**
* Compares processing stages of possibly different kinds.
*/
Comparator<AstProcessingStage<?>> COMPARATOR = AstProcessingStage::compare;
/**
* Returns the language this processing stage applies to.
@ -96,6 +102,20 @@ public interface AstProcessingStage<T extends AstProcessingStage<T>> extends Com
void processAST(RootNode rootNode, AstAnalysisContext configuration);
@Override
int compareTo(AstProcessingStage o);
/**
* Same contract as {@link Comparable#compareTo(Object)}, but we can't extend
* Comparable with that type argument if we implement processing stages within
* an enum.
*
* @param t the object to compare
*
* @return a negative integer, zero, or a positive integer as this object
* is less than, equal to, or greater than the specified object.
*/
@SuppressWarnings("unchecked")
default int compare(AstProcessingStage<?> t) {
return this.compareTo((T) t);
}
}

View File

@ -43,12 +43,6 @@ public enum DummyAstStages implements AstProcessingStage<DummyAstStages> {
return name();
}
@Override
public int compareTo(AstProcessingStage o) {
return o instanceof DummyAstStages ? compareTo((DummyAstStages) o)
: 0;
}
@Override
public void processAST(RootNode rootNode, AstAnalysisContext configuration) {
((DummyNode) rootNode).getUserData().put(name() + "_STAGE", "done");

View File

@ -89,12 +89,6 @@ public enum JavaProcessingStage implements AstProcessingStage<JavaProcessingStag
this.dependencies = Collections.unmodifiableList(Arrays.asList(dependencies));
}
@Override
public int compareTo(AstProcessingStage o) {
return o instanceof JavaProcessingStage ? compareTo((JavaProcessingStage) o)
: 0;
}
@Override
public List<JavaProcessingStage> getDependencies() {
return dependencies;

View File

@ -30,11 +30,6 @@ public enum ModelicaProcessingStage implements AstProcessingStage<ModelicaProces
this.displayName = displayName;
}
@Override
public int compareTo(AstProcessingStage o) {
return o instanceof ModelicaProcessingStage ? compareTo((ModelicaProcessingStage) o)
: 0;
}
@Override
public Language getLanguage() {

View File

@ -76,12 +76,6 @@ public enum PlsqlProcessingStage implements AstProcessingStage<PlsqlProcessingSt
return displayName;
}
@Override
public int compareTo(AstProcessingStage o) {
return o instanceof PlsqlProcessingStage ? compareTo((PlsqlProcessingStage) o)
: 0;
}
}