Skip to content

Commit

Permalink
General cleanup; remove warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
vteague committed May 15, 2024
1 parent 727ba93 commit ff162bd
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ the raire assertion generation engine (https://github.com/DemocracyDevelopers/ra
import au.org.democracydevelopers.raireservice.request.GetAssertionsRequest;
import au.org.democracydevelopers.raireservice.request.RequestValidationException;
import au.org.democracydevelopers.raireservice.response.GenerateAssertionsResponse;
import au.org.democracydevelopers.raireservice.service.GetAssertionsCSVService;
import au.org.democracydevelopers.raireservice.service.GetAssertionsCsvService;
import au.org.democracydevelopers.raireservice.service.RaireServiceException;
import au.org.democracydevelopers.raireservice.service.GenerateAssertionsService;
import au.org.democracydevelopers.raireservice.service.GetAssertionsService;
import au.org.democracydevelopers.raireservice.service.GetAssertionsJsonService;
import au.org.democracydevelopers.raireservice.service.RaireServiceException.RaireErrorCodes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -64,8 +64,8 @@ public class AssertionController {

private final GenerateAssertionsService generateAssertionsService;

private final GetAssertionsService getAssertionsService;
private final GetAssertionsCSVService getAssertionsCSVService;
private final GetAssertionsJsonService getAssertionsService;
private final GetAssertionsCsvService getAssertionsCSVService;

/**
* The API endpoint for generating assertions, by contest name, and returning the IRV winner as
Expand Down Expand Up @@ -179,7 +179,7 @@ public ResponseEntity<String> serveCSV(@RequestBody GetAssertionsRequest request
*/
public AssertionController(ContestRepository contestRepository,
GenerateAssertionsService generateAssertionsService,
GetAssertionsService getAssertionsService, GetAssertionsCSVService getAssertionsCSVService) {
GetAssertionsJsonService getAssertionsService, GetAssertionsCsvService getAssertionsCSVService) {
this.contestRepository = contestRepository;
this.generateAssertionsService = generateAssertionsService;
this.getAssertionsService = getAssertionsService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ the raire assertion generation engine (https://github.com/DemocracyDevelopers/ra

package au.org.democracydevelopers.raireservice.persistence.entity;

import static au.org.democracydevelopers.raireservice.persistence.converters.CSVUtils.escapeThenJoin;
import static au.org.democracydevelopers.raireservice.util.CSVUtils.escapeThenJoin;

import au.org.democracydevelopers.raire.assertions.AssertionAndDifficulty;
import au.org.democracydevelopers.raire.assertions.AssertionAndDifficulty;
import java.math.BigDecimal;
import java.text.DecimalFormat;
Expand All @@ -31,7 +30,6 @@ the raire assertion generation engine (https://github.com/DemocracyDevelopers/ra
import java.util.List;
import jakarta.persistence.*;
import java.util.Map;
import org.apache.commons.text.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.annotation.ReadOnlyProperty;
Expand Down Expand Up @@ -194,13 +192,6 @@ public abstract class Assertion {
@ReadOnlyProperty
protected BigDecimal currentRisk = new BigDecimal("1.00");

/**
* Get the difficulty. Used only for testing.
*/
public double getDifficulty() {
return difficulty;
}

/**
* Default no-args constructor (required for persistence).
*/
Expand Down Expand Up @@ -285,11 +276,11 @@ public Integer getMargin() {
}

/**
* Get the list of candidates who are assumed continuing. Used for CSV output.
* @return the list of assumed-continuing candidates, by name.
* Get the difficulty. Used for CSV output.
* @return the difficulty.
*/
public List<String> getAssumedContinuing() {
return assumedContinuing;
public double getDifficulty() {
return difficulty;
}

/**
Expand All @@ -316,46 +307,6 @@ public int getOptimisticSamplesToAudit() {
return optimisticSamplesToAudit;
}

/**
* Get the two vote undercount. Used for CSV output.
* @return the two vote undercount.
*/
public int getTwoVoteUnderCount() {
return twoVoteUnderCount;
}

/**
* Get the one vote undercount. Used for CSV output.
* @return the one vote undercount.
*/
public int getOneVoteUnderCount() {
return oneVoteUnderCount;
}

/**
* Get the one vote overcount. Used for CSV output.
* @return the one vote overcount.
*/
public int getOneVoteOverCount() {
return oneVoteOverCount;
}

/**
* Get the two vote overcount. Used for CSV output.
* @return the two vote overcount.
*/
public int getTwoVoteOverCount() {
return twoVoteOverCount;
}

/**
* Get the count of discrepancies that are neither overcounts nor undercounts. Used for CSV output.
* @return the count of non-impactful discrepancies.
*/
public int getOtherCount() {
return otherCount;
}

/**
* Get the current risk calculation. Used for CSV output.
* @return the risk.
Expand Down Expand Up @@ -389,7 +340,7 @@ public abstract AssertionAndDifficulty convert(List<String> candidates)
public List<String> asCSVRow() {
var fm = new DecimalFormat("0.0###");
return List.of(
printAssertionType(),
gettAssertionType(),
winner,
loser,
escapeThenJoin(assumedContinuing),
Expand All @@ -410,5 +361,5 @@ public List<String> asCSVRow() {
/**
* Print the assertion type, either NEN or NEB.
*/
abstract String printAssertionType();
abstract String gettAssertionType();
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ the raire assertion generation engine (https://github.com/DemocracyDevelopers/ra
* a higher tally than a candidate _loser_. What this means is that the minimum possible tally
* that _winner_ will have at any stage of tabulation is greater than the maximum possible
* tally _loser_ can ever achieve. For more detail on NEB assertions, refer to the Guide to RAIRE.
*
* The constructor for this class takes a raire-java NEB assertion construct (NotEliminatedBefore)
* and translates it into a NEBAssertion entity, suitable for storage in the corla database.
*/
Expand Down Expand Up @@ -100,7 +99,7 @@ public AssertionAndDifficulty convert(List<String> candidates) throws IllegalArg
* @return The string "NEB"
*/
@Override
public String printAssertionType() {
public String gettAssertionType() {
return "NEB";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ the raire assertion generation engine (https://github.com/DemocracyDevelopers/ra
/**
* A Not Eliminated Next assertion asserts that a _winner_ beats a _loser_ in an audit when all
* candidates other that those in a specified _assumed to be continuing_ list have been removed.
*
* In particular, this means that _winner_ can not be the next candidate eliminated.
*
* This assertion type is also referred to as an NEN assertion in A Guide to RAIRE.
*
* The constructor for this class takes a raire-java NEN assertion construct (NotEliminatedNext)
* and translates it into a NENAssertion entity, suitable for storage in the corla database.
*/
Expand Down Expand Up @@ -112,7 +109,7 @@ public AssertionAndDifficulty convert(List<String> candidates) throws IllegalArg
* @return The string "NEN"
*/
@Override
public String printAssertionType() {
public String gettAssertionType() {
return "NEN";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,13 @@ the raire assertion generation engine (https://github.com/DemocracyDevelopers/ra
import java.beans.ConstructorProperties;

/**
* The success response to a GenerateAssertionsRequest.
* This simply returns the winner, as calculated by raire, along with the
* name of the contest for which the initial request was made.
* The success response to a GenerateAssertionsRequest. This simply returns the winner, as
* calculated by raire, along with the name of the contest for which the initial request was made.
*
* @param contestName The name of the contest.
* @param winner The winner of the contest, as calculated by raire.
*/
public class GenerateAssertionsResponse {

/**
* The name of the contest.
*/
public final String contestName;

/**
* The winner of the contest, as calculated by raire.
*/
public final String winner;
public record GenerateAssertionsResponse(String contestName, String winner) {

/**
* All args constructor.
Expand All @@ -46,8 +38,6 @@ public class GenerateAssertionsResponse {
* @param winner the name of the winner.
*/
@ConstructorProperties({"contestName", "winner"})
public GenerateAssertionsResponse(String contestName, String winner) {
this.contestName = contestName;
this.winner = winner;
public GenerateAssertionsResponse {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ the raire assertion generation engine (https://github.com/DemocracyDevelopers/ra
import static au.org.democracydevelopers.raireservice.service.Metadata.OPTIMISTIC_SAMPLES;
import static au.org.democracydevelopers.raireservice.service.Metadata.extremumHeaders;
import static au.org.democracydevelopers.raireservice.service.Metadata.csvHeaders;
import static au.org.democracydevelopers.raireservice.persistence.converters.CSVUtils.escapeThenJoin;
import static au.org.democracydevelopers.raireservice.util.CSVUtils.escapeThenJoin;

import au.org.democracydevelopers.raireservice.persistence.entity.Assertion;
import au.org.democracydevelopers.raireservice.persistence.repository.AssertionRepository;
Expand All @@ -52,17 +52,17 @@ the raire assertion generation engine (https://github.com/DemocracyDevelopers/ra
* the /get-assertions-csv endpoint.
*/
@Service
public class GetAssertionsCSVService {
public class GetAssertionsCsvService {

private final static Logger logger = LoggerFactory.getLogger(GetAssertionsService.class);
private final static Logger logger = LoggerFactory.getLogger(GetAssertionsJsonService.class);

private final AssertionRepository assertionRepository;

/**
* All args constructor.
* @param assertionRepository the assertion repository.
*/
public GetAssertionsCSVService(AssertionRepository assertionRepository) {
public GetAssertionsCsvService(AssertionRepository assertionRepository) {
this.assertionRepository = assertionRepository;
}

Expand Down Expand Up @@ -102,6 +102,8 @@ public String generateCSV(GetAssertionsRequest request) throws RaireServiceExcep

return preface + extrema + "\n\n" + headers + "\n" + contents;

} catch(RaireServiceException ex) {
throw ex;
} catch (Exception e) {
String msg = "Error retrieving assertions for the contest " + request.contestName;
logger.error("GetAssertionsCSVService::generateCSV" + msg);
Expand Down Expand Up @@ -204,35 +206,35 @@ private String findExtrema(List<Assertion> sortedAssertions) {
List<String> csvRows = new ArrayList<>();
DoubleComparator doubleComparator = new DoubleComparator();

extremumResult<Integer> marginResult = findExtremum(sortedAssertions,
// Minimum margin.
csvRows.add(findExtremum(sortedAssertions,
MARGIN, extremumType.MIN, Assertion::getMargin, Integer::compare
);
csvRows.add(marginResult.toCSVRow());
).toCSVRow());

extremumResult<Double> dilutedMarginResult = findExtremum(sortedAssertions,
// Minimum diluted margin.
csvRows.add(findExtremum(sortedAssertions,
DILUTED_MARGIN, extremumType.MIN, Assertion::getDilutedMargin, doubleComparator
);
csvRows.add(dilutedMarginResult.toCSVRow());
).toCSVRow());

extremumResult<Double> difficultyResult = findExtremum(sortedAssertions,
// Maximum difficulty.
csvRows.add(findExtremum(sortedAssertions,
DIFFICULTY, extremumType.MAX, Assertion::getDifficulty, doubleComparator
);
csvRows.add(difficultyResult.toCSVRow());
).toCSVRow());

extremumResult<BigDecimal> currentRiskResult = findExtremum(sortedAssertions,
// Maximum current risk.
csvRows.add(findExtremum(sortedAssertions,
CURRENT_RISK, extremumType.MAX, Assertion::getCurrentRisk, BigDecimal::compareTo
);
csvRows.add(currentRiskResult.toCSVRow());
).toCSVRow());

extremumResult<Integer> optimisticSamplesResult = findExtremum(sortedAssertions,
// Maximum optimistic samples to audit.
csvRows.add(findExtremum(sortedAssertions,
OPTIMISTIC_SAMPLES, extremumType.MAX, Assertion::getOptimisticSamplesToAudit, Integer::compare
);
csvRows.add(optimisticSamplesResult.toCSVRow());
).toCSVRow());

extremumResult<Integer> estimatedSamplesResult = findExtremum(sortedAssertions,
// Maximum estimated samples to audit.
csvRows.add(findExtremum(sortedAssertions,
ESTIMATED_SAMPLES, extremumType.MAX, Assertion::getEstimatedSamplesToAudit, Integer::compare
);
csvRows.add(estimatedSamplesResult.toCSVRow());
).toCSVRow());

return String.join("\n", csvRows);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ the raire assertion generation engine (https://github.com/DemocracyDevelopers/ra
* Assertions are retrieved for a contest as specified in a GetAssertionsRequest.
*/
@Service
public class GetAssertionsService {
public class GetAssertionsJsonService {

private final static Logger logger = LoggerFactory.getLogger(GetAssertionsService.class);
private final static Logger logger = LoggerFactory.getLogger(GetAssertionsJsonService.class);

private final AssertionRepository assertionRepository;

/**
* All-args constructor.
* @param assertionRepository Repository for retrieval of assertions from the colorado-rla database.
*/
public GetAssertionsService(AssertionRepository assertionRepository){
public GetAssertionsJsonService(AssertionRepository assertionRepository){
this.assertionRepository = assertionRepository;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,6 @@ public class Metadata {
*/
public final static String ESTIMATED_SAMPLES = "Estimated samples to audit";

/**
* List of statistics we find the minimum or maximum of.
*/
public final static List<String> statisticNames = List.of(MARGIN, DILUTED_MARGIN,
DIFFICULTY, CURRENT_RISK, OPTIMISTIC_SAMPLES, ESTIMATED_SAMPLES);


// Other headers used in parts of the csv
public final static String CONTEST_NAME_HEADER = "Contest name";
public final static String CANDIDATES_HEADER = "Candidates";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ the raire assertion generation engine (https://github.com/DemocracyDevelopers/ra
raire-service. If not, see <https://www.gnu.org/licenses/>.
*/

package au.org.democracydevelopers.raireservice.persistence.converters;
package au.org.democracydevelopers.raireservice.util;

import java.util.List;
import org.apache.commons.text.StringEscapeUtils;

/**
* Functions for doing basic string operations useful for turning some data into a row of a csv file.
* (csv) escape, then join, is used by the get-assertions-csv endpoint.
*
*/
public class CSVUtils {

/**
* Take a list of strings and format them as a csv row, escaping each individually and then
* joining them all with a comma.
Expand All @@ -34,16 +40,4 @@ public class CSVUtils {
public static String escapeThenJoin(List<String> data) {
return String.join(", ", data.stream().map(StringEscapeUtils::escapeCsv).toList());
}

/**
* Take a list of strings and format them as a csv _cell_, escaping each individually and then
* joining them all with a comma, then csv-escaping them so that they're interpeted as a single
* entry.
* @param data The list of strings to be joined
* @return The same data, as an appropriately-escaped csv _cell_, with each element of the input
* list separated by a comma.
*/
public static String escapeThenJoinThenEscape(List<String> data) {
return StringEscapeUtils.escapeCsv(escapeThenJoin(data));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class GetAssertionsAPIErrorTests {
private final static HttpHeaders httpHeaders = new HttpHeaders();

private final static String baseURL = "http://localhost:";
private final static String getAssertionsEndpoint = "/raire/get-assertions";
private final static String getAssertionsEndpoint = "/raire/get-assertions-json";

@LocalServerPort
private int port;
Expand Down
Loading

0 comments on commit ff162bd

Please sign in to comment.