From 0b8b5826cb2d760ede8f23a3adc9f38e9e05daa4 Mon Sep 17 00:00:00 2001 From: AlexElin Date: Sun, 26 Apr 2020 12:38:11 +0300 Subject: [PATCH] use java 8 features: List.sort, Comparator.comparing and others (#757) * use java 8 features: List.sort, Comparator.comparing and others * use java 8 features: remove unused import * use java 8 features: remove unused imports * fix NPE and remove useless null check * fix indent and double copying * remove unused import * remove numberCoveredLines BiFunction rename getTestsForClassName -> getLineCoverageForClassName --- .../pitest/bytecode/analysis/MethodTree.java | 11 +++---- .../org/pitest/coverage/CoverageData.java | 33 ++++++------------- .../mutationtest/MutationStatusMap.java | 3 +- .../build/CompoundMutationInterceptor.java | 19 ++++------- .../build/DefaultTestPrioritiser.java | 10 ++---- .../build/MutationTestBuilder.java | 12 +++---- .../InlinedFinallyBlockFilter.java | 7 ++-- .../mutationtest/config/SettingsFactory.java | 2 +- .../execute/MutationAnalysisExecutor.java | 7 ++-- .../MutationStatisticsPrecursor.java | 2 +- .../tooling/DirectorySourceLocator.java | 4 +-- .../verify/DefaultBuildVerifier.java | 2 +- .../build/AnalyisPriorityComparatorTest.java | 5 ++- .../TestInfoPriorisationComparatorTest.java | 3 +- .../pitest/mutationtest/report/html/Line.java | 3 +- .../report/html/MutationResultList.java | 8 ++--- ...TestSummaryDataFileNameComparatorTest.java | 2 +- .../report/html/ResultComparatorTest.java | 3 +- .../analysis/ControlFlowAnalyser.java | 5 +-- .../coverage/execute/CoverageWorker.java | 18 ++++------ .../dependency/DependencyExtractor.java | 10 +----- .../org/pitest/functional/FCollection.java | 6 ---- .../engine/gregor/config/Mutator.java | 5 ++- .../pitest/functional/FCollectionTest.java | 2 +- .../functional/prelude/PreludeTest.java | 2 -- .../java/org/pitest/util/StringUtilTest.java | 3 -- 26 files changed, 58 insertions(+), 129 deletions(-) diff --git a/pitest-entry/src/main/java/org/pitest/bytecode/analysis/MethodTree.java b/pitest-entry/src/main/java/org/pitest/bytecode/analysis/MethodTree.java index 5d43c5766..675538944 100644 --- a/pitest-entry/src/main/java/org/pitest/bytecode/analysis/MethodTree.java +++ b/pitest-entry/src/main/java/org/pitest/bytecode/analysis/MethodTree.java @@ -1,9 +1,7 @@ package org.pitest.bytecode.analysis; import java.util.ArrayList; -import java.util.LinkedList; import java.util.List; -import java.util.ListIterator; import org.objectweb.asm.Opcodes; import org.objectweb.asm.tree.AbstractInsnNode; @@ -73,12 +71,11 @@ public List annotations() { } private List createInstructionList() { - final List list = new LinkedList<>(); - final ListIterator it = this.rawNode.instructions.iterator(); - while (it.hasNext()) { - list.add(it.next()); + final List list = new ArrayList<>(); + for (AbstractInsnNode abstractInsnNode : this.rawNode.instructions) { + list.add(abstractInsnNode); } - this.lazyInstructions = new ArrayList<>(list); + this.lazyInstructions = list; return this.lazyInstructions; } diff --git a/pitest-entry/src/main/java/org/pitest/coverage/CoverageData.java b/pitest-entry/src/main/java/org/pitest/coverage/CoverageData.java index 69ee3585b..f91735caf 100644 --- a/pitest-entry/src/main/java/org/pitest/coverage/CoverageData.java +++ b/pitest-entry/src/main/java/org/pitest/coverage/CoverageData.java @@ -74,14 +74,13 @@ public CoverageData(final CodeSource code, final LineMap lm, Map getTestsForInstructionLocation( - InstructionLocation location) { - return this.instructionCoverage.get(location); + public Collection getTestsForInstructionLocation(InstructionLocation location) { + return this.instructionCoverage.getOrDefault(location, Collections.emptySet()); } @Override public Collection getTestsForClassLine(final ClassLine classLine) { - final Collection result = getTestsForClassName( + final Collection result = getLineCoverageForClassName( classLine.getClassName()).get(classLine); if (result == null) { return Collections.emptyList(); @@ -109,7 +108,10 @@ public Collection getClassInfo(final Collection classes) { @Override public int getNumberOfCoveredLines(final Collection mutatedClass) { - return FCollection.fold(numberCoveredLines(), 0, mutatedClass); + return mutatedClass.stream() + .map(this::getLineCoverageForClassName) + .mapToInt(Map::size) + .sum(); } @Override @@ -147,7 +149,7 @@ private void addTestsToBlockMap(final TestInfo ti, InstructionLocation each) { @Override public BigInteger getCoverageIdForClass(final ClassName clazz) { - final Map> coverage = getTestsForClassName(clazz); + final Map> coverage = getLineCoverageForClassName(clazz); if (coverage.isEmpty()) { return BigInteger.ZERO; } @@ -224,7 +226,7 @@ private int numberOfLines() { } private int coveredLines() { - return FCollection.fold(numberCoveredLines(), 0, allClasses()); + return getNumberOfCoveredLines(allClasses()); } private BiFunction numberLines() { @@ -247,22 +249,7 @@ private TestInfo createTestInfo(final Description description, description.getQualifiedName(), executionTime, testee, linesCovered); } - private BiFunction numberCoveredLines() { - return (a, clazz) -> a + getNumberOfCoveredLines(clazz); - } - - private int getNumberOfCoveredLines(final ClassName clazz) { - final Map> map = getTestsForClassName(clazz); - if (map != null) { - return map.size(); - } else { - return 0; - } - - } - - private Map> getTestsForClassName( - final ClassName clazz) { + private Map> getLineCoverageForClassName(final ClassName clazz) { // Use any test that provided some coverage of the class // This fails to consider tests that only accessed a static variable // of the class in question as this does not register as coverage. diff --git a/pitest-entry/src/main/java/org/pitest/mutationtest/MutationStatusMap.java b/pitest-entry/src/main/java/org/pitest/mutationtest/MutationStatusMap.java index 90c1507f1..66a33ed24 100644 --- a/pitest-entry/src/main/java/org/pitest/mutationtest/MutationStatusMap.java +++ b/pitest-entry/src/main/java/org/pitest/mutationtest/MutationStatusMap.java @@ -46,8 +46,7 @@ public void setStatusForMutation(final MutationDetails mutation, public void setStatusForMutations( final Collection mutations, final DetectionStatus status) { - FCollection.forEach(mutations, - putToMap(this.mutationMap, MutationStatusTestPair.notAnalysed(0, status))); + mutations.forEach(putToMap(this.mutationMap, MutationStatusTestPair.notAnalysed(0, status))); } public List createMutationResults() { diff --git a/pitest-entry/src/main/java/org/pitest/mutationtest/build/CompoundMutationInterceptor.java b/pitest-entry/src/main/java/org/pitest/mutationtest/build/CompoundMutationInterceptor.java index 06938f6b6..ca18a3040 100644 --- a/pitest-entry/src/main/java/org/pitest/mutationtest/build/CompoundMutationInterceptor.java +++ b/pitest-entry/src/main/java/org/pitest/mutationtest/build/CompoundMutationInterceptor.java @@ -1,9 +1,10 @@ package org.pitest.mutationtest.build; +import static java.util.Comparator.comparing; + import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.Comparator; import java.util.List; import org.pitest.bytecode.analysis.ClassTree; @@ -16,18 +17,16 @@ public class CompoundMutationInterceptor implements MutationInterceptor { public CompoundMutationInterceptor(List interceptors) { this.children.addAll(interceptors); - Collections.sort(this.children, sortByType()); + this.children.sort(comparing(MutationInterceptor::type)); } public static MutationInterceptor nullInterceptor() { - return new CompoundMutationInterceptor(Collections.emptyList()); + return new CompoundMutationInterceptor(Collections.emptyList()); } @Override public void begin(ClassTree clazz) { - for (final MutationInterceptor each : this.children) { - each.begin(clazz); - } + this.children.forEach(each -> each.begin(clazz)); } @Override @@ -42,9 +41,7 @@ public Collection intercept( @Override public void end() { - for (final MutationInterceptor each : this.children) { - each.end(); - } + this.children.forEach(MutationInterceptor::end); } @Override @@ -52,8 +49,4 @@ public InterceptorType type() { return InterceptorType.OTHER; } - private static Comparator sortByType() { - return (o1, o2) -> o1.type().compareTo(o2.type()); - } - } diff --git a/pitest-entry/src/main/java/org/pitest/mutationtest/build/DefaultTestPrioritiser.java b/pitest-entry/src/main/java/org/pitest/mutationtest/build/DefaultTestPrioritiser.java index cbfc36f18..cd1105238 100644 --- a/pitest-entry/src/main/java/org/pitest/mutationtest/build/DefaultTestPrioritiser.java +++ b/pitest-entry/src/main/java/org/pitest/mutationtest/build/DefaultTestPrioritiser.java @@ -1,7 +1,7 @@ package org.pitest.mutationtest.build; +import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.logging.Logger; @@ -11,8 +11,6 @@ import org.pitest.coverage.CoverageDatabase; import org.pitest.coverage.InstructionLocation; import org.pitest.coverage.TestInfo; -import org.pitest.functional.FCollection; -import org.pitest.functional.prelude.Prelude; import org.pitest.mutationtest.engine.MutationDetails; import org.pitest.util.Log; @@ -69,10 +67,8 @@ private Collection pickTests(MutationDetails mutation) { private List prioritizeTests(ClassName clazz, Collection testsForMutant) { - final List sortedTis = FCollection.map(testsForMutant, - Prelude.id(TestInfo.class)); - Collections.sort(sortedTis, new TestInfoPriorisationComparator(clazz, - TIME_WEIGHTING_FOR_DIRECT_UNIT_TESTS)); + final List sortedTis = new ArrayList<>(testsForMutant); + sortedTis.sort(new TestInfoPriorisationComparator(clazz, TIME_WEIGHTING_FOR_DIRECT_UNIT_TESTS)); return sortedTis; } diff --git a/pitest-entry/src/main/java/org/pitest/mutationtest/build/MutationTestBuilder.java b/pitest-entry/src/main/java/org/pitest/mutationtest/build/MutationTestBuilder.java index e341a084c..81adee4ba 100644 --- a/pitest-entry/src/main/java/org/pitest/mutationtest/build/MutationTestBuilder.java +++ b/pitest-entry/src/main/java/org/pitest/mutationtest/build/MutationTestBuilder.java @@ -14,10 +14,10 @@ */ package org.pitest.mutationtest.build; +import static java.util.Comparator.comparing; + import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -58,7 +58,7 @@ public List createMutationTestUnits( final List mutations = FCollection.flatMap(codeClasses, classToMutations()); - Collections.sort(mutations, comparator()); + mutations.sort(comparing(MutationDetails::getId)); final Collection analysedMutations = this.analyser .analyse(mutations); @@ -82,14 +82,10 @@ public List createMutationTestUnits( } } - Collections.sort(tus, new AnalysisPriorityComparator()); + tus.sort(new AnalysisPriorityComparator()); return tus; } - private Comparator comparator() { - return (arg0, arg1) -> arg0.getId().compareTo(arg1.getId()); - } - private Function> classToMutations() { return a -> MutationTestBuilder.this.mutationSource.createMutations(a); } diff --git a/pitest-entry/src/main/java/org/pitest/mutationtest/build/intercept/javafeatures/InlinedFinallyBlockFilter.java b/pitest-entry/src/main/java/org/pitest/mutationtest/build/intercept/javafeatures/InlinedFinallyBlockFilter.java index 97dbe61f5..cfb2da92d 100644 --- a/pitest-entry/src/main/java/org/pitest/mutationtest/build/intercept/javafeatures/InlinedFinallyBlockFilter.java +++ b/pitest-entry/src/main/java/org/pitest/mutationtest/build/intercept/javafeatures/InlinedFinallyBlockFilter.java @@ -8,7 +8,6 @@ import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.List; @@ -67,7 +66,7 @@ public Collection intercept( } /* FIXME tests rely on order of returned mutants */ - Collections.sort(combined, compareLineNumbers()); + combined.sort(compareLineNumbers()); return combined; } @@ -77,7 +76,7 @@ public void end() { } private static Comparator compareLineNumbers() { - return (arg0, arg1) -> arg0.getLineNumber() - arg1.getLineNumber(); + return Comparator.comparingInt(MutationDetails::getLineNumber); } private void checkForInlinedCode(final Collection combined, @@ -97,7 +96,7 @@ private void checkForInlinedCode(final Collection combined, // to the base one (is this not implied by there being only 1 mutation in // the handler ????) final List ids = map(each.getValue(), mutationToBlock()); - if (ids.stream().filter(not(isEqual(firstBlock))).findFirst().isPresent()) { + if (ids.stream().anyMatch(not(isEqual(firstBlock)))) { combined.add(makeCombinedMutant(each.getValue())); } else { combined.addAll(each.getValue()); diff --git a/pitest-entry/src/main/java/org/pitest/mutationtest/config/SettingsFactory.java b/pitest-entry/src/main/java/org/pitest/mutationtest/config/SettingsFactory.java index c463ad7b4..3e86bb554 100644 --- a/pitest-entry/src/main/java/org/pitest/mutationtest/config/SettingsFactory.java +++ b/pitest-entry/src/main/java/org/pitest/mutationtest/config/SettingsFactory.java @@ -167,6 +167,6 @@ private static Function toFeature() { private Comparator byName() { - return (a,b) -> a.name().compareTo(b.name()); + return Comparator.comparing(Feature::name); } } diff --git a/pitest-entry/src/main/java/org/pitest/mutationtest/execute/MutationAnalysisExecutor.java b/pitest-entry/src/main/java/org/pitest/mutationtest/execute/MutationAnalysisExecutor.java index 6a3ef7526..da7718fb2 100644 --- a/pitest-entry/src/main/java/org/pitest/mutationtest/execute/MutationAnalysisExecutor.java +++ b/pitest-entry/src/main/java/org/pitest/mutationtest/execute/MutationAnalysisExecutor.java @@ -10,7 +10,6 @@ import java.util.concurrent.TimeUnit; import java.util.logging.Logger; -import org.pitest.functional.FCollection; import org.pitest.mutationtest.ClassMutationResults; import org.pitest.mutationtest.MutationMetaData; import org.pitest.mutationtest.MutationResultListener; @@ -74,13 +73,11 @@ private void processResult(List> results) } private void signalRunStartToAllListeners() { - FCollection.forEach(this.listeners, - a -> a.runStart()); + this.listeners.forEach(MutationResultListener::runStart); } private void signalRunEndToAllListeners() { - FCollection.forEach(this.listeners, - a -> a.runEnd()); + this.listeners.forEach(MutationResultListener::runEnd); } } diff --git a/pitest-entry/src/main/java/org/pitest/mutationtest/statistics/MutationStatisticsPrecursor.java b/pitest-entry/src/main/java/org/pitest/mutationtest/statistics/MutationStatisticsPrecursor.java index e5ebac737..1731243fb 100644 --- a/pitest-entry/src/main/java/org/pitest/mutationtest/statistics/MutationStatisticsPrecursor.java +++ b/pitest-entry/src/main/java/org/pitest/mutationtest/statistics/MutationStatisticsPrecursor.java @@ -15,7 +15,7 @@ class MutationStatisticsPrecursor { private long numberOfTestsRun = 0; public void registerResults(final Collection results) { - FCollection.forEach(results, register()); + results.forEach(register()); } private Consumer register() { diff --git a/pitest-entry/src/main/java/org/pitest/mutationtest/tooling/DirectorySourceLocator.java b/pitest-entry/src/main/java/org/pitest/mutationtest/tooling/DirectorySourceLocator.java index 7420c19b2..14846c522 100644 --- a/pitest-entry/src/main/java/org/pitest/mutationtest/tooling/DirectorySourceLocator.java +++ b/pitest-entry/src/main/java/org/pitest/mutationtest/tooling/DirectorySourceLocator.java @@ -37,7 +37,7 @@ private static class FileToReader implements Function> { public Optional apply(final File f) { if (f.exists()) { try { - return Optional. ofNullable(new FileReader(f)); + return Optional.of(new FileReader(f)); } catch (final FileNotFoundException e) { return Optional.empty(); } @@ -45,7 +45,7 @@ public Optional apply(final File f) { return Optional.empty(); } - }; + } DirectorySourceLocator(final File root, final Function> fileToReader) { diff --git a/pitest-entry/src/main/java/org/pitest/mutationtest/verify/DefaultBuildVerifier.java b/pitest-entry/src/main/java/org/pitest/mutationtest/verify/DefaultBuildVerifier.java index 9e4b55c0c..cc40b6be1 100644 --- a/pitest-entry/src/main/java/org/pitest/mutationtest/verify/DefaultBuildVerifier.java +++ b/pitest-entry/src/main/java/org/pitest/mutationtest/verify/DefaultBuildVerifier.java @@ -33,7 +33,7 @@ public void verify(final CodeSource code) { if (hasMutableCode(codeClasses)) { checkAtLeastOneClassHasLineNumbers(codeClasses); - FCollection.forEach(codeClasses, throwErrorIfHasNoSourceFile()); + codeClasses.forEach(throwErrorIfHasNoSourceFile()); } } diff --git a/pitest-entry/src/test/java/org/pitest/mutationtest/build/AnalyisPriorityComparatorTest.java b/pitest-entry/src/test/java/org/pitest/mutationtest/build/AnalyisPriorityComparatorTest.java index 069dd5121..fe54cea06 100644 --- a/pitest-entry/src/test/java/org/pitest/mutationtest/build/AnalyisPriorityComparatorTest.java +++ b/pitest-entry/src/test/java/org/pitest/mutationtest/build/AnalyisPriorityComparatorTest.java @@ -3,7 +3,6 @@ import static org.junit.Assert.assertEquals; import java.util.Arrays; -import java.util.Collections; import java.util.List; import org.junit.Test; @@ -19,7 +18,7 @@ public void shouldPrioritiseLargestFirst() { final MutationAnalysisUnit b = unit(2); final MutationAnalysisUnit c = unit(3); final List actual = Arrays.asList(a, b, c); - Collections.sort(actual, this.testee); + actual.sort(this.testee); assertEquals(Arrays.asList(c, b, a), actual); } @@ -29,7 +28,7 @@ public void shouldPreserveCorrectOrder() { final MutationAnalysisUnit b = unit(2); final MutationAnalysisUnit c = unit(1); final List actual = Arrays.asList(a, b, c); - Collections.sort(actual, this.testee); + actual.sort(this.testee); assertEquals(Arrays.asList(a, b, c), actual); } diff --git a/pitest-entry/src/test/java/org/pitest/mutationtest/build/TestInfoPriorisationComparatorTest.java b/pitest-entry/src/test/java/org/pitest/mutationtest/build/TestInfoPriorisationComparatorTest.java index 7ec793933..437e35040 100644 --- a/pitest-entry/src/test/java/org/pitest/mutationtest/build/TestInfoPriorisationComparatorTest.java +++ b/pitest-entry/src/test/java/org/pitest/mutationtest/build/TestInfoPriorisationComparatorTest.java @@ -18,7 +18,6 @@ import static org.junit.Assert.assertThat; import java.util.Arrays; -import java.util.Collections; import java.util.List; import org.junit.Before; @@ -82,7 +81,7 @@ public void shouldFavourTestsThatCoverFewerLinesInTheSameAmountOfTime() { private List sortWithTestee(final TestInfo... testInfos) { final List list = Arrays.asList(testInfos); - Collections.sort(list, this.testee); + list.sort(this.testee); return list; } diff --git a/pitest-html-report/src/main/java/org/pitest/mutationtest/report/html/Line.java b/pitest-html-report/src/main/java/org/pitest/mutationtest/report/html/Line.java index d40474201..00e7ee53b 100644 --- a/pitest-html-report/src/main/java/org/pitest/mutationtest/report/html/Line.java +++ b/pitest-html-report/src/main/java/org/pitest/mutationtest/report/html/Line.java @@ -14,7 +14,6 @@ */ package org.pitest.mutationtest.report.html; -import java.util.Collections; import java.util.List; import java.util.Optional; @@ -33,7 +32,7 @@ public Line(final long number, final String text, this.text = text; this.lineCovered = lineCovered; this.mutations = mutations; - Collections.sort(mutations, new ResultComparator()); + mutations.sort(new ResultComparator()); } public long getNumber() { diff --git a/pitest-html-report/src/main/java/org/pitest/mutationtest/report/html/MutationResultList.java b/pitest-html-report/src/main/java/org/pitest/mutationtest/report/html/MutationResultList.java index 8755efc6b..31e7e708d 100644 --- a/pitest-html-report/src/main/java/org/pitest/mutationtest/report/html/MutationResultList.java +++ b/pitest-html-report/src/main/java/org/pitest/mutationtest/report/html/MutationResultList.java @@ -14,10 +14,10 @@ */ package org.pitest.mutationtest.report.html; +import static java.util.Comparator.comparingInt; + import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; import java.util.Iterator; import java.util.List; @@ -54,9 +54,7 @@ public List groupMutationsByLine() { } private void sortMutationsIntoLineOrder() { - final Comparator c = (o1, o2) -> o1.getDetails().getLineNumber() - - o2.getDetails().getLineNumber(); - Collections.sort(this.impl, c); + this.impl.sort(comparingInt(o -> o.getDetails().getLineNumber())); } @Override diff --git a/pitest-html-report/src/test/java/org/pitest/mutationtest/report/html/MutationTestSummaryDataFileNameComparatorTest.java b/pitest-html-report/src/test/java/org/pitest/mutationtest/report/html/MutationTestSummaryDataFileNameComparatorTest.java index 116b3e743..3188bc893 100644 --- a/pitest-html-report/src/test/java/org/pitest/mutationtest/report/html/MutationTestSummaryDataFileNameComparatorTest.java +++ b/pitest-html-report/src/test/java/org/pitest/mutationtest/report/html/MutationTestSummaryDataFileNameComparatorTest.java @@ -19,7 +19,7 @@ public void shouldSortDataByFileName() { final MutationTestSummaryData aa = makeSummaryData("aa"); final MutationTestSummaryData z = makeSummaryData("z"); final List list = Arrays.asList(z, aa, ab); - Collections.sort(list, testee); + list.sort(testee); final List expected = Arrays.asList(aa, ab, z); assertEquals(expected, list); } diff --git a/pitest-html-report/src/test/java/org/pitest/mutationtest/report/html/ResultComparatorTest.java b/pitest-html-report/src/test/java/org/pitest/mutationtest/report/html/ResultComparatorTest.java index a0c8c1438..cffed0eae 100644 --- a/pitest-html-report/src/test/java/org/pitest/mutationtest/report/html/ResultComparatorTest.java +++ b/pitest-html-report/src/test/java/org/pitest/mutationtest/report/html/ResultComparatorTest.java @@ -3,7 +3,6 @@ import static org.junit.Assert.assertEquals; import java.util.Arrays; -import java.util.Collections; import java.util.List; import org.junit.Test; @@ -20,7 +19,7 @@ public void shouldSortInDesiredOrder() { final List mrs = Arrays.asList( make(DetectionStatus.TIMED_OUT), make(DetectionStatus.SURVIVED), make(DetectionStatus.NO_COVERAGE), make(DetectionStatus.KILLED)); - Collections.sort(mrs, this.testee); + mrs.sort(this.testee); assertEquals(DetectionStatus.SURVIVED,mrs.get(0).getStatus()); assertEquals(DetectionStatus.NO_COVERAGE,mrs.get(1).getStatus()); assertEquals(DetectionStatus.TIMED_OUT,mrs.get(2).getStatus()); diff --git a/pitest/src/main/java/org/pitest/coverage/analysis/ControlFlowAnalyser.java b/pitest/src/main/java/org/pitest/coverage/analysis/ControlFlowAnalyser.java index 0864efb5d..0081657d7 100644 --- a/pitest/src/main/java/org/pitest/coverage/analysis/ControlFlowAnalyser.java +++ b/pitest/src/main/java/org/pitest/coverage/analysis/ControlFlowAnalyser.java @@ -11,7 +11,6 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.List; -import java.util.ListIterator; import java.util.Set; import org.objectweb.asm.tree.AbstractInsnNode; @@ -149,9 +148,7 @@ private static boolean isReturn(final AbstractInsnNode ins) { private static Set findJumpTargets(final InsnList instructions) { final Set jumpTargets = new HashSet<>(); - final ListIterator it = instructions.iterator(); - while (it.hasNext()) { - final AbstractInsnNode o = it.next(); + for (AbstractInsnNode o : instructions) { if (o instanceof JumpInsnNode) { jumpTargets.add(((JumpInsnNode) o).label); } else if (o instanceof TableSwitchInsnNode) { diff --git a/pitest/src/main/java/org/pitest/coverage/execute/CoverageWorker.java b/pitest/src/main/java/org/pitest/coverage/execute/CoverageWorker.java index 50f5b06a7..255634131 100644 --- a/pitest/src/main/java/org/pitest/coverage/execute/CoverageWorker.java +++ b/pitest/src/main/java/org/pitest/coverage/execute/CoverageWorker.java @@ -14,10 +14,9 @@ */ package org.pitest.coverage.execute; +import static java.util.stream.Collectors.toList; import static org.pitest.util.Unchecked.translateCheckedException; -import java.util.ArrayList; -import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -38,18 +37,16 @@ public CoverageWorker(final CoveragePipe pipe, final List tests) { } public void run() { - try { final List decoratedTests = decorateForCoverage(this.tests, this.pipe); - Collections.sort(decoratedTests, testComparator()); + decoratedTests.sort(testComparator()); final Container c = new UnContainer(); final Pitest pit = new Pitest(new ErrorListener()); pit.run(c, decoratedTests); - } catch (final Exception ex) { throw translateCheckedException(ex); } @@ -57,16 +54,13 @@ public void run() { } private static Comparator testComparator() { - return (o1, o2) -> o1.getDescription().getQualifiedName() - .compareTo(o2.getDescription().getQualifiedName()); + return Comparator.comparing(o -> o.getDescription().getQualifiedName()); } private static List decorateForCoverage(final List plainTests, final CoverageReceiver queue) { - final List decorated = new ArrayList<>(plainTests.size()); - for (final TestUnit each : plainTests) { - decorated.add(new CoverageDecorator(queue, each)); - } - return decorated; + return plainTests.stream() + .map(each -> new CoverageDecorator(queue, each)) + .collect(toList()); } } diff --git a/pitest/src/main/java/org/pitest/dependency/DependencyExtractor.java b/pitest/src/main/java/org/pitest/dependency/DependencyExtractor.java index 6c285623f..2b6d154d8 100644 --- a/pitest/src/main/java/org/pitest/dependency/DependencyExtractor.java +++ b/pitest/src/main/java/org/pitest/dependency/DependencyExtractor.java @@ -134,7 +134,7 @@ private Set extractRelevantDependencies(final String clazz, } private static Comparator equalDestinationComparator() { - return (o1, o2) -> o1.getDest().compareTo(o2.getDest()); + return Comparator.comparing(DependencyAccess::getDest); } private List extract(final String clazz, @@ -157,10 +157,6 @@ private List extract(final String clazz, private Map> groupDependenciesByClass( final Set relevantDependencies) { - final List sortedByClass = new ArrayList<>( - relevantDependencies.size()); - Collections.sort(sortedByClass, classNameComparator()); - return FCollection.fold(addDependenciesToMap(), new HashMap>(), relevantDependencies); @@ -180,10 +176,6 @@ private static BiFunction>, DependencyAccess, }; } - private static Comparator classNameComparator() { - return (lhs, rhs) -> lhs.getDest().getOwner().compareTo(rhs.getDest().getOwner()); - } - private static Predicate nameIsEqual(final String clazz) { return a -> a.getDest().getOwner().equals(clazz); } diff --git a/pitest/src/main/java/org/pitest/functional/FCollection.java b/pitest/src/main/java/org/pitest/functional/FCollection.java index fe177dd25..c2f672358 100644 --- a/pitest/src/main/java/org/pitest/functional/FCollection.java +++ b/pitest/src/main/java/org/pitest/functional/FCollection.java @@ -20,7 +20,6 @@ import java.util.List; import java.util.Map; import java.util.function.BiFunction; -import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; @@ -29,11 +28,6 @@ */ public abstract class FCollection { - public static void forEach(final Iterable as, - final Consumer e) { - as.forEach(e); - } - public static void mapTo(final Iterable as, final Function f, final Collection bs) { if (as != null) { diff --git a/pitest/src/main/java/org/pitest/mutationtest/engine/gregor/config/Mutator.java b/pitest/src/main/java/org/pitest/mutationtest/engine/gregor/config/Mutator.java index e77baf68e..27ca6b58d 100644 --- a/pitest/src/main/java/org/pitest/mutationtest/engine/gregor/config/Mutator.java +++ b/pitest/src/main/java/org/pitest/mutationtest/engine/gregor/config/Mutator.java @@ -398,15 +398,14 @@ private static void addGroup(final String key, public static Collection fromStrings( final Collection names) { - final Set unique = new TreeSet<>( - compareId()); + final Set unique = new TreeSet<>(compareId()); FCollection.flatMapTo(names, fromString(), unique); return unique; } private static Comparator compareId() { - return (o1, o2) -> o1.getGloballyUniqueId().compareTo(o2.getGloballyUniqueId()); + return Comparator.comparing(MethodMutatorFactory::getGloballyUniqueId); } private static Function> fromString() { diff --git a/pitest/src/test/java/org/pitest/functional/FCollectionTest.java b/pitest/src/test/java/org/pitest/functional/FCollectionTest.java index 60358b041..0386ab82b 100644 --- a/pitest/src/test/java/org/pitest/functional/FCollectionTest.java +++ b/pitest/src/test/java/org/pitest/functional/FCollectionTest.java @@ -60,7 +60,7 @@ public void shouldApplyForEachToAllItems() { final List actual = new ArrayList<>(); final Consumer e = a -> actual.add(a); - FCollection.forEach(this.is, e); + this.is.forEach(e); assertEquals(this.is, actual); } diff --git a/pitest/src/test/java/org/pitest/functional/prelude/PreludeTest.java b/pitest/src/test/java/org/pitest/functional/prelude/PreludeTest.java index dc072589e..0cfd498ce 100644 --- a/pitest/src/test/java/org/pitest/functional/prelude/PreludeTest.java +++ b/pitest/src/test/java/org/pitest/functional/prelude/PreludeTest.java @@ -14,8 +14,6 @@ */ package org.pitest.functional.prelude; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.verify; import java.io.PrintStream; diff --git a/pitest/src/test/java/org/pitest/util/StringUtilTest.java b/pitest/src/test/java/org/pitest/util/StringUtilTest.java index 7daf63eda..fc265ae60 100644 --- a/pitest/src/test/java/org/pitest/util/StringUtilTest.java +++ b/pitest/src/test/java/org/pitest/util/StringUtilTest.java @@ -16,9 +16,6 @@ import static org.junit.Assert.assertEquals; -import java.util.Arrays; -import java.util.Collections; - import org.junit.Test; public class StringUtilTest {