diff --git a/README.md b/README.md index a879d74..c6c1cf8 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,10 @@ assert(1 == result.getProposalResults()[1].getRank()); // Proposal B Got more than 2³² judges? Use a `Long[]` in a `ProposalTally`. +Got even more than that ? Use `BigInteger`s ! + +Want to set a static default grade ? Use a `TallyWithDefaultGrade` instead of a `Tally`. + ## Roadmap @@ -55,9 +59,9 @@ Got more than 2³² judges? Use a `Long[]` in a `ProposalTally`. - [x] Release v0.1.0 - [ ] Guess the amount of judges - [ ] Allow defining a default grade - - [ ] Static Grade (configurable) + - [x] Static Grade (configurable) - [ ] Median Grade - - [ ] Normalization (using smallest common multiple) + - [ ] Normalization (using least common multiple) - [ ] Release v0.2.0 - [ ] Publish on package repositories - [ ] Gradle diff --git a/src/main/java/fr/mieuxvoter/mj/ProposalTally.java b/src/main/java/fr/mieuxvoter/mj/ProposalTally.java index 3ec19fe..a10f954 100644 --- a/src/main/java/fr/mieuxvoter/mj/ProposalTally.java +++ b/src/main/java/fr/mieuxvoter/mj/ProposalTally.java @@ -9,17 +9,33 @@ public class ProposalTally implements ProposalTallyInterface { // Should we allow this as well? //public ProposalTally() {} + + public ProposalTally(String[] tally) { + setTally(tally); + } public ProposalTally(Integer[] tally) { + setTally(tally); + } + + public ProposalTally(Long[] tally) { + setTally(tally); + } + + public ProposalTally(BigInteger[] tally) { + setTally(tally); + } + + public void setTally(String[] tally) { int tallyLength = tally.length; BigInteger[] bigTally = new BigInteger[tallyLength]; for (int i = 0 ; i < tallyLength ; i++) { - bigTally[i] = BigInteger.valueOf(tally[i]); + bigTally[i] = new BigInteger(tally[i]); } setTally(bigTally); } - - public ProposalTally(Long[] tally) { + + public void setTally(Integer[] tally) { int tallyLength = tally.length; BigInteger[] bigTally = new BigInteger[tallyLength]; for (int i = 0 ; i < tallyLength ; i++) { @@ -27,9 +43,14 @@ public class ProposalTally implements ProposalTallyInterface { } setTally(bigTally); } - - public ProposalTally(BigInteger[] tally) { - setTally(tally); + + public void setTally(Long[] tally) { + int tallyLength = tally.length; + BigInteger[] bigTally = new BigInteger[tallyLength]; + for (int i = 0 ; i < tallyLength ; i++) { + bigTally[i] = BigInteger.valueOf(tally[i]); + } + setTally(bigTally); } public void setTally(BigInteger[] tally) { @@ -48,7 +69,6 @@ public class ProposalTally implements ProposalTallyInterface { @Override public void moveJudgments(Integer fromGrade, Integer intoGrade) { -// this.tally[intoGrade] += this.tally[fromGrade]; this.tally[intoGrade] = this.tally[intoGrade].add(this.tally[fromGrade]); this.tally[fromGrade] = BigInteger.ZERO; } diff --git a/src/main/java/fr/mieuxvoter/mj/ProposalTallyAnalysis.java b/src/main/java/fr/mieuxvoter/mj/ProposalTallyAnalysis.java index 72625f3..c79a459 100644 --- a/src/main/java/fr/mieuxvoter/mj/ProposalTallyAnalysis.java +++ b/src/main/java/fr/mieuxvoter/mj/ProposalTallyAnalysis.java @@ -92,7 +92,6 @@ public class ProposalTallyAnalysis { this.medianGrade = grade; this.contestationGroupSize = tallyBeforeCursor; this.medianGroupSize = gradeTally; -// this.adhesionGroupSize = this.totalSize - this.contestationGroupSize - this.medianGroupSize; this.adhesionGroupSize = this.totalSize.subtract(this.contestationGroupSize).subtract(this.medianGroupSize); } else { if (1 == gradeTally.compareTo(BigInteger.ZERO)) { // 0 < gradeTally @@ -108,7 +107,6 @@ public class ProposalTallyAnalysis { this.contestationGrade = contestationGrade; this.adhesionGrade = adhesionGrade; -// this.secondMedianGroupSize = Math.max(this.contestationGroupSize, this.adhesionGroupSize); this.secondMedianGroupSize = this.contestationGroupSize.max(this.adhesionGroupSize); this.secondMedianGroupSign = 0; // if (this.contestationGroupSize < this.adhesionGroupSize) { diff --git a/src/test/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberatorTest.java b/src/test/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberatorTest.java index bcb60d2..14d9860 100644 --- a/src/test/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberatorTest.java +++ b/src/test/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberatorTest.java @@ -68,8 +68,16 @@ class MajorityJudgmentDeliberatorTest { } tallies[i] = new ProposalTally(tally); } + + String mode = datum.getString("mode", "None"); + TallyInterface tally; + if ("StaticDefault".equalsIgnoreCase(mode)) { + tally = new TallyWithDefaultGrade(tallies, amountOfParticipants, datum.getInt("default")); + } else { + tally = new Tally(tallies, amountOfParticipants); + } + DeliberatorInterface mj = new MajorityJudgmentDeliberator(); - TallyInterface tally = new Tally(tallies, amountOfParticipants); ResultInterface result = mj.deliberate(tally); assertNotNull(result); diff --git a/src/test/resources/assertions.json b/src/test/resources/assertions.json index 0b1ff15..7e8deb5 100644 --- a/src/test/resources/assertions.json +++ b/src/test/resources/assertions.json @@ -49,6 +49,46 @@ 1, 2 ] + }, + { + "title": "Static Default Grade", + "participants": 10, + "mode": "StaticDefault", + "default": 0, + "tallies": [ + [ 2, 2, 2, 2, 2 ], + [ 1, 2, 2, 2, 2 ], + [ 0, 0, 4, 0, 0 ], + [ 0, 0, 0, 0, 2 ], + [ 0, 0, 0, 1, 1 ] + ], + "ranks": [ + 1, + 1, + 3, + 4, + 5 + ] + }, + { + "title": "Static Default Grade to Passable", + "participants": 10, + "mode": "StaticDefault", + "default": 2, + "tallies": [ + [ 2, 2, 2, 2, 2 ], + [ 2, 2, 1, 2, 2 ], + [ 0, 0, 4, 0, 0 ], + [ 0, 0, 0, 0, 2 ], + [ 0, 0, 0, 1, 1 ] + ], + "ranks": [ + 4, + 4, + 3, + 1, + 2 + ] } ]