From f4420fe15eff8c88bc80d7de85b28e2bbada0f9e Mon Sep 17 00:00:00 2001 From: domi41 Date: Sun, 16 May 2021 05:34:03 +0200 Subject: [PATCH] refacto: dry things up in the default grade tallies --- .../fr/mieuxvoter/mj/DefaultGradeTally.java | 44 +++++++++++++++++++ .../fr/mieuxvoter/mj/MedianDefaultTally.java | 25 +++++------ 2 files changed, 54 insertions(+), 15 deletions(-) create mode 100644 src/main/java/fr/mieuxvoter/mj/DefaultGradeTally.java diff --git a/src/main/java/fr/mieuxvoter/mj/DefaultGradeTally.java b/src/main/java/fr/mieuxvoter/mj/DefaultGradeTally.java new file mode 100644 index 0000000..882d7d3 --- /dev/null +++ b/src/main/java/fr/mieuxvoter/mj/DefaultGradeTally.java @@ -0,0 +1,44 @@ +package fr.mieuxvoter.mj; + +import java.math.BigInteger; + +/** + * Fill the missing judgments into the grade defined by `getDefaultGrade()`. + * This is an abstract class to dry code between static default grade and median default grade. + */ +abstract public class DefaultGradeTally extends Tally implements TallyInterface { + + public DefaultGradeTally(TallyInterface tally) { + super(tally.getProposalsTallies(), tally.getAmountOfJudges()); + } + + public DefaultGradeTally(ProposalTallyInterface[] proposalsTallies, Integer amountOfJudges) { + super(proposalsTallies, amountOfJudges); + } + + public DefaultGradeTally(ProposalTallyInterface[] proposalsTallies, Long amountOfJudges) { + super(proposalsTallies, amountOfJudges); + } + + public DefaultGradeTally(ProposalTallyInterface[] proposalsTallies, BigInteger amountOfJudges) { + super(proposalsTallies, amountOfJudges); + } + + protected void fillWithDefaultGrade() { + int amountOfProposals = getAmountOfProposals(); + for (int i = 0 ; i < amountOfProposals ; i++) { + ProposalTallyInterface proposal = getProposalsTallies()[i]; + Integer defaultGrade = getDefaultGrade(proposal); + BigInteger amountOfJudgments = proposal.getAmountOfJudgments(); + BigInteger missingAmount = this.amountOfJudges.subtract(amountOfJudgments); + int missingSign = missingAmount.compareTo(BigInteger.ZERO); + assert(0 <= missingSign); // ERROR: More judgments than judges! + if (0 < missingSign) { + proposal.getTally()[defaultGrade] = proposal.getTally()[defaultGrade].add(missingAmount); + } + } + } + + abstract protected Integer getDefaultGrade(ProposalTallyInterface proposalTally); + +} diff --git a/src/main/java/fr/mieuxvoter/mj/MedianDefaultTally.java b/src/main/java/fr/mieuxvoter/mj/MedianDefaultTally.java index 4a665f1..8ec118c 100644 --- a/src/main/java/fr/mieuxvoter/mj/MedianDefaultTally.java +++ b/src/main/java/fr/mieuxvoter/mj/MedianDefaultTally.java @@ -7,7 +7,12 @@ import java.math.BigInteger; * Useful when the proposals have not received the exact same amount of votes and * the median grade is considered a sane default. */ -public class MedianDefaultTally extends Tally implements TallyInterface { +public class MedianDefaultTally extends DefaultGradeTally implements TallyInterface { + + public MedianDefaultTally(TallyInterface tally) { + super(tally.getProposalsTallies(), tally.getAmountOfJudges()); + fillWithDefaultGrade(); + } public MedianDefaultTally(ProposalTallyInterface[] proposalsTallies, BigInteger amountOfJudges) { super(proposalsTallies, amountOfJudges); @@ -24,20 +29,10 @@ public class MedianDefaultTally extends Tally implements TallyInterface { fillWithDefaultGrade(); } - protected void fillWithDefaultGrade() { - int amountOfProposals = getAmountOfProposals(); - for (int i = 0 ; i < amountOfProposals ; i++) { - ProposalTallyInterface proposal = getProposalsTallies()[i]; - ProposalTallyAnalysis analysis = new ProposalTallyAnalysis(proposal); - Integer defaultGrade = analysis.getMedianGrade(); - BigInteger amountOfJudgments = proposal.getAmountOfJudgments(); - BigInteger missingAmount = this.amountOfJudges.subtract(amountOfJudgments); - int missingSign = missingAmount.compareTo(BigInteger.ZERO); - assert(0 <= missingSign); // ERROR: More judgments than judges! - if (0 < missingSign) { - proposal.getTally()[defaultGrade] = proposal.getTally()[defaultGrade].add(missingAmount); - } - } + @Override + protected Integer getDefaultGrade(ProposalTallyInterface proposalTally) { + ProposalTallyAnalysis analysis = new ProposalTallyAnalysis(proposalTally); + return analysis.getMedianGrade(); } }