refacto: dry up the static and median default grade tallies

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

@ -3,7 +3,7 @@ package fr.mieuxvoter.mj;
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
* the median grade is considered a sane default.
*/

@ -2,10 +2,28 @@ package fr.mieuxvoter.mj;
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;
public TallyWithDefaultGrade(TallyInterface tally, Integer defaultGrade) {
super(tally.getProposalsTallies(), tally.getAmountOfJudges());
this.defaultGrade = defaultGrade;
fillWithDefaultGrade();
}
public TallyWithDefaultGrade(ProposalTallyInterface[] proposalsTallies, BigInteger amountOfJudges, Integer defaultGrade) {
super(proposalsTallies, amountOfJudges);
this.defaultGrade = defaultGrade;
@ -24,18 +42,9 @@ public class TallyWithDefaultGrade extends Tally implements TallyInterface {
fillWithDefaultGrade();
}
protected void fillWithDefaultGrade() {
int amountOfProposals = getAmountOfProposals();
for (int i = 0 ; i < amountOfProposals ; i++) {
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);
}
}
@Override
protected Integer getDefaultGrade(ProposalTallyInterface proposalTally) {
return this.defaultGrade;
}
}

Loading…
Cancel
Save