diff --git a/build.gradle b/build.gradle
index c54a605f5..3bcf49ee5 100644
--- a/build.gradle
+++ b/build.gradle
@@ -1,6 +1,7 @@
plugins {
id "application"
id "checkstyle"
+ id "com.github.spotbugs" version "5.0.6"
id "idea"
id "java-library"
id "org.beryx.jlink" version "2.20.0"
@@ -43,9 +44,14 @@ checkstyle {
maxWarnings = 0
ignoreFailures = false
}
-System.setProperty( "org.checkstyle.google.suppressionfilter.config",
+System.setProperty("org.checkstyle.google.suppressionfilter.config",
"$projectDir/config/checkstyle/checkstyle-suppressions.xml")
+spotbugs {
+ toolVersion = '4.6.0'
+ excludeFilter = file("config/spotbugs/exclude.xml")
+}
+
// ### Idea plugin settings
idea {
module {
diff --git a/config/spotbugs/exclude.xml b/config/spotbugs/exclude.xml
new file mode 100644
index 000000000..7a6a92166
--- /dev/null
+++ b/config/spotbugs/exclude.xml
@@ -0,0 +1,19 @@
+
+
+ xsi:schemaLocation="https://raw.githubusercontent.com/spotbugs/spotbugs/4.6.0/spotbugs/etc/findbugsfilter.xsd">
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/main/java/network/brightspots/rcv/ClearBallotCvrReader.java b/src/main/java/network/brightspots/rcv/ClearBallotCvrReader.java
index 31caeed03..ca47ae095 100644
--- a/src/main/java/network/brightspots/rcv/ClearBallotCvrReader.java
+++ b/src/main/java/network/brightspots/rcv/ClearBallotCvrReader.java
@@ -20,6 +20,7 @@
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -48,7 +49,7 @@ void readCastVoteRecords(List castVoteRecords, String contestId)
// map for tracking unrecognized candidates during parsing
Map unrecognizedCandidateCounts = new HashMap<>();
try {
- csvReader = new BufferedReader(new FileReader(this.cvrPath));
+ csvReader = new BufferedReader(new FileReader(this.cvrPath, StandardCharsets.UTF_8));
// each "choice column" in the input Csv corresponds to a unique ranking: candidate+rank pair
// we parse these rankings from the header row into a map for lookup during CVR parsing
String firstRow = csvReader.readLine();
@@ -103,11 +104,10 @@ void readCastVoteRecords(List castVoteRecords, String contestId)
// parse rankings
String[] cvrData = row.split(",");
ArrayList> rankings = new ArrayList<>();
- for (int columnIndex : columnIndexToRanking.keySet()) {
- if (Integer.parseInt(cvrData[columnIndex]) == 1) {
+ for (var entry : columnIndexToRanking.entrySet()) {
+ if (Integer.parseInt(cvrData[entry.getKey()]) == 1) {
// user marked this column
- Pair ranking = columnIndexToRanking.get(columnIndex);
- rankings.add(ranking);
+ rankings.add(entry.getValue());
}
}
// create the cast vote record
diff --git a/src/main/java/network/brightspots/rcv/CommonDataFormatReader.java b/src/main/java/network/brightspots/rcv/CommonDataFormatReader.java
index a638b6827..eac1e9472 100644
--- a/src/main/java/network/brightspots/rcv/CommonDataFormatReader.java
+++ b/src/main/java/network/brightspots/rcv/CommonDataFormatReader.java
@@ -129,165 +129,168 @@ void parseXml(List castVoteRecords)
// load XML
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
- FileInputStream inputStream = new FileInputStream(new File(filePath));
- CastVoteRecordReport cvrReport = xmlMapper.readValue(inputStream, CastVoteRecordReport.class);
-
- // Parse static election data:
-
- // Find the Contest we are tabulating:
- // Note: Contest is different from CVRContest objects which appear in CVRSnapshots
- Contest contestToTabulate = null;
- for (Election election : cvrReport.Election) {
- for (Contest contest : election.Contest) {
- if (contest.Name.equals(this.contestId)) {
- contestToTabulate = contest;
- break;
+ try (FileInputStream inputStream = new FileInputStream(filePath)) {
+ CastVoteRecordReport cvrReport = xmlMapper.readValue(inputStream, CastVoteRecordReport.class);
+ inputStream.close();
+
+ // Parse static election data:
+
+ // Find the Contest we are tabulating:
+ // Note: Contest is different from CVRContest objects which appear in CVRSnapshots
+ Contest contestToTabulate = null;
+ for (Election election : cvrReport.Election) {
+ for (Contest contest : election.Contest) {
+ if (contest.Name.equals(this.contestId)) {
+ contestToTabulate = contest;
+ break;
+ }
}
}
- }
-
- if (contestToTabulate == null) {
- Logger.severe("Contest \"%s\" from config file not found!", this.contestId);
- throw new CvrParseException();
- }
- // build a map of Candidates
- HashMap candidateById = new HashMap<>();
- for (Election election : cvrReport.Election) {
- for (Candidate candidate : election.Candidate) {
- candidateById.put(candidate.ObjectId, candidate);
+ if (contestToTabulate == null) {
+ Logger.severe("Contest \"%s\" from config file not found!", this.contestId);
+ throw new CvrParseException();
}
- }
- // ContestSelections
- HashMap contestSelectionById = new HashMap<>();
- for (ContestSelection contestSelection : contestToTabulate.ContestSelection) {
- contestSelectionById.put(contestSelection.ObjectId, contestSelection);
- }
+ // build a map of Candidates
+ HashMap candidateById = new HashMap<>();
+ for (Election election : cvrReport.Election) {
+ for (Candidate candidate : election.Candidate) {
+ candidateById.put(candidate.ObjectId, candidate);
+ }
+ }
- // build a map of GpUnits (aka precinct or district)
- HashMap gpUnitById = new HashMap<>();
- for (GpUnit gpUnit : cvrReport.GpUnit) {
- gpUnitById.put(gpUnit.ObjectId, gpUnit);
- }
+ // ContestSelections
+ HashMap contestSelectionById = new HashMap<>();
+ for (ContestSelection contestSelection : contestToTabulate.ContestSelection) {
+ contestSelectionById.put(contestSelection.ObjectId, contestSelection);
+ }
- // process the Cvrs
- int cvrIndex = 0;
- String fileName = new File(filePath).getName();
- for (CVR cvr : cvrReport.CVR) {
- CVRContest contest = getCvrContestXml(cvr, contestToTabulate);
- if (contest == null) {
- // the CVR does not contain any votes for this contest
- continue;
+ // build a map of GpUnits (aka precinct or district)
+ HashMap gpUnitById = new HashMap<>();
+ for (GpUnit gpUnit : cvrReport.GpUnit) {
+ gpUnitById.put(gpUnit.ObjectId, gpUnit);
}
- List> rankings = new ArrayList<>();
- // parse CVRContestSelections into rankings
- // they will be null for an undervote
- if (contest.CVRContestSelection != null) {
- for (CVRContestSelection cvrContestSelection : contest.CVRContestSelection) {
- if (cvrContestSelection.Status != null
- && cvrContestSelection.Status.equals("needs-adjudication")) {
- Logger.info("Contest Selection needs adjudication. Skipping.");
- continue;
- }
- String contestSelectionId = cvrContestSelection.ContestSelectionId;
- ContestSelection contestSelection = contestSelectionById.get(contestSelectionId);
- if (contestSelection == null) {
- Logger.severe("ContestSelection \"%s\" from CVR not found!", contestSelectionId);
- throw new CvrParseException();
- }
- String candidateId;
- // check for declared write-in:
- if (contestSelection.IsWriteIn != null
- && contestSelection.IsWriteIn.equals(BOOLEAN_TRUE)) {
- candidateId = Tabulator.UNDECLARED_WRITE_IN_OUTPUT_LABEL;
- } else {
- // validate candidate Ids:
- // CDF allows multiple candidate Ids to support party ticket voting options
- // but in practice this is always a single candidate id
- if (contestSelection.CandidateIds == null
- || contestSelection.CandidateIds.length == 0) {
- Logger.severe(
- "CandidateSelection \"%s\" has no CandidateIds!", contestSelection.ObjectId);
- throw new CvrParseException();
- }
- if (contestSelection.CandidateIds.length > 1) {
- Logger.warning(
- "CandidateSelection \"%s\" has multiple CandidateIds. "
- + "Only the first one will be processed.",
- contestSelection.ObjectId);
- }
- Candidate candidate = candidateById.get(contestSelection.CandidateIds[0]);
- if (candidate == null) {
- Logger.severe(
- "CandidateId \"%s\" from ContestSelectionId \"%s\" not found!",
- contestSelection.CandidateIds[0], contestSelection.ObjectId);
- throw new CvrParseException();
+ // process the Cvrs
+ int cvrIndex = 0;
+ String fileName = new File(filePath).getName();
+ for (CVR cvr : cvrReport.CVR) {
+ CVRContest contest = getCvrContestXml(cvr, contestToTabulate);
+ if (contest == null) {
+ // the CVR does not contain any votes for this contest
+ continue;
+ }
+ List> rankings = new ArrayList<>();
+ // parse CVRContestSelections into rankings
+ // they will be null for an undervote
+ if (contest.CVRContestSelection != null) {
+ for (CVRContestSelection cvrContestSelection : contest.CVRContestSelection) {
+ if (cvrContestSelection.Status != null
+ && cvrContestSelection.Status.equals("needs-adjudication")) {
+ Logger.info("Contest Selection needs adjudication. Skipping.");
+ continue;
}
- candidateId = candidate.Name;
- if (candidateId.equals(overvoteLabel)) {
- candidateId = Tabulator.EXPLICIT_OVERVOTE_LABEL;
- } else if (!config.getCandidateCodeList().contains(candidateId)) {
- Logger.severe("Unrecognized candidate found in CVR: %s", candidateId);
- unrecognizedCandidateCounts.merge(candidateId, 1, Integer::sum);
+ String contestSelectionId = cvrContestSelection.ContestSelectionId;
+ ContestSelection contestSelection = contestSelectionById.get(contestSelectionId);
+ if (contestSelection == null) {
+ Logger.severe("ContestSelection \"%s\" from CVR not found!", contestSelectionId);
+ throw new CvrParseException();
}
- }
-
- if (cvrContestSelection.Rank == null) {
- for (SelectionPosition selectionPosition : cvrContestSelection.SelectionPosition) {
- if (selectionPosition.CVRWriteIn != null) {
- candidateId = Tabulator.UNDECLARED_WRITE_IN_OUTPUT_LABEL;
- }
- // ignore if no indication is present (NIST 1500-103 section 3.4.2)
- if (selectionPosition.HasIndication != null
- && selectionPosition.HasIndication.equals(STATUS_NO)) {
- continue;
+ String candidateId;
+ // check for declared write-in:
+ if (contestSelection.IsWriteIn != null
+ && contestSelection.IsWriteIn.equals(BOOLEAN_TRUE)) {
+ candidateId = Tabulator.UNDECLARED_WRITE_IN_OUTPUT_LABEL;
+ } else {
+ // validate candidate Ids:
+ // CDF allows multiple candidate Ids to support party ticket voting options
+ // but in practice this is always a single candidate id
+ if (contestSelection.CandidateIds == null
+ || contestSelection.CandidateIds.length == 0) {
+ Logger.severe(
+ "CandidateSelection \"%s\" has no CandidateIds!", contestSelection.ObjectId);
+ throw new CvrParseException();
}
- // skip if not allocable
- if (selectionPosition.IsAllocable.equals(STATUS_NO)) {
- continue;
+ if (contestSelection.CandidateIds.length > 1) {
+ Logger.warning(
+ "CandidateSelection \"%s\" has multiple CandidateIds. "
+ + "Only the first one will be processed.",
+ contestSelection.ObjectId);
}
- if (selectionPosition.Rank == null) {
+
+ Candidate candidate = candidateById.get(contestSelection.CandidateIds[0]);
+ if (candidate == null) {
Logger.severe(
- "No Rank found on CVR \"%s\" Contest \"%s\"!", cvr.UniqueId, contest.ContestId);
+ "CandidateId \"%s\" from ContestSelectionId \"%s\" not found!",
+ contestSelection.CandidateIds[0], contestSelection.ObjectId);
throw new CvrParseException();
}
- Integer rank = Integer.parseInt(selectionPosition.Rank);
+ candidateId = candidate.Name;
+ if (candidateId.equals(overvoteLabel)) {
+ candidateId = Tabulator.EXPLICIT_OVERVOTE_LABEL;
+ } else if (!config.getCandidateCodeList().contains(candidateId)) {
+ Logger.severe("Unrecognized candidate found in CVR: %s", candidateId);
+ unrecognizedCandidateCounts.merge(candidateId, 1, Integer::sum);
+ }
+ }
+
+ if (cvrContestSelection.Rank == null) {
+ for (SelectionPosition selectionPosition : cvrContestSelection.SelectionPosition) {
+ if (selectionPosition.CVRWriteIn != null) {
+ candidateId = Tabulator.UNDECLARED_WRITE_IN_OUTPUT_LABEL;
+ }
+ // ignore if no indication is present (NIST 1500-103 section 3.4.2)
+ if (selectionPosition.HasIndication != null
+ && selectionPosition.HasIndication.equals(STATUS_NO)) {
+ continue;
+ }
+ // skip if not allocable
+ if (selectionPosition.IsAllocable.equals(STATUS_NO)) {
+ continue;
+ }
+ if (selectionPosition.Rank == null) {
+ Logger.severe(
+ "No Rank found on CVR \"%s\" Contest \"%s\"!", cvr.UniqueId,
+ contest.ContestId);
+ throw new CvrParseException();
+ }
+ Integer rank = Integer.parseInt(selectionPosition.Rank);
+ rankings.add(new Pair<>(rank, candidateId));
+ }
+ } else {
+ Integer rank = Integer.parseInt(cvrContestSelection.Rank);
rankings.add(new Pair<>(rank, candidateId));
}
- } else {
- Integer rank = Integer.parseInt(cvrContestSelection.Rank);
- rankings.add(new Pair<>(rank, candidateId));
}
}
- }
- // Extract GPUnit if provided
- String precinctId = null;
- if (cvr.BallotStyleUnitId != null) {
- GpUnit unit = gpUnitById.get(cvr.BallotStyleUnitId);
- if (unit == null) {
- Logger.severe(
- "GpUnit \"%s\" for CVR \"%s\" not found!", cvr.BallotStyleUnitId, cvr.UniqueId);
- throw new CvrParseException();
+ // Extract GPUnit if provided
+ String precinctId = null;
+ if (cvr.BallotStyleUnitId != null) {
+ GpUnit unit = gpUnitById.get(cvr.BallotStyleUnitId);
+ if (unit == null) {
+ Logger.severe(
+ "GpUnit \"%s\" for CVR \"%s\" not found!", cvr.BallotStyleUnitId, cvr.UniqueId);
+ throw new CvrParseException();
+ }
+ precinctId = unit.Name;
}
- precinctId = unit.Name;
- }
- String computedCastVoteRecordId = String.format("%s(%d)", fileName, ++cvrIndex);
- // create the new CastVoteRecord
- CastVoteRecord newRecord =
- new CastVoteRecord(computedCastVoteRecordId, cvr.UniqueId, precinctId, null, rankings);
- castVoteRecords.add(newRecord);
+ String computedCastVoteRecordId = String.format("%s(%d)", fileName, ++cvrIndex);
+ // create the new CastVoteRecord
+ CastVoteRecord newRecord =
+ new CastVoteRecord(computedCastVoteRecordId, cvr.UniqueId, precinctId, null, rankings);
+ castVoteRecords.add(newRecord);
- // provide some user feedback on the CVR count
- if (castVoteRecords.size() % 50000 == 0) {
- Logger.info("Parsed %d cast vote records.", castVoteRecords.size());
+ // provide some user feedback on the CVR count
+ if (castVoteRecords.size() % 50000 == 0) {
+ Logger.info("Parsed %d cast vote records.", castVoteRecords.size());
+ }
+ }
+ if (unrecognizedCandidateCounts.size() > 0) {
+ throw new UnrecognizedCandidatesException(unrecognizedCandidateCounts);
}
- }
- if (unrecognizedCandidateCounts.size() > 0) {
- throw new UnrecognizedCandidatesException(unrecognizedCandidateCounts);
}
}
@@ -461,7 +464,7 @@ void parseJson(List castVoteRecords)
// The following classes are based on the NIST 1500-103 UML structure.
// Many of the elements represented here will not be present on any particular implementation of
// a Cdf Cvr report. Many of these elements are also irrelevant for tabulation purposes.
- // However they are included here for completeness and to aid in interpreting the UML.
+ // However, they are included here for completeness and to aid in interpreting the UML.
// Note that fields identified as "boolean-like" can be (yes, no, 1, 0, or null)
static class ContestSelection {
diff --git a/src/main/java/network/brightspots/rcv/GuiConfigController.java b/src/main/java/network/brightspots/rcv/GuiConfigController.java
index 2185617dc..f6e45ee0a 100644
--- a/src/main/java/network/brightspots/rcv/GuiConfigController.java
+++ b/src/main/java/network/brightspots/rcv/GuiConfigController.java
@@ -25,6 +25,7 @@
import java.io.File;
import java.io.InputStreamReader;
import java.net.URL;
+import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
@@ -271,14 +272,10 @@ private static String getTextOrEmptyString(TextField textField) {
private static String loadTxtFileIntoString(String configFileDocumentationFilename) {
String text;
- try {
- text =
- new BufferedReader(
- new InputStreamReader(
- Objects.requireNonNull(
- ClassLoader.getSystemResourceAsStream(configFileDocumentationFilename))))
- .lines()
- .collect(Collectors.joining("\n"));
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(Objects.requireNonNull(
+ ClassLoader.getSystemResourceAsStream(configFileDocumentationFilename)),
+ StandardCharsets.UTF_8))) {
+ text = reader.lines().collect(Collectors.joining("\n"));
} catch (Exception exception) {
Logger.severe(
"Error loading text file: %s\n%s",
@@ -1000,7 +997,7 @@ public LocalDate fromString(String string) {
textFieldNumberOfWinners.setDisable(false);
}
case MULTI_SEAT_BOTTOMS_UP_UNTIL_N_WINNERS, MULTI_SEAT_SEQUENTIAL_WINNER_TAKES_ALL ->
- textFieldNumberOfWinners.setDisable(false);
+ textFieldNumberOfWinners.setDisable(false);
case MULTI_SEAT_BOTTOMS_UP_USING_PERCENTAGE_THRESHOLD -> {
textFieldNumberOfWinners.setText("0");
textFieldMultiSeatBottomsUpPercentageThreshold.setDisable(false);
diff --git a/src/main/java/network/brightspots/rcv/HartCvrReader.java b/src/main/java/network/brightspots/rcv/HartCvrReader.java
index 066914168..4fda9cab2 100644
--- a/src/main/java/network/brightspots/rcv/HartCvrReader.java
+++ b/src/main/java/network/brightspots/rcv/HartCvrReader.java
@@ -72,12 +72,11 @@ void readCastVoteRecordsFromFolder(List castVoteRecords)
// parse Cvr xml file into CastVoteRecord objects and add them to the input List
void readCastVoteRecord(List castVoteRecords, Path path) throws IOException {
- try {
- Logger.info("Reading Hart cast vote record file: %s...", path.getFileName());
+ Logger.info("Reading Hart cast vote record file: %s...", path.getFileName());
- XmlMapper xmlMapper = new XmlMapper();
- xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
- FileInputStream inputStream = new FileInputStream(path.toFile());
+ XmlMapper xmlMapper = new XmlMapper();
+ xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
+ try (FileInputStream inputStream = new FileInputStream(path.toFile())) {
HartCvrXml xmlCvr = xmlMapper.readValue(inputStream, HartCvrXml.class);
for (Contest contest : xmlCvr.Contests) {
@@ -125,7 +124,7 @@ void readCastVoteRecord(List castVoteRecords, Path path) throws
Logger.info("Parsed %d cast vote records.", castVoteRecords.size());
}
}
- } catch (Exception exception) {
+ } catch (IOException exception) {
Logger.severe("Error parsing cast vote record:\n%s", exception);
throw exception;
}
diff --git a/src/main/java/network/brightspots/rcv/JsonParser.java b/src/main/java/network/brightspots/rcv/JsonParser.java
index 952f9ca22..92253b655 100644
--- a/src/main/java/network/brightspots/rcv/JsonParser.java
+++ b/src/main/java/network/brightspots/rcv/JsonParser.java
@@ -27,6 +27,7 @@
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
@SuppressWarnings("SameParameterValue")
class JsonParser {
@@ -38,25 +39,28 @@ class JsonParser {
// returns: instance of the object parsed from json or null if there was a problem
private static T readFromFile(String jsonFilePath, Class valueType, boolean logsEnabled) {
T createdObject;
- try {
- FileReader fileReader = new FileReader(jsonFilePath);
+ try (FileReader fileReader = new FileReader(jsonFilePath, StandardCharsets.UTF_8)) {
ObjectMapper objectMapper = new ObjectMapper();
createdObject = objectMapper.readValue(fileReader, valueType);
} catch (JsonParseException | JsonMappingException exception) {
if (logsEnabled) {
Logger.severe(
- "Error parsing JSON file: %s\n%s\n"
- + "Check file formatting and values and make sure they are correct!\n"
- + "It might help to try surrounding values causing problems with quotes (e.g. "
- + " \"value\").\nSee config_file_documentation.txt for more details.",
+ """
+ Error parsing JSON file: %s
+ %s
+ Check file formatting and values and make sure they are correct!
+ It might help to try surrounding values causing problems with quotes (e.g. "value").
+ See config_file_documentation.txt for more details.""",
jsonFilePath, exception);
}
createdObject = null;
} catch (IOException exception) {
if (logsEnabled) {
Logger.severe(
- "Error opening file: %s\n%s\n"
- + "Check file path and permissions and make sure they are correct!",
+ """
+ Error opening file: %s
+ %s
+ Check file path and permissions and make sure they are correct!""",
jsonFilePath, exception);
}
createdObject = null;
diff --git a/src/main/java/network/brightspots/rcv/ResultsWriter.java b/src/main/java/network/brightspots/rcv/ResultsWriter.java
index 1dc123423..05d3a121f 100644
--- a/src/main/java/network/brightspots/rcv/ResultsWriter.java
+++ b/src/main/java/network/brightspots/rcv/ResultsWriter.java
@@ -18,7 +18,7 @@
* Purpose:
* Helper class takes tabulation results data as input and generates summary files which
* contains results summary information.
- * Currently we support a CSV summary file and a JSON summary file.
+ * Currently, we support a CSV summary file and a JSON summary file.
*/
package network.brightspots.rcv;
@@ -137,37 +137,29 @@ private static String generateCvrSnapshotId(String cvrId, Integer round) {
// generates an internal ContestSelectionId based on a candidate code
private static String getCdfContestSelectionIdForCandidateCode(String code) {
- String id = cdfCandidateCodeToContestSelectionId.get(code);
- if (id == null) {
- id = String.format("cs-%s", sanitizeStringForOutput(code).toLowerCase());
- cdfCandidateCodeToContestSelectionId.put(code, id);
- }
- return id;
+ return cdfCandidateCodeToContestSelectionId.computeIfAbsent(code,
+ c -> String.format("cs-%s", sanitizeStringForOutput(c).toLowerCase()));
}
// generates an internal CandidateId based on a candidate code
private static String getCdfCandidateIdForCandidateCode(String code) {
- String id = cdfCandidateCodeToCandidateId.get(code);
- if (id == null) {
- id = String.format("c-%s", sanitizeStringForOutput(code).toLowerCase());
- cdfCandidateCodeToCandidateId.put(code, id);
- }
- return id;
+ return cdfCandidateCodeToCandidateId.computeIfAbsent(code,
+ c -> String.format("c-%s", sanitizeStringForOutput(c).toLowerCase()));
}
// Instead of a map from rank to list of candidates, we need a sorted list of candidates
// with the ranks they were given. (Ordinarily a candidate will have only a single rank, but they
// could have multiple ranks if the ballot duplicates the candidate, i.e. assigns them multiple
- // ranks.
+ // ranks.)
// We sort by the lowest (best) rank, then alphabetically by name.
private static List>> getCandidatesWithRanksList(
Map> rankToCandidateIds) {
Map> candidateIdToRanks = new HashMap<>();
// first group the ranks by candidate
- for (int rank : rankToCandidateIds.keySet()) {
- for (String candidateId : rankToCandidateIds.get(rank)) {
+ for (var entry : rankToCandidateIds.entrySet()) {
+ for (String candidateId : entry.getValue()) {
candidateIdToRanks.computeIfAbsent(candidateId, k -> new LinkedList<>());
- candidateIdToRanks.get(candidateId).add(rank);
+ candidateIdToRanks.get(candidateId).add(entry.getKey());
}
}
// we want the ranks for a given candidate in ascending order
@@ -238,13 +230,12 @@ ResultsWriter setPrecinctIds(Set precinctIds) {
}
ResultsWriter setCandidatesToRoundEliminated(Map candidatesToRoundEliminated) {
- // roundToEliminatedCandidates is the inverse of candidatesToRoundEliminated map
+ // roundToEliminatedCandidates is the inverse of candidatesToRoundEliminated map,
// so we can look up who got eliminated for each round
roundToEliminatedCandidates = new HashMap<>();
- for (String candidate : candidatesToRoundEliminated.keySet()) {
- int round = candidatesToRoundEliminated.get(candidate);
- roundToEliminatedCandidates.computeIfAbsent(round, k -> new LinkedList<>());
- roundToEliminatedCandidates.get(round).add(candidate);
+ for (var entry : candidatesToRoundEliminated.entrySet()) {
+ roundToEliminatedCandidates.computeIfAbsent(entry.getValue(), k -> new LinkedList<>());
+ roundToEliminatedCandidates.get(entry.getValue()).add(entry.getKey());
}
return this;
}
@@ -252,10 +243,9 @@ ResultsWriter setCandidatesToRoundEliminated(Map candidatesToRo
ResultsWriter setWinnerToRound(Map winnerToRound) {
// very similar to the logic in setCandidatesToRoundEliminated above
roundToWinningCandidates = new HashMap<>();
- for (String candidate : winnerToRound.keySet()) {
- int round = winnerToRound.get(candidate);
- roundToWinningCandidates.computeIfAbsent(round, k -> new LinkedList<>());
- roundToWinningCandidates.get(round).add(candidate);
+ for (var entry : winnerToRound.entrySet()) {
+ roundToWinningCandidates.computeIfAbsent(entry.getValue(), k -> new LinkedList<>());
+ roundToWinningCandidates.get(entry.getValue()).add(entry.getKey());
}
return this;
}
@@ -280,18 +270,15 @@ void generatePrecinctSummaryFiles(
Map numBallotsByPrecinct)
throws IOException {
Set filenames = new HashSet<>();
- for (String precinct : precinctRoundTallies.keySet()) {
+ for (var entry : precinctRoundTallies.entrySet()) {
+ String precinct = entry.getKey();
+ Map> roundTallies = entry.getValue();
String precinctFileString = getPrecinctFileString(precinct, filenames);
String outputPath =
getOutputFilePathFromInstance(String.format("%s_precinct_summary", precinctFileString));
int numBallots = numBallotsByPrecinct.get(precinct);
- generateSummarySpreadsheet(
- precinctRoundTallies.get(precinct), numBallots, precinct, outputPath);
- generateSummaryJson(
- precinctRoundTallies.get(precinct),
- precinctTallyTransfers.get(precinct),
- precinct,
- outputPath);
+ generateSummarySpreadsheet(roundTallies, numBallots, precinct, outputPath);
+ generateSummaryJson(roundTallies, precinctTallyTransfers.get(precinct), precinct, outputPath);
}
}
@@ -463,7 +450,7 @@ private void addHeaderRows(CSVPrinter csvPrinter, String precinct) throws IOExce
csvPrinter.println();
}
- // return a list of all input candidates sorted from highest tally to lowest
+ // return a list of all input candidates sorted from the highest tally to lowest
private List sortCandidatesByTally(Map tally) {
List> entries = new ArrayList<>(tally.entrySet());
entries.sort(
@@ -479,7 +466,7 @@ private List sortCandidatesByTally(Map tally) {
return ret;
});
List sortedCandidates = new LinkedList<>();
- for (Map.Entry entry : entries) {
+ for (var entry : entries) {
sortedCandidates.add(entry.getKey());
}
return sortedCandidates;
@@ -596,7 +583,7 @@ List writeGenericCvrCsv(
// create NIST Common Data Format CVR json
void generateCdfJson(List castVoteRecords)
throws IOException, RoundSnapshotDataMissingException {
- // generate GpUnitIds for precincts "geo-political units" (can be a precinct or jurisdiction)
+ // generate GpUnitIds for precincts "geopolitical units" (can be a precinct or jurisdiction)
gpUnitIds = generateGpUnitIds();
String outputPath = getOutputFilePathFromInstance("cvr_cdf") + ".json";
@@ -876,8 +863,8 @@ private void generateSummaryJson(
private Map updateCandidateNamesInTally(Map tally) {
Map newTally = new HashMap<>();
- for (String key : tally.keySet()) {
- newTally.put(config.getNameForCandidateCode(key), tally.get(key));
+ for (var entry : tally.entrySet()) {
+ newTally.put(config.getNameForCandidateCode(entry.getKey()), entry.getValue());
}
return newTally;
}
@@ -913,12 +900,12 @@ private void addActionObjects(
if (transfersFromCandidate != null) {
// We want to replace candidate IDs with names here, too.
Map translatedTransfers = new HashMap<>();
- for (String candidateId : transfersFromCandidate.keySet()) {
+ for (var entry : transfersFromCandidate.entrySet()) {
// candidateName will be null for special values like "exhausted"
- String candidateName = config.getNameForCandidateCode(candidateId);
+ String candidateName = config.getNameForCandidateCode(entry.getKey());
translatedTransfers.put(
- candidateName != null ? candidateName : candidateId,
- transfersFromCandidate.get(candidateId));
+ candidateName != null ? candidateName : entry.getKey(),
+ entry.getValue());
}
action.put("transfers", translatedTransfers);
}
diff --git a/src/main/java/network/brightspots/rcv/Tabulator.java b/src/main/java/network/brightspots/rcv/Tabulator.java
index e57a9fbd5..a64c3ffbe 100644
--- a/src/main/java/network/brightspots/rcv/Tabulator.java
+++ b/src/main/java/network/brightspots/rcv/Tabulator.java
@@ -136,9 +136,9 @@ Set tabulate() throws TabulationCancelledException {
// - If winnerElectionMode is "Bottoms-up using percentage threshold", we loop until all
// remaining candidates have vote shares that meet or exceed that threshold.
//
- // At each iteration, we'll either a) identify one or more
+ // At each iteration, we'll either a. identify one or more
// winners and transfer their votes to the remaining candidates (if we still need to find more
- // winners), or b) eliminate one or more candidates and gradually transfer votes to the
+ // winners), or b. eliminate one or more candidates and gradually transfer votes to the
// remaining candidates.
while (shouldContinueTabulating()) {
currentRound++;
@@ -268,15 +268,14 @@ private void updatePastWinnerTallies() {
Map previousRoundTally = roundTallies.get(currentRound - 1);
List winnersToProcess = new LinkedList<>();
Set winnersRequiringComputation = new HashSet<>();
- for (String winner : winnerToRound.keySet()) {
+ for (var entry : winnerToRound.entrySet()) {
// skip someone who won in the current round (we only care about previous round winners)
- int winningRound = winnerToRound.get(winner);
- if (winningRound == currentRound) {
+ if (entry.getValue() == currentRound) {
continue;
}
- winnersToProcess.add(winner);
- if (winningRound == currentRound - 1) {
- winnersRequiringComputation.add(winner);
+ winnersToProcess.add(entry.getKey());
+ if (entry.getValue() == currentRound - 1) {
+ winnersRequiringComputation.add(entry.getKey());
}
}
@@ -291,10 +290,8 @@ private void updatePastWinnerTallies() {
// initialize or populate precinct tallies
if (config.isTabulateByPrecinctEnabled()) {
- for (String precinct : precinctRoundTallies.keySet()) {
- // this is all the tallies for the given precinct
- Map> roundTalliesForPrecinct =
- precinctRoundTallies.get(precinct);
+ // this is all the tallies for the given precinct
+ for (var roundTalliesForPrecinct : precinctRoundTallies.values()) {
// and this is the tally for the current round for the precinct
Map roundTallyForPrecinct = roundTalliesForPrecinct.get(currentRound);
for (String winner : winnersToProcess) {
@@ -307,16 +304,18 @@ private void updatePastWinnerTallies() {
}
}
- // process all the CVRs if needed (if we have any winners from the previous round to process)
+ // process all the CVRs if needed (i.e. if we have any winners from the previous round to
+ // process)
if (winnersRequiringComputation.size() > 0) {
for (CastVoteRecord cvr : castVoteRecords) {
// the record of winners who got partial votes from this CVR
Map winnerToFractionalValue = cvr.getWinnerToFractionalValue();
- for (String winner : winnerToFractionalValue.keySet()) {
+ for (var entry : winnerToFractionalValue.entrySet()) {
+ String winner = entry.getKey();
if (!winnersRequiringComputation.contains(winner)) {
continue;
}
- BigDecimal fractionalTransferValue = winnerToFractionalValue.get(winner);
+ BigDecimal fractionalTransferValue = entry.getValue();
incrementTally(roundTally, fractionalTransferValue, winner);
if (config.isTabulateByPrecinctEnabled() && cvr.getPrecinct() != null) {
@@ -366,7 +365,7 @@ private void setWinningThreshold(Map currentRoundCandidateTo
? config.getNumberOfWinners()
: config.getNumberOfWinners() + 1);
// If we use integers, we shouldn't use any decimal places.
- // Otherwise we use the amount of decimal places specified by the user
+ // Otherwise, we use the amount of decimal places specified by the user.
int decimals =
config.isNonIntegerWinningThresholdEnabled()
? config.getDecimalPlacesForVoteArithmetic()
@@ -418,7 +417,7 @@ private boolean shouldContinueTabulating() {
}
}
- // This handles continued tabulation after a winner has been chosen when
+ // Handles continued tabulation after a winner has been chosen when
// continueUntilTwoCandidatesRemain is true.
private boolean isCandidateContinuing(String candidate) {
CandidateStatus status = getCandidateStatus(candidate);
@@ -475,11 +474,10 @@ private List identifyWinners(
// case we just wait until there are numWinners candidates remaining and then declare all
// of them as winners simultaneously).
// tally indexes over all tallies to find any winners
- for (BigDecimal tally : currentRoundTallyToCandidates.keySet()) {
- if (tally.compareTo(winningThreshold) >= 0) {
+ for (var entry : currentRoundTallyToCandidates.entrySet()) {
+ if (entry.getKey().compareTo(winningThreshold) >= 0) {
// we have winner(s)
- List winningCandidates = currentRoundTallyToCandidates.get(tally);
- for (String candidate : winningCandidates) {
+ for (String candidate : entry.getValue()) {
// The undeclared write-in placeholder can't win!
if (!candidate.equals(UNDECLARED_WRITE_IN_OUTPUT_LABEL)) {
selectedWinners.add(candidate);
@@ -490,7 +488,7 @@ private List identifyWinners(
}
}
- // Edge case: if we've identified multiple winners in this round but we're only supposed to
+ // Edge case: if we've identified multiple winners in this round, but we're only supposed to
// elect one winner per round, pick the top vote-getter and defer the others to subsequent
// rounds.
if (config.isMultiSeatAllowOnlyOneWinnerPerRoundEnabled() && selectedWinners.size() > 1) {
@@ -562,14 +560,14 @@ private List dropCandidatesBelowThreshold(
BigDecimal threshold = config.getMinimumVoteThreshold();
if (threshold.signum() == 1
&& currentRoundTallyToCandidates.firstKey().compareTo(threshold) < 0) {
- for (BigDecimal tally : currentRoundTallyToCandidates.keySet()) {
- if (tally.compareTo(threshold) < 0) {
- for (String candidate : currentRoundTallyToCandidates.get(tally)) {
+ for (var entry : currentRoundTallyToCandidates.entrySet()) {
+ if (entry.getKey().compareTo(threshold) < 0) {
+ for (String candidate : entry.getValue()) {
eliminated.add(candidate);
Logger.info(
"Eliminated candidate \"%s\" in round %d because they only had %s vote(s), below "
+ "the minimum threshold of %s.",
- candidate, currentRound, tally, threshold);
+ candidate, currentRound, entry.getKey(), threshold);
}
} else {
break;
@@ -721,7 +719,8 @@ private List runBatchElimination(
// At each iteration, currentVoteTally is the next-lowest vote count received by one or more
// candidate(s) in the current round.
- for (BigDecimal currentVoteTally : currentRoundTallyToCandidates.keySet()) {
+ for (var entry : currentRoundTallyToCandidates.entrySet()) {
+ BigDecimal currentVoteTally = entry.getKey();
// a shallow copy is sufficient
LinkedList newEliminations = new LinkedList<>(eliminations);
// Test whether leapfrogging is possible.
@@ -737,7 +736,7 @@ private List runBatchElimination(
}
// Add the candidates for the currentVoteTally to the seen list and accumulate their votes.
// currentCandidates is all candidates receiving the current vote tally
- List currentCandidates = currentRoundTallyToCandidates.get(currentVoteTally);
+ List currentCandidates = entry.getValue();
BigDecimal totalForThisRound =
config.multiply(currentVoteTally, new BigDecimal(currentCandidates.size()));
runningTotal = runningTotal.add(totalForThisRound);
@@ -758,7 +757,7 @@ private List runBatchElimination(
}
// purpose: determine if any overvote has occurred for this ranking set (from a CVR)
- // and if so return how to handle it based on the rules configuration in use
+ // and if so return how to handle it based on the rule configuration in use
// param: candidateSet all candidates this CVR contains at a particular rank
// return: an OvervoteDecision enum to be applied to the CVR under consideration
private OvervoteDecision getOvervoteDecision(Set candidateSet) {
@@ -1000,10 +999,10 @@ && isCandidateContinuing(cvr.getCurrentRecipientOfVote())) {
// Take the tallies for this round for each precinct and merge them into the main map tracking
// the tallies by precinct.
if (config.isTabulateByPrecinctEnabled()) {
- for (String precinct : roundTallyByPrecinct.keySet()) {
+ for (var entry : roundTallyByPrecinct.entrySet()) {
Map> roundTalliesForPrecinct =
- precinctRoundTallies.get(precinct);
- roundTalliesForPrecinct.put(currentRound, roundTallyByPrecinct.get(precinct));
+ precinctRoundTallies.get(entry.getKey());
+ roundTalliesForPrecinct.put(currentRound, entry.getValue());
}
}
diff --git a/src/main/java/network/brightspots/rcv/TabulatorSession.java b/src/main/java/network/brightspots/rcv/TabulatorSession.java
index a464870c1..e2e3dcf9f 100644
--- a/src/main/java/network/brightspots/rcv/TabulatorSession.java
+++ b/src/main/java/network/brightspots/rcv/TabulatorSession.java
@@ -32,6 +32,7 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@@ -142,14 +143,14 @@ void tabulate() {
ContestConfig config = ContestConfig.loadContestConfig(configPath);
checkConfigVersionMatchesApp(config);
boolean tabulationSuccess = false;
- //noinspection ConstantConditions
- if (config != null && config.validate() && setUpLogging(config)) {
+ if (config.validate() && setUpLogging(config)) {
Logger.info("Computer name: %s", Utils.getComputerName());
Logger.info("User name: %s", Utils.getUserName());
Logger.info("Config file: %s", configPath);
try {
Logger.fine("Begin config file contents:");
- BufferedReader reader = new BufferedReader(new FileReader(configPath));
+ BufferedReader reader = new BufferedReader(
+ new FileReader(configPath, StandardCharsets.UTF_8));
String line = reader.readLine();
while (line != null) {
Logger.fine(line);
@@ -164,7 +165,7 @@ void tabulate() {
if (config.isMultiSeatSequentialWinnerTakesAllEnabled()) {
Logger.info("This is a multi-pass IRV contest.");
int numWinners = config.getNumberOfWinners();
- // temporarily set config to single-seat so we can run sequential elections
+ // temporarily set config to single-seat so that we can run sequential elections
config.setNumberOfWinners(1);
while (config.getSequentialWinners().size() < numWinners) {
Logger.info(
@@ -324,7 +325,7 @@ private List parseCastVoteRecords(ContestConfig config, Set tiedCandidates) throws TabulationCa
String selection = null;
while (selection == null) {
- Scanner sc = new Scanner(System.in);
+ Scanner sc = new Scanner(System.in, StandardCharsets.UTF_8);
String userInput = sc.nextLine();
if (userInput.equals(CLI_CANCEL_COMMAND)) {
System.out.println("Cancelling tabulation...");
diff --git a/src/test/java/network/brightspots/rcv/TabulatorTests.java b/src/test/java/network/brightspots/rcv/TabulatorTests.java
index 4bb59187e..fa6085e53 100644
--- a/src/test/java/network/brightspots/rcv/TabulatorTests.java
+++ b/src/test/java/network/brightspots/rcv/TabulatorTests.java
@@ -33,6 +33,7 @@
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import network.brightspots.rcv.ContestConfig.Provider;
@@ -51,13 +52,10 @@ class TabulatorTests {
// compare file contents line by line to identify differences
private static boolean fileCompare(String path1, String path2) {
boolean result = true;
- FileReader fr1 = null;
- FileReader fr2 = null;
- try {
- fr1 = new FileReader(path1);
- BufferedReader br1 = new BufferedReader(fr1);
- fr2 = new FileReader(path2);
- BufferedReader br2 = new BufferedReader(fr2);
+ try (
+ BufferedReader br1 = new BufferedReader(new FileReader(path1, StandardCharsets.UTF_8));
+ BufferedReader br2 = new BufferedReader(new FileReader(path2, StandardCharsets.UTF_8))
+ ) {
int currentLine = 1;
int errorCount = 0;
@@ -91,17 +89,6 @@ private static boolean fileCompare(String path1, String path2) {
} catch (IOException exception) {
Logger.severe("Error reading file!\n%s", exception);
result = false;
- } finally {
- try {
- if (fr1 != null) {
- fr1.close();
- }
- if (fr2 != null) {
- fr2.close();
- }
- } catch (IOException exception) {
- Logger.severe("Error closing file!\n%s", exception);
- }
}
return result;
}
@@ -147,9 +134,9 @@ private static void runTabulationTest(String stem) {
}
// test passed so cleanup test output folder
File outputFolder = new File(session.getOutputPath());
- if (outputFolder.listFiles() != null) {
- //noinspection ConstantConditions
- for (File file : outputFolder.listFiles()) {
+ File[] files = outputFolder.listFiles();
+ if (files != null) {
+ for (File file : files) {
if (!file.isDirectory()) {
try {
Files.delete(file.toPath());