Browse Source

feat: allow specifying a static default grade

pull/4/head
Dominique Merle 1 year ago
parent
commit
b8739d61f7
  1. 11
      src/main/java/fr/mieuxvoter/mj/ProposalTally.java
  2. 13
      src/main/java/fr/mieuxvoter/mj/ProposalTallyInterface.java
  3. 35
      src/main/java/fr/mieuxvoter/mj/TallyWithDefaultGrade.java
  4. 17
      src/test/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberatorTest.java

11
src/main/java/fr/mieuxvoter/mj/ProposalTally.java

@ -1,5 +1,6 @@
package fr.mieuxvoter.mj;
import java.math.BigInteger;
import java.util.Arrays;
public class ProposalTally implements ProposalTallyInterface {
@ -42,4 +43,14 @@ public class ProposalTally implements ProposalTallyInterface {
this.tally[fromGrade] = 0L;
}
@Override
public BigInteger getAmountOfJudgments() {
BigInteger sum = BigInteger.valueOf(0);
int tallyLength = this.tally.length;
for (int i = 0 ; i < tallyLength ; i++) {
sum = sum.add(BigInteger.valueOf(this.tally[i]));
}
return sum;
}
}

13
src/main/java/fr/mieuxvoter/mj/ProposalTallyInterface.java

@ -1,12 +1,23 @@
package fr.mieuxvoter.mj;
import java.math.BigInteger;
public interface ProposalTallyInterface {
/**
* The amount of judgments received for each Grade, from "worst" Grade to "best" Grade.
* The tallies of each Grade, that is
* the amount of judgments received for each Grade by the Proposal,
* from "worst" ("most conservative") Grade to "best" Grade.
*/
public Long[] getTally();
/**
* Should be the sum of getTally()
*
* @return The total amount of judgments received by this proposal.
*/
public BigInteger getAmountOfJudgments();
/**
* Homemade factory to skip the clone() shenanigans.
* Used by the score calculus.

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

@ -0,0 +1,35 @@
package fr.mieuxvoter.mj;
import java.math.BigInteger;
public class TallyWithDefaultGrade extends Tally implements TallyInterface {
protected Integer defaultGrade = 0;
public TallyWithDefaultGrade(ProposalTallyInterface[] proposalsTallies, Integer amountOfJudges, Integer defaultGrade) {
super(proposalsTallies, amountOfJudges);
this.defaultGrade = defaultGrade;
fillWithDefaultGrade();
}
public TallyWithDefaultGrade(ProposalTallyInterface[] proposalsTallies, Long 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 = BigInteger.valueOf(this.amountOfJudges).subtract(amountOfJudgments);
int missingSign = missingAmount.compareTo(BigInteger.ZERO);
assert(0 <= missingSign); // More judgments than judges!
if (0 < missingSign) {
proposal.getTally()[this.defaultGrade] = proposal.getTally()[this.defaultGrade] + missingAmount.longValue();
}
}
}
}

17
src/test/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberatorTest.java

@ -21,6 +21,7 @@ class MajorityJudgmentDeliberatorTest {
new ProposalTally(new Integer[]{4, 5, 2, 1, 3, 1, 2}),
new ProposalTally(new Integer[]{3, 6, 2, 1, 3, 1, 2}),
}, 18L);
ResultInterface result = mj.deliberate(tally);
assertNotNull(result);
@ -82,6 +83,22 @@ class MajorityJudgmentDeliberatorTest {
}
}
@Test
public void testWithStaticDefaultGrade() {
DeliberatorInterface mj = new MajorityJudgmentDeliberator();
TallyInterface tally = new TallyWithDefaultGrade(new ProposalTallyInterface[] {
new ProposalTally(new Integer[]{ 0, 0, 1 }),
new ProposalTally(new Integer[]{ 0, 3, 0 }),
}, 3L, 0);
ResultInterface result = mj.deliberate(tally);
assertNotNull(result);
assertEquals(2, result.getProposalResults().length);
assertEquals(2, result.getProposalResults()[0].getRank());
assertEquals(1, result.getProposalResults()[1].getRank());
}
// @Test
// public void runBenchmarks() throws Exception {
// Options options = new OptionsBuilder()

Loading…
Cancel
Save