Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

use java 8 features: List.sort, Comparator.comparing and others #757

Merged
merged 7 commits into from
Apr 26, 2020
Merged
Show file tree
Hide file tree
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
@@ -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;
Expand Down Expand Up @@ -73,12 +71,11 @@ public List<AnnotationNode> annotations() {
}

private List<AbstractInsnNode> createInstructionList() {
final List<AbstractInsnNode> list = new LinkedList<>();
final ListIterator<AbstractInsnNode> it = this.rawNode.instructions.iterator();
while (it.hasNext()) {
list.add(it.next());
final List<AbstractInsnNode> list = new ArrayList<>();
for (AbstractInsnNode abstractInsnNode : this.rawNode.instructions) {
list.add(abstractInsnNode);
}
this.lazyInstructions = new ArrayList<>(list);
this.lazyInstructions = list;
return this.lazyInstructions;
}

Expand Down
33 changes: 10 additions & 23 deletions pitest-entry/src/main/java/org/pitest/coverage/CoverageData.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,13 @@ public CoverageData(final CodeSource code, final LineMap lm, Map<InstructionLoca
}

@Override
public Collection<TestInfo> getTestsForInstructionLocation(
InstructionLocation location) {
return this.instructionCoverage.get(location);
public Collection<TestInfo> getTestsForInstructionLocation(InstructionLocation location) {
return this.instructionCoverage.getOrDefault(location, Collections.emptySet());
}

@Override
public Collection<TestInfo> getTestsForClassLine(final ClassLine classLine) {
final Collection<TestInfo> result = getTestsForClassName(
final Collection<TestInfo> result = getLineCoverageForClassName(
classLine.getClassName()).get(classLine);
if (result == null) {
return Collections.emptyList();
Expand Down Expand Up @@ -109,7 +108,10 @@ public Collection<ClassInfo> getClassInfo(final Collection<ClassName> classes) {

@Override
public int getNumberOfCoveredLines(final Collection<ClassName> mutatedClass) {
return FCollection.fold(numberCoveredLines(), 0, mutatedClass);
return mutatedClass.stream()
.map(this::getLineCoverageForClassName)
.mapToInt(Map::size)
.sum();
}

@Override
Expand Down Expand Up @@ -147,7 +149,7 @@ private void addTestsToBlockMap(final TestInfo ti, InstructionLocation each) {

@Override
public BigInteger getCoverageIdForClass(final ClassName clazz) {
final Map<ClassLine, Set<TestInfo>> coverage = getTestsForClassName(clazz);
final Map<ClassLine, Set<TestInfo>> coverage = getLineCoverageForClassName(clazz);
if (coverage.isEmpty()) {
return BigInteger.ZERO;
}
Expand Down Expand Up @@ -224,7 +226,7 @@ private int numberOfLines() {
}

private int coveredLines() {
return FCollection.fold(numberCoveredLines(), 0, allClasses());
return getNumberOfCoveredLines(allClasses());
}

private BiFunction<Integer, ClassInfo, Integer> numberLines() {
Expand All @@ -247,22 +249,7 @@ private TestInfo createTestInfo(final Description description,
description.getQualifiedName(), executionTime, testee, linesCovered);
}

private BiFunction<Integer, ClassName, Integer> numberCoveredLines() {
return (a, clazz) -> a + getNumberOfCoveredLines(clazz);
}

private int getNumberOfCoveredLines(final ClassName clazz) {
final Map<ClassLine, Set<TestInfo>> map = getTestsForClassName(clazz);
if (map != null) {
return map.size();
} else {
return 0;
}

}

private Map<ClassLine, Set<TestInfo>> getTestsForClassName(
final ClassName clazz) {
private Map<ClassLine, Set<TestInfo>> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public void setStatusForMutation(final MutationDetails mutation,

public void setStatusForMutations(
final Collection<MutationDetails> 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<MutationResult> createMutationResults() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,18 +17,16 @@ public class CompoundMutationInterceptor implements MutationInterceptor {

public CompoundMutationInterceptor(List<? extends MutationInterceptor> interceptors) {
this.children.addAll(interceptors);
Collections.sort(this.children, sortByType());
this.children.sort(comparing(MutationInterceptor::type));
}

public static MutationInterceptor nullInterceptor() {
return new CompoundMutationInterceptor(Collections.<MutationInterceptor>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
Expand All @@ -42,18 +41,12 @@ public Collection<MutationDetails> intercept(

@Override
public void end() {
for (final MutationInterceptor each : this.children) {
each.end();
}
this.children.forEach(MutationInterceptor::end);
}

@Override
public InterceptorType type() {
return InterceptorType.OTHER;
}

private static Comparator<? super MutationInterceptor> sortByType() {
return (o1, o2) -> o1.type().compareTo(o2.type());
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -69,10 +67,8 @@ private Collection<TestInfo> pickTests(MutationDetails mutation) {

private List<TestInfo> prioritizeTests(ClassName clazz,
Collection<TestInfo> testsForMutant) {
final List<TestInfo> sortedTis = FCollection.map(testsForMutant,
Prelude.id(TestInfo.class));
Collections.sort(sortedTis, new TestInfoPriorisationComparator(clazz,
TIME_WEIGHTING_FOR_DIRECT_UNIT_TESTS));
final List<TestInfo> sortedTis = new ArrayList<>(testsForMutant);
sortedTis.sort(new TestInfoPriorisationComparator(clazz, TIME_WEIGHTING_FOR_DIRECT_UNIT_TESTS));
return sortedTis;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -58,7 +58,7 @@ public List<MutationAnalysisUnit> createMutationTestUnits(
final List<MutationDetails> mutations = FCollection.flatMap(codeClasses,
classToMutations());

Collections.sort(mutations, comparator());
mutations.sort(comparing(MutationDetails::getId));

final Collection<MutationResult> analysedMutations = this.analyser
.analyse(mutations);
Expand All @@ -82,14 +82,10 @@ public List<MutationAnalysisUnit> createMutationTestUnits(
}
}

Collections.sort(tus, new AnalysisPriorityComparator());
tus.sort(new AnalysisPriorityComparator());
return tus;
}

private Comparator<MutationDetails> comparator() {
return (arg0, arg1) -> arg0.getId().compareTo(arg1.getId());
}

private Function<ClassName, Iterable<MutationDetails>> classToMutations() {
return a -> MutationTestBuilder.this.mutationSource.createMutations(a);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,7 +66,7 @@ public Collection<MutationDetails> intercept(
}

/* FIXME tests rely on order of returned mutants */
Collections.sort(combined, compareLineNumbers());
combined.sort(compareLineNumbers());
return combined;
}

Expand All @@ -77,7 +76,7 @@ public void end() {
}

private static Comparator<MutationDetails> compareLineNumbers() {
return (arg0, arg1) -> arg0.getLineNumber() - arg1.getLineNumber();
return Comparator.comparingInt(MutationDetails::getLineNumber);
}

private void checkForInlinedCode(final Collection<MutationDetails> combined,
Expand All @@ -97,7 +96,7 @@ private void checkForInlinedCode(final Collection<MutationDetails> combined,
// to the base one (is this not implied by there being only 1 mutation in
// the handler ????)
final List<Integer> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,6 @@ private static Function<ProvidesFeature, Feature> toFeature() {


private Comparator<Feature> byName() {
return (a,b) -> a.name().compareTo(b.name());
return Comparator.comparing(Feature::name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -74,13 +73,11 @@ private void processResult(List<Future<MutationMetaData>> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class MutationStatisticsPrecursor {
private long numberOfTestsRun = 0;

public void registerResults(final Collection<MutationResult> results) {
FCollection.forEach(results, register());
results.forEach(register());
}

private Consumer<MutationResult> register() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ private static class FileToReader implements Function<File, Optional<Reader>> {
public Optional<Reader> apply(final File f) {
if (f.exists()) {
try {
return Optional.<Reader> ofNullable(new FileReader(f));
return Optional.of(new FileReader(f));
} catch (final FileNotFoundException e) {
return Optional.empty();
}
}
return Optional.empty();
}

};
}

DirectorySourceLocator(final File root,
final Function<File, Optional<Reader>> fileToReader) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void verify(final CodeSource code) {

if (hasMutableCode(codeClasses)) {
checkAtLeastOneClassHasLineNumbers(codeClasses);
FCollection.forEach(codeClasses, throwErrorIfHasNoSourceFile());
codeClasses.forEach(throwErrorIfHasNoSourceFile());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,7 +18,7 @@ public void shouldPrioritiseLargestFirst() {
final MutationAnalysisUnit b = unit(2);
final MutationAnalysisUnit c = unit(3);
final List<MutationAnalysisUnit> actual = Arrays.asList(a, b, c);
Collections.sort(actual, this.testee);
actual.sort(this.testee);
assertEquals(Arrays.asList(c, b, a), actual);
}

Expand All @@ -29,7 +28,7 @@ public void shouldPreserveCorrectOrder() {
final MutationAnalysisUnit b = unit(2);
final MutationAnalysisUnit c = unit(1);
final List<MutationAnalysisUnit> actual = Arrays.asList(a, b, c);
Collections.sort(actual, this.testee);
actual.sort(this.testee);
assertEquals(Arrays.asList(a, b, c), actual);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -82,7 +81,7 @@ public void shouldFavourTestsThatCoverFewerLinesInTheSameAmountOfTime() {

private List<TestInfo> sortWithTestee(final TestInfo... testInfos) {
final List<TestInfo> list = Arrays.asList(testInfos);
Collections.sort(list, this.testee);
list.sort(this.testee);
return list;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
package org.pitest.mutationtest.report.html;

import java.util.Collections;
import java.util.List;

import java.util.Optional;
Expand All @@ -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() {
Expand Down
Loading