refacto: dry up the static and median default grade tallies

pull/12/head
Dominique Merle 3 years ago
parent f4420fe15e
commit 093ae22354

@ -7,7 +7,7 @@ import java.math.BigInteger;
* This is an abstract class to dry code between static default grade and median default grade. * This is an abstract class to dry code between static default grade and median default grade.
*/ */
abstract public class DefaultGradeTally extends Tally implements TallyInterface { abstract public class DefaultGradeTally extends Tally implements TallyInterface {
public DefaultGradeTally(TallyInterface tally) { public DefaultGradeTally(TallyInterface tally) {
super(tally.getProposalsTallies(), tally.getAmountOfJudges()); super(tally.getProposalsTallies(), tally.getAmountOfJudges());
} }

@ -3,7 +3,7 @@ package fr.mieuxvoter.mj;
import java.math.BigInteger; import java.math.BigInteger;
/** /**
* Fill the missing judgments into the median grade. * Fill the missing judgments into the median grade of each proposal.
* Useful when the proposals have not received the exact same amount of votes and * Useful when the proposals have not received the exact same amount of votes and
* the median grade is considered a sane default. * the median grade is considered a sane default.
*/ */

@ -2,9 +2,27 @@ package fr.mieuxvoter.mj;
import java.math.BigInteger; import java.math.BigInteger;
public class TallyWithDefaultGrade extends Tally implements TallyInterface { public class TallyWithDefaultGrade extends DefaultGradeTally implements TallyInterface {
/**
* Grades are represented as numbers, as indices in a list.
* Grades start from 0 ("worst" grade, most conservative) and go upwards.
* Values out of the range of grades defined in the tally will yield errors.
*
* Example:
*
* 0 == REJECT
* 1 == PASSABLE
* 2 == GOOD
* 3 == EXCELLENT
*/
protected Integer defaultGrade = 0; protected Integer defaultGrade = 0;
public TallyWithDefaultGrade(TallyInterface tally, Integer defaultGrade) {
super(tally.getProposalsTallies(), tally.getAmountOfJudges());
this.defaultGrade = defaultGrade;
fillWithDefaultGrade();
}
public TallyWithDefaultGrade(ProposalTallyInterface[] proposalsTallies, BigInteger amountOfJudges, Integer defaultGrade) { public TallyWithDefaultGrade(ProposalTallyInterface[] proposalsTallies, BigInteger amountOfJudges, Integer defaultGrade) {
super(proposalsTallies, amountOfJudges); super(proposalsTallies, amountOfJudges);
@ -17,25 +35,16 @@ public class TallyWithDefaultGrade extends Tally implements TallyInterface {
this.defaultGrade = defaultGrade; this.defaultGrade = defaultGrade;
fillWithDefaultGrade(); fillWithDefaultGrade();
} }
public TallyWithDefaultGrade(ProposalTallyInterface[] proposalsTallies, Integer amountOfJudges, Integer defaultGrade) { public TallyWithDefaultGrade(ProposalTallyInterface[] proposalsTallies, Integer amountOfJudges, Integer defaultGrade) {
super(proposalsTallies, amountOfJudges); super(proposalsTallies, amountOfJudges);
this.defaultGrade = defaultGrade; this.defaultGrade = defaultGrade;
fillWithDefaultGrade(); fillWithDefaultGrade();
} }
protected void fillWithDefaultGrade() { @Override
int amountOfProposals = getAmountOfProposals(); protected Integer getDefaultGrade(ProposalTallyInterface proposalTally) {
for (int i = 0 ; i < amountOfProposals ; i++) { return this.defaultGrade;
ProposalTallyInterface proposal = getProposalsTallies()[i];
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()[this.defaultGrade] = proposal.getTally()[this.defaultGrade].add(missingAmount);
}
}
} }
} }

Loading…
Cancel
Save