diff --git a/src/main/java/fr/mieuxvoter/mj/ProposalTally.java b/src/main/java/fr/mieuxvoter/mj/ProposalTally.java index dc14602..2c4eedd 100644 --- a/src/main/java/fr/mieuxvoter/mj/ProposalTally.java +++ b/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; + } + } diff --git a/src/main/java/fr/mieuxvoter/mj/ProposalTallyInterface.java b/src/main/java/fr/mieuxvoter/mj/ProposalTallyInterface.java index 9f4b6c6..5907018 100644 --- a/src/main/java/fr/mieuxvoter/mj/ProposalTallyInterface.java +++ b/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. diff --git a/src/main/java/fr/mieuxvoter/mj/TallyWithDefaultGrade.java b/src/main/java/fr/mieuxvoter/mj/TallyWithDefaultGrade.java new file mode 100644 index 0000000..dea5891 --- /dev/null +++ b/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(); + } + } + } + +} diff --git a/src/test/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberatorTest.java b/src/test/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberatorTest.java index cac001f..bcb60d2 100644 --- a/src/test/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberatorTest.java +++ b/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()