Browse Source

refacto: dry up the static and median default grade tallies

feat-median-default-grade
Dominique Merle 1 year ago
parent
commit
093ae22354
  1. 2
      src/main/java/fr/mieuxvoter/mj/DefaultGradeTally.java
  2. 2
      src/main/java/fr/mieuxvoter/mj/MedianDefaultTally.java
  3. 39
      src/main/java/fr/mieuxvoter/mj/TallyWithDefaultGrade.java

2
src/main/java/fr/mieuxvoter/mj/DefaultGradeTally.java

@ -7,7 +7,7 @@ import java.math.BigInteger;
* 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());
}

2
src/main/java/fr/mieuxvoter/mj/MedianDefaultTally.java

@ -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.
*/

39
src/main/java/fr/mieuxvoter/mj/TallyWithDefaultGrade.java

@ -2,9 +2,27 @@ 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);
@ -17,25 +35,16 @@ public class TallyWithDefaultGrade extends Tally implements TallyInterface {
this.defaultGrade = defaultGrade;
fillWithDefaultGrade();
}
public TallyWithDefaultGrade(ProposalTallyInterface[] proposalsTallies, Integer amountOfJudges, Integer defaultGrade) {
super(proposalsTallies, amountOfJudges);
this.defaultGrade = defaultGrade;
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