refacto: dry things up in the default grade tallies

pull/12/head
Dominique Merle 3 years ago
parent 86d893f53f
commit f4420fe15e

@ -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);
}

@ -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();
}
}

Loading…
Cancel
Save