From 7a0dc98d0243bfa911ae23781874bb6ba83eb695 Mon Sep 17 00:00:00 2001 From: Armin Samii Date: Mon, 23 Sep 2024 12:03:36 -0400 Subject: [PATCH] PR Review Comments: clean up STATUSES_TO_PRINT --- .../brightspots/rcv/ResultsWriter.java | 43 +++++++++---------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/src/main/java/network/brightspots/rcv/ResultsWriter.java b/src/main/java/network/brightspots/rcv/ResultsWriter.java index 58b09526..d410e958 100644 --- a/src/main/java/network/brightspots/rcv/ResultsWriter.java +++ b/src/main/java/network/brightspots/rcv/ResultsWriter.java @@ -46,6 +46,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import java.util.stream.Collectors; import javafx.util.Pair; import network.brightspots.rcv.ContestConfig.TabulateBySlice; import network.brightspots.rcv.RawContestConfig.CvrSource; @@ -78,6 +79,14 @@ class ResultsWriter { private String timestampString; // map from round number to residual surplus generated in that round private Map roundToResidualSurplus; + // statuses to print in all summary files + // (additional fields are added if needed in specific summary filetypes) + private static final List STATUSES_TO_PRINT = List.of( + StatusForRound.INVALIDATED_BY_OVERVOTE, + StatusForRound.INVALIDATED_BY_SKIPPED_RANKING, + StatusForRound.EXHAUSTED_CHOICE, + StatusForRound.INVALIDATED_BY_REPEATED_RANKING); + // visible for testing @SuppressWarnings("WeakerAccess") @@ -382,16 +391,9 @@ private void generateSummarySpreadsheet( csvPrinter.println(); } - List statusesToPrint = new ArrayList<>(); - statusesToPrint.add(StatusForRound.INVALIDATED_BY_OVERVOTE); - statusesToPrint.add(StatusForRound.INVALIDATED_BY_SKIPPED_RANKING); - statusesToPrint.add(StatusForRound.EXHAUSTED_CHOICE); - statusesToPrint.add(StatusForRound.INVALIDATED_BY_REPEATED_RANKING); - - for (StatusForRound statusToPrint : statusesToPrint) { - csvPrinter.print(statusToPrint.getTitleCaseKey()); + for (StatusForRound status : STATUSES_TO_PRINT) { + csvPrinter.print(status.getTitleCaseKey()); - StatusForRound status = statusToPrint; for (int round = 1; round <= numRounds; round++) { BigDecimal thisRoundInactive = roundTallies.get(round).getBallotStatusTally(status); csvPrinter.print(thisRoundInactive); @@ -1023,21 +1025,16 @@ private Map updateCandidateNamesInTally(RoundTally roundSumm } private Map getInactiveJsonMap(RoundTally roundTally) { - Map inactiveMap = new HashMap<>(); - List statusesToPrint = new ArrayList<>(); - statusesToPrint.add(StatusForRound.INVALIDATED_BY_OVERVOTE); - statusesToPrint.add(StatusForRound.INVALIDATED_BY_SKIPPED_RANKING); - statusesToPrint.add(StatusForRound.EXHAUSTED_CHOICE); - statusesToPrint.add(StatusForRound.INVALIDATED_BY_REPEATED_RANKING); - if (config.usesSurpluses() - && roundTally.getRoundNumber() == numRounds) { - statusesToPrint.add(StatusForRound.FINAL_ROUND_SURPLUS); - } - for (StatusForRound statusToPrint : statusesToPrint) { - inactiveMap.put( - statusToPrint.getCamelCaseKey(), roundTally.getBallotStatusTally(statusToPrint)); + Map result = STATUSES_TO_PRINT.stream() + .collect(Collectors.toMap(StatusForRound::getCamelCaseKey, + roundTally::getBallotStatusTally)); + + if (config.usesSurpluses() && roundTally.getRoundNumber() == numRounds) { + result.put(StatusForRound.FINAL_ROUND_SURPLUS.getCamelCaseKey(), + roundTally.getBallotStatusTally(StatusForRound.FINAL_ROUND_SURPLUS)); } - return inactiveMap; + + return result; } // adds action objects to input action list representing all actions applied this round