Improve readability

This commit is contained in:
René Pfeuffer
2020-11-26 08:53:09 +01:00
parent dcc848eb4b
commit 58cbe1a535
2 changed files with 34 additions and 14 deletions

View File

@@ -50,7 +50,7 @@ public class BranchXDaysOlderThanDefaultStaleComputer implements BranchStaleComp
@SuppressWarnings("java:S3655") // we check "isPresent" for both dates, but due to the third check sonar does not get it
public boolean computeStale(Branch branch, StaleContext context) {
Branch defaultBranch = context.getDefaultBranch();
if (!branch.isDefaultBranch() && branch.getLastCommitDate().isPresent() && defaultBranch.getLastCommitDate().isPresent()) {
if (shouldCompute(branch, defaultBranch)) {
Instant defaultCommitDate = ofEpochMilli(defaultBranch.getLastCommitDate().get());
Instant thisCommitDate = ofEpochMilli(branch.getLastCommitDate().get());
return thisCommitDate.plus(amountOfDays, ChronoUnit.DAYS).isBefore(defaultCommitDate);
@@ -58,4 +58,8 @@ public class BranchXDaysOlderThanDefaultStaleComputer implements BranchStaleComp
return false;
}
}
public boolean shouldCompute(Branch branch, Branch defaultBranch) {
return !branch.isDefaultBranch() && branch.getLastCommitDate().isPresent() && defaultBranch.getLastCommitDate().isPresent();
}
}

View File

@@ -28,29 +28,45 @@ import sonia.scm.repository.Branch;
import java.io.IOException;
import java.util.List;
import java.util.function.Function;
import java.util.Optional;
/**
*
* @author Sebastian Sdorra
* @since 1.18
*/
public interface BranchesCommand
{
public interface BranchesCommand {
List<Branch> getBranches() throws IOException;
default List<Branch> getBranchesWithStaleFlags(BranchStaleComputer computer) throws IOException {
Function<Branch, BranchStaleComputer.StaleContext> createContext = branch -> {
BranchStaleComputer.StaleContext staleContext = new BranchStaleComputer.StaleContext();
staleContext.setDefaultBranch(branch);
return staleContext;
};
List<Branch> branches = getBranches();
branches.stream()
.filter(Branch::isDefaultBranch)
.findFirst()
.ifPresent(defaultBranch -> branches.forEach(branch -> branch.setStale(computer.computeStale(branch, createContext.apply(defaultBranch)))));
new StaleProcessor(computer, branches).process();
return branches;
}
final class StaleProcessor {
private final BranchStaleComputer computer;
private final List<Branch> branches;
private StaleProcessor(BranchStaleComputer computer, List<Branch> branches) {
this.computer = computer;
this.branches = branches;
}
private void process() {
Optional<Branch> defaultBranch = branches.stream()
.filter(Branch::isDefaultBranch)
.findFirst();
defaultBranch.ifPresent(this::process);
}
private void process(Branch defaultBranch) {
BranchStaleComputer.StaleContext staleContext = new BranchStaleComputer.StaleContext();
staleContext.setDefaultBranch(defaultBranch);
branches.forEach(branch -> branch.setStale(computer.computeStale(branch, staleContext)));
}
}
}