diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 95d4e549a..b360421f2 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -16,4 +16,4 @@ jobs: java-version: '17' distribution: 'temurin' - name: Test with Gradle - run: ./gradlew build + run: ./gradlew check diff --git a/.idea/misc.xml b/.idea/misc.xml index d1bacf819..67cba9600 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -6,7 +6,7 @@ - + diff --git a/build.gradle b/build.gradle index 4b301454c..3bcf49ee5 100644 --- a/build.gradle +++ b/build.gradle @@ -1,10 +1,11 @@ plugins { id "application" id "checkstyle" + id "com.github.spotbugs" version "5.0.6" id "idea" id "java-library" - id "org.openjfx.javafxplugin" version "0.0.11" id "org.beryx.jlink" version "2.20.0" + id "org.openjfx.javafxplugin" version "0.0.11" } repositories { @@ -12,14 +13,14 @@ repositories { } dependencies { - implementation "org.apache.commons:commons-csv:1.8" - implementation "org.apache.poi:poi-ooxml:4.1.2" - implementation "com.fasterxml.jackson.core:jackson-core:2.11.1" - implementation "com.fasterxml.jackson.core:jackson-annotations:2.11.1" - implementation "com.fasterxml.jackson.core:jackson-databind:2.11.1" - implementation "com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.11.1" - testImplementation "org.junit.jupiter:junit-jupiter-api:5.6.2" - testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.6.2" + implementation 'org.apache.commons:commons-csv:1.9.0' + implementation 'org.apache.poi:poi-ooxml:5.2.2' + implementation 'com.fasterxml.jackson.core:jackson-core:2.13.2' + implementation 'com.fasterxml.jackson.core:jackson-annotations:2.13.2' + implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.2.2' + implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.13.2' + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2' + testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2' } // ### Application plugin settings @@ -35,15 +36,22 @@ application { // ### Checkstyle plugin settings checkstyle { - toolVersion = "8.36.2" + toolVersion = '10.2' // Keep the below file updated along with subsequent versions of Checkstyle (make sure to choose // the tag matching the toolVersion above): - // https://github.com/checkstyle/checkstyle/blob/checkstyle-8.36.2/src/main/resources/google_checks.xml + // https://github.com/checkstyle/checkstyle/blob/checkstyle-10.2/src/main/resources/google_checks.xml configFile = file("$projectDir/config/checkstyle/google_checks.xml") + 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 { @@ -52,11 +60,10 @@ idea { } // ### Java plugin settings -sourceCompatibility = JavaVersion.VERSION_17 - -compileJava.dependsOn(clean) -compileJava { - options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation" +java { + toolchain { + languageVersion.set(JavaLanguageVersion.of(17)) + } } test { diff --git a/config/checkstyle/google_checks.xml b/config/checkstyle/google_checks.xml index 651247499..b26ce4297 100644 --- a/config/checkstyle/google_checks.xml +++ b/config/checkstyle/google_checks.xml @@ -1,7 +1,7 @@ + "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN" + "https://checkstyle.org/dtds/configuration_1_3.dtd"> - + + + + + + + + + + + + - + + + + + + + - + @@ -35,9 +51,9 @@ + value="\\u00(09|0(a|A)|0(c|C)|0(d|D)|22|27|5(C|c))|\\(0(10|11|12|14|15|42|47)|134)"/> + value="Consider using special escape sequence instead of octal value or Unicode escaped value."/> @@ -52,15 +68,15 @@ + value="LITERAL_TRY, LITERAL_FINALLY, LITERAL_IF, LITERAL_ELSE, LITERAL_SWITCH"/> + value="LITERAL_DO, LITERAL_ELSE, LITERAL_FOR, LITERAL_IF, LITERAL_WHILE"/> @@ -88,8 +104,9 @@ + value="COMMA, SEMI, TYPECAST, LITERAL_IF, LITERAL_ELSE, LITERAL_RETURN, + LITERAL_WHILE, LITERAL_DO, LITERAL_FOR, LITERAL_FINALLY, DO_WHILE, ELLIPSIS, + LITERAL_SWITCH, LITERAL_SYNCHRONIZED, LITERAL_TRY, LITERAL_CATCH, LAMBDA"/> @@ -99,7 +116,7 @@ + value="WhitespaceAround: ''{0}'' is not followed by whitespace. Empty blocks may only be represented as '{}' when not part of a multi-block statement (4.1.3)"/> + value="WhitespaceAround: ''{0}'' is not preceded with whitespace."/> @@ -120,7 +137,7 @@ @@ -136,13 +153,13 @@ - + - + @@ -155,78 +172,83 @@ + value="Package name ''{0}'' must match pattern ''{1}''."/> + value="Type name ''{0}'' must match pattern ''{1}''."/> + value="Member name ''{0}'' must match pattern ''{1}''."/> + value="Parameter name ''{0}'' must match pattern ''{1}''."/> + value="Lambda parameter name ''{0}'' must match pattern ''{1}''."/> + value="Catch parameter name ''{0}'' must match pattern ''{1}''."/> + value="Local variable name ''{0}'' must match pattern ''{1}''."/> - + + value="Class type name ''{0}'' must match pattern ''{1}''."/> - + + + value="Record component name ''{0}'' must match pattern ''{1}''."/> + + + + value="Method type name ''{0}'' must match pattern ''{1}''."/> + value="Interface type name ''{0}'' must match pattern ''{1}''."/> + value="GenericWhitespace ''{0}'' is followed by whitespace."/> + value="GenericWhitespace ''{0}'' is preceded with whitespace."/> + value="GenericWhitespace ''{0}'' should followed by whitespace."/> + value="GenericWhitespace ''{0}'' is not preceded with whitespace."/> - + @@ -236,10 +258,11 @@ + @@ -250,18 +273,18 @@ + value="BAND, BOR, BSR, BXOR, DIV, EQUAL, GE, GT, LAND, LE, LITERAL_INSTANCEOF, LOR, + LT, MINUS, MOD, NOT_EQUAL, PLUS, QUESTION, SL, SR, STAR, METHOD_REF, + TYPE_EXTENSION_AND "/> @@ -289,17 +313,17 @@ + value="^@return the *|^This method returns |^A [{]@code [a-zA-Z0-9]+[}]( is a )"/> + value="CLASS_DEF, INTERFACE_DEF, ENUM_DEF, METHOD_DEF, CTOR_DEF, VARIABLE_DEF"/> - + @@ -312,14 +336,19 @@ + + + + + - - - + value="Method name ''{0}'' must match pattern ''{1}''."/> + @@ -328,26 +357,9 @@ - + - - - - - - - - - - - - - - - - - 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/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 62d4c0535..7454180f2 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 669386b87..2e6e5897b 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew index fbd7c5158..1b6c78733 100755 --- a/gradlew +++ b/gradlew @@ -1,7 +1,7 @@ -#!/usr/bin/env sh +#!/bin/sh # -# Copyright 2015 the original author or authors. +# Copyright © 2015-2021 the original authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -17,67 +17,101 @@ # ############################################################################## -## -## Gradle start up script for UN*X -## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# ############################################################################## # Attempt to set APP_HOME + # Resolve links: $0 may be a link -PRG="$0" -# Need this for relative symlinks. -while [ -h "$PRG" ] ; do - ls=`ls -ld "$PRG"` - link=`expr "$ls" : '.*-> \(.*\)$'` - if expr "$link" : '/.*' > /dev/null; then - PRG="$link" - else - PRG=`dirname "$PRG"`"/$link" - fi +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac done -SAVED="`pwd`" -cd "`dirname \"$PRG\"`/" >/dev/null -APP_HOME="`pwd -P`" -cd "$SAVED" >/dev/null + +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit APP_NAME="Gradle" -APP_BASE_NAME=`basename "$0"` +APP_BASE_NAME=${0##*/} # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' # Use the maximum available, or set MAX_FD != -1 to use that value. -MAX_FD="maximum" +MAX_FD=maximum warn () { echo "$*" -} +} >&2 die () { echo echo "$*" echo exit 1 -} +} >&2 # OS specific support (must be 'true' or 'false'). cygwin=false msys=false darwin=false nonstop=false -case "`uname`" in - CYGWIN* ) - cygwin=true - ;; - Darwin* ) - darwin=true - ;; - MINGW* ) - msys=true - ;; - NONSTOP* ) - nonstop=true - ;; +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; esac CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar @@ -87,9 +121,9 @@ CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar if [ -n "$JAVA_HOME" ] ; then if [ -x "$JAVA_HOME/jre/sh/java" ] ; then # IBM's JDK on AIX uses strange locations for the executables - JAVACMD="$JAVA_HOME/jre/sh/java" + JAVACMD=$JAVA_HOME/jre/sh/java else - JAVACMD="$JAVA_HOME/bin/java" + JAVACMD=$JAVA_HOME/bin/java fi if [ ! -x "$JAVACMD" ] ; then die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME @@ -98,7 +132,7 @@ Please set the JAVA_HOME variable in your environment to match the location of your Java installation." fi else - JAVACMD="java" + JAVACMD=java which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. Please set the JAVA_HOME variable in your environment to match the @@ -106,80 +140,95 @@ location of your Java installation." fi # Increase the maximum file descriptors if we can. -if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then - MAX_FD_LIMIT=`ulimit -H -n` - if [ $? -eq 0 ] ; then - if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then - MAX_FD="$MAX_FD_LIMIT" - fi - ulimit -n $MAX_FD - if [ $? -ne 0 ] ; then - warn "Could not set maximum file descriptor limit: $MAX_FD" - fi - else - warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" - fi +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac fi -# For Darwin, add options to specify how the application appears in the dock -if $darwin; then - GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" -fi +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. # For Cygwin or MSYS, switch paths to Windows format before running java -if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then - APP_HOME=`cygpath --path --mixed "$APP_HOME"` - CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` - - JAVACMD=`cygpath --unix "$JAVACMD"` - - # We build the pattern for arguments to be converted via cygpath - ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` - SEP="" - for dir in $ROOTDIRSRAW ; do - ROOTDIRS="$ROOTDIRS$SEP$dir" - SEP="|" - done - OURCYGPATTERN="(^($ROOTDIRS))" - # Add a user-defined pattern to the cygpath arguments - if [ "$GRADLE_CYGPATTERN" != "" ] ; then - OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" - fi +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + # Now convert the arguments - kludge to limit ourselves to /bin/sh - i=0 - for arg in "$@" ; do - CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` - CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option - - if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition - eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` - else - eval `echo args$i`="\"$arg\"" + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) fi - i=`expr $i + 1` + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg done - case $i in - 0) set -- ;; - 1) set -- "$args0" ;; - 2) set -- "$args0" "$args1" ;; - 3) set -- "$args0" "$args1" "$args2" ;; - 4) set -- "$args0" "$args1" "$args2" "$args3" ;; - 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; - 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; - 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; - 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; - 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; - esac fi -# Escape application args -save () { - for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done - echo " " -} -APP_ARGS=`save "$@"` +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# -# Collect all arguments for the java command, following the shell quoting and substitution rules -eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat index a9f778a7a..ac1b06f93 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -40,7 +40,7 @@ if defined JAVA_HOME goto findJavaFromJavaHome set JAVA_EXE=java.exe %JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto init +if "%ERRORLEVEL%" == "0" goto execute echo. echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. @@ -54,7 +54,7 @@ goto fail set JAVA_HOME=%JAVA_HOME:"=% set JAVA_EXE=%JAVA_HOME%/bin/java.exe -if exist "%JAVA_EXE%" goto init +if exist "%JAVA_EXE%" goto execute echo. echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% @@ -64,21 +64,6 @@ echo location of your Java installation. goto fail -:init -@rem Get command-line arguments, handling Windows variants - -if not "%OS%" == "Windows_NT" goto win9xME_args - -:win9xME_args -@rem Slurp the command line arguments. -set CMD_LINE_ARGS= -set _SKIP=2 - -:win9xME_args_slurp -if "x%~1" == "x" goto execute - -set CMD_LINE_ARGS=%* - :execute @rem Setup the command line @@ -86,7 +71,7 @@ set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar @rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* :end @rem End local scope for the variables with windows NT shell diff --git a/rcv.iml b/rcv.iml deleted file mode 100644 index a5a744bb8..000000000 --- a/rcv.iml +++ /dev/null @@ -1,2 +0,0 @@ - - \ No newline at end of file diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index c733df485..015f2c777 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -10,7 +10,7 @@ requires javafx.fxml; requires javafx.graphics; requires java.xml; - requires poi.ooxml; + requires org.apache.poi.ooxml; // enable reflexive calls from network.brightspots.rcv into javafx.fxml opens network.brightspots.rcv; // our main module 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/GuiApplication.java b/src/main/java/network/brightspots/rcv/GuiApplication.java index 6d923ea4c..2d78e4edb 100644 --- a/src/main/java/network/brightspots/rcv/GuiApplication.java +++ b/src/main/java/network/brightspots/rcv/GuiApplication.java @@ -27,7 +27,7 @@ import javafx.stage.Stage; @SuppressWarnings("WeakerAccess") -public class GuiApplication extends Application { +class GuiApplication extends Application { private static final int STAGE_HEIGHT = 1000; private static final int STAGE_WIDTH = 1200; diff --git a/src/main/java/network/brightspots/rcv/GuiConfigController.java b/src/main/java/network/brightspots/rcv/GuiConfigController.java index 0ee955bea..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; @@ -72,6 +73,9 @@ import network.brightspots.rcv.Tabulator.TiebreakMode; import network.brightspots.rcv.Tabulator.WinnerElectionMode; +/** + * View controller for config layout. + */ @SuppressWarnings({"WeakerAccess"}) public class GuiConfigController implements Initializable { @@ -268,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", @@ -997,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/GuiTiebreakerController.java b/src/main/java/network/brightspots/rcv/GuiTiebreakerController.java index b5c4b10b7..42affe55c 100644 --- a/src/main/java/network/brightspots/rcv/GuiTiebreakerController.java +++ b/src/main/java/network/brightspots/rcv/GuiTiebreakerController.java @@ -26,6 +26,9 @@ import javafx.scene.control.ListView; import javafx.stage.Stage; +/** + * View controller for tiebreaker layout. + */ @SuppressWarnings("WeakerAccess") public class GuiTiebreakerController { 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/Main.java b/src/main/java/network/brightspots/rcv/Main.java index 94691a717..7291fcf77 100644 --- a/src/main/java/network/brightspots/rcv/Main.java +++ b/src/main/java/network/brightspots/rcv/Main.java @@ -25,6 +25,9 @@ import java.util.ArrayList; import java.util.List; +/** + * Main entry point to the RCV tabulator program. + */ @SuppressWarnings("WeakerAccess") public class Main extends GuiApplication { diff --git a/src/main/java/network/brightspots/rcv/RawContestConfig.java b/src/main/java/network/brightspots/rcv/RawContestConfig.java index 57003fb23..ceddffd6f 100644 --- a/src/main/java/network/brightspots/rcv/RawContestConfig.java +++ b/src/main/java/network/brightspots/rcv/RawContestConfig.java @@ -27,6 +27,9 @@ import com.fasterxml.jackson.annotation.JsonInclude; import java.util.List; +/** + * Contest configuration that can be serialized and deserialized. + */ @SuppressWarnings("WeakerAccess") @JsonIgnoreProperties(ignoreUnknown = true) public class RawContestConfig { @@ -40,7 +43,9 @@ public class RawContestConfig { RawContestConfig() { } - // OutputSettings: encapsulates the output settings + /** + * Output settings that can be serialized and deserialized. + */ @JsonIgnoreProperties(ignoreUnknown = true) @JsonInclude(JsonInclude.Include.NON_NULL) public static class OutputSettings { @@ -54,8 +59,11 @@ public static class OutputSettings { public boolean generateCdfJson; } - // CvrSource: encapsulates a source cast vote record file - // all indexes are 1-based + /** + * Source cast vote record file that can be serialized and deserialized. + * + *

All indexes are 1-based.

+ */ @JsonIgnoreProperties(ignoreUnknown = true) @JsonInclude(JsonInclude.Include.NON_NULL) public static class CvrSource { @@ -200,7 +208,9 @@ public void setTreatBlankAsUndeclaredWriteIn(boolean treatBlankAsUndeclaredWrite } } - // Candidate encapsulates data for each candidate in a contest + /** + * Contest candidate data that can be serialized and deserialized. + */ @JsonIgnoreProperties(ignoreUnknown = true) @JsonInclude(JsonInclude.Include.NON_NULL) public static class Candidate { @@ -243,7 +253,9 @@ public void setExcluded(boolean excluded) { } } - // ContestRules: encapsulates the set of rules required to perform contest tabulation + /** + * Contest rules necessary for tabulation that can be serialized and deserialized. + */ @SuppressWarnings({"unused", "RedundantSuppression"}) @JsonIgnoreProperties(ignoreUnknown = true) @JsonInclude(JsonInclude.Include.NON_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());