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 4 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 @@ -3,7 +3,6 @@
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 @@ -74,9 +73,8 @@ 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());
for (AbstractInsnNode abstractInsnNode : this.rawNode.instructions) {
list.add(abstractInsnNode);
}
this.lazyInstructions = new ArrayList<>(list);
AlexElin marked this conversation as resolved.
Show resolved Hide resolved
return this.lazyInstructions;
Expand Down
13 changes: 3 additions & 10 deletions pitest-entry/src/main/java/org/pitest/coverage/CoverageData.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ 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
Expand Down Expand Up @@ -252,13 +251,7 @@ private BiFunction<Integer, ClassName, Integer> numberCoveredLines() {
}

private int getNumberOfCoveredLines(final ClassName clazz) {
AlexElin marked this conversation as resolved.
Show resolved Hide resolved
final Map<ClassLine, Set<TestInfo>> map = getTestsForClassName(clazz);
if (map != null) {
return map.size();
} else {
return 0;
}

return getTestsForClassName(clazz).size();
}

private Map<ClassLine, Set<TestInfo>> getTestsForClassName(
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -54,9 +54,7 @@ public List<MutationGrouping> groupMutationsByLine() {
}

private void sortMutationsIntoLineOrder() {
final Comparator<MutationResult> c = (o1, o2) -> o1.getDetails().getLineNumber()
- o2.getDetails().getLineNumber();
Collections.sort(this.impl, c);
this.impl.sort(comparingInt(o -> o.getDetails().getLineNumber()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void shouldSortDataByFileName() {
final MutationTestSummaryData aa = makeSummaryData("aa");
final MutationTestSummaryData z = makeSummaryData("z");
final List<MutationTestSummaryData> list = Arrays.asList(z, aa, ab);
Collections.sort(list, testee);
list.sort(testee);
final List<MutationTestSummaryData> expected = Arrays.asList(aa, ab, z);
assertEquals(expected, list);
}
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 @@ -20,7 +19,7 @@ public void shouldSortInDesiredOrder() {
final List<MutationResult> 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());
Expand Down
Loading