diff --git a/src/main/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberator.java b/src/main/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberator.java index fb063cf..6092105 100644 --- a/src/main/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberator.java +++ b/src/main/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberator.java @@ -18,9 +18,25 @@ import java.util.Comparator; * * https://en.wikipedia.org/wiki/Majority_judgment * https://fr.wikipedia.org/wiki/Jugement_majoritaire + * + * Should this class be `final`? */ final public class MajorityJudgmentDeliberator implements DeliberatorInterface { + protected boolean favorContestation = true; + protected boolean numerizeScore = false; + + public MajorityJudgmentDeliberator() {} + + public MajorityJudgmentDeliberator(boolean favorContestation) { + this.favorContestation = favorContestation; + } + + public MajorityJudgmentDeliberator(boolean favorContestation, boolean numerizeScore) { + this.favorContestation = favorContestation; + this.numerizeScore = numerizeScore; + } + @Override public ResultInterface deliberate(TallyInterface tally) { ProposalTallyInterface[] tallies = tally.getProposalsTallies(); @@ -34,8 +50,7 @@ final public class MajorityJudgmentDeliberator implements DeliberatorInterface { for (int proposalIndex = 0; proposalIndex < amountOfProposals; proposalIndex++) { ProposalTallyInterface proposalTally = tallies[proposalIndex]; String score = computeScore(proposalTally, amountOfJudges); - ProposalTallyAnalysis analysis = new ProposalTallyAnalysis(); - analysis.reanalyze(proposalTally); + ProposalTallyAnalysis analysis = new ProposalTallyAnalysis(proposalTally, this.favorContestation); ProposalResult proposalResult = new ProposalResult(); proposalResult.setScore(score); proposalResult.setAnalysis(analysis); @@ -73,7 +88,7 @@ final public class MajorityJudgmentDeliberator implements DeliberatorInterface { } protected String computeScore(ProposalTallyInterface tally, BigInteger amountOfJudges) { - return computeScore(tally, amountOfJudges, true, false); + return computeScore(tally, amountOfJudges, this.favorContestation, this.numerizeScore); } /** diff --git a/src/main/java/fr/mieuxvoter/mj/ProposalTallyAnalysis.java b/src/main/java/fr/mieuxvoter/mj/ProposalTallyAnalysis.java index b65ba61..967bf0f 100644 --- a/src/main/java/fr/mieuxvoter/mj/ProposalTallyAnalysis.java +++ b/src/main/java/fr/mieuxvoter/mj/ProposalTallyAnalysis.java @@ -43,10 +43,14 @@ public class ProposalTallyAnalysis { reanalyze(tally); } + public ProposalTallyAnalysis(ProposalTallyInterface tally, Boolean favorContestation) { + reanalyze(tally, favorContestation); + } + public void reanalyze(ProposalTallyInterface tally) { reanalyze(tally, true); } - + public void reanalyze(ProposalTallyInterface tally, Boolean favorContestation) { this.tally = tally; this.totalSize = BigInteger.ZERO; diff --git a/src/test/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberatorTest.java b/src/test/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberatorTest.java index 30ab250..473b6f0 100644 --- a/src/test/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberatorTest.java +++ b/src/test/java/fr/mieuxvoter/mj/MajorityJudgmentDeliberatorTest.java @@ -341,8 +341,34 @@ class MajorityJudgmentDeliberatorTest { } } + @Test + @DisplayName("Test favoring adhesion") + public void testFavoringAdhesion() { + boolean favorContestation = false; + Integer amountOfJudges = 4; + DeliberatorInterface mj = new MajorityJudgmentDeliberator(favorContestation); + TallyInterface tally = new Tally(new ProposalTallyInterface[] { + new ProposalTally(new Integer[]{ 2, 0, 2 }), + new ProposalTally(new Integer[]{ 0, 2, 2 }), + new ProposalTally(new Integer[]{ 2, 1, 1 }), + }, amountOfJudges); + + ResultInterface result = mj.deliberate(tally); + + assertNotNull(result); + assertEquals(3, result.getProposalResults().length); + assertEquals(2, result.getProposalResults()[0].getRank()); + assertEquals(1, result.getProposalResults()[1].getRank()); + assertEquals(3, result.getProposalResults()[2].getRank()); + assertEquals(2, result.getProposalResults()[0].getAnalysis().getMedianGrade()); + assertEquals(2, result.getProposalResults()[1].getAnalysis().getMedianGrade()); + assertEquals(1, result.getProposalResults()[2].getAnalysis().getMedianGrade()); + } + + // … + // @Test // public void runBenchmarks() throws Exception { // Options options = new OptionsBuilder()