branch develop updated (50e1076 -> 30c80cc)
This is an automated email from the git hooks/post-receive script. New change to branch develop in repository pollen. See https://gitlab.nuiton.org/chorem/pollen.git from 50e1076 correction de l'édition des dates dans le sondage new 30c80cc dépouillement des sondages restreints The 1 revisions listed above as "new" are entirely new to this repository and will be described in separate emails. The revisions listed as "adds" were already present in the repository and have only been added to this reference. Detailed log of new commits: commit 30c80cc37252bf35ad51b8a2bc9729c4dc9fde69 Author: Sylvain Bavencoff <bavencoff@codelutin.com> Date: Thu May 18 17:55:32 2017 +0200 dépouillement des sondages restreints Summary of changes: .../chorem/pollen/rest/api/v1/VoteCountingApi.java | 4 +- ...ltBean.java => ListVoteCountingResultBean.java} | 18 +- .../services/service/VoteCountingService.java | 104 +++++---- .../votecounting/AbstractVoteCountingStrategy.java | 30 +-- .../pollen/votecounting/VoteCountingStrategy.java | 16 +- .../pollen/votecounting/model/ChoiceScore.java | 30 +++ .../votecounting/model/GroupOfVoterBuilder.java | 90 -------- .../model/{GroupOfVoter.java => ListOfVoter.java} | 34 +-- ...tingResult.java => ListVoteCountingResult.java} | 28 +-- pollen-votecounting-borda/pom.xml | 5 + .../votecounting/BordaVoteCountingStrategy.java | 17 ++ .../BordaVoteCountingStrategyTest.java | 189 +++++++++------- pollen-votecounting-condorcet/pom.xml | 5 + .../CondorcetVoteCountingStrategy.java | 16 ++ .../CondorcetVoteCountingStrategyTest.java | 192 +++++++++------- pollen-votecounting-coombs/pom.xml | 5 + .../votecounting/CoombsVoteCountingStrategy.java | 16 ++ .../CoombsVoteCountingStrategyTest.java | 127 +++++------ pollen-votecounting-instant-runoff/pom.xml | 5 + .../InstantRunoffVoteCountingStrategy.java | 16 ++ .../InstantRunoffVoteCountingStrategyTest.java | 136 +++++------- pollen-votecounting-normal/pom.xml | 6 + .../votecounting/NormalVoteCountingStrategy.java | 18 ++ .../NormalVoteCountingStrategyTest.java | 241 +++++++++------------ pollen-votecounting-number/pom.xml | 5 + .../votecounting/NumberVoteCountingStrategy.java | 16 ++ .../NumberVoteCountingStrategyTest.java | 173 +++++++++------ pollen-votecounting-percentage/pom.xml | 5 + .../PercentageVoteCountingStrategy.java | 28 +++ .../PercentageVoteCountingStrategyTest.java | 191 +++++++++------- pom.xml | 6 + 31 files changed, 979 insertions(+), 793 deletions(-) rename pollen-services/src/main/java/org/chorem/pollen/services/bean/{GroupVoteCountingResultBean.java => ListVoteCountingResultBean.java} (72%) delete mode 100644 pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/GroupOfVoterBuilder.java rename pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/{GroupOfVoter.java => ListOfVoter.java} (75%) rename pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/{GroupVoteCountingResult.java => ListVoteCountingResult.java} (64%) -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository pollen. See https://gitlab.nuiton.org/chorem/pollen.git commit 30c80cc37252bf35ad51b8a2bc9729c4dc9fde69 Author: Sylvain Bavencoff <bavencoff@codelutin.com> Date: Thu May 18 17:55:32 2017 +0200 dépouillement des sondages restreints --- .../chorem/pollen/rest/api/v1/VoteCountingApi.java | 4 +- ...ltBean.java => ListVoteCountingResultBean.java} | 18 +- .../services/service/VoteCountingService.java | 104 +++++---- .../votecounting/AbstractVoteCountingStrategy.java | 30 +-- .../pollen/votecounting/VoteCountingStrategy.java | 16 +- .../pollen/votecounting/model/ChoiceScore.java | 30 +++ .../votecounting/model/GroupOfVoterBuilder.java | 90 -------- .../model/{GroupOfVoter.java => ListOfVoter.java} | 34 +-- ...tingResult.java => ListVoteCountingResult.java} | 28 +-- pollen-votecounting-borda/pom.xml | 5 + .../votecounting/BordaVoteCountingStrategy.java | 17 ++ .../BordaVoteCountingStrategyTest.java | 189 +++++++++------- pollen-votecounting-condorcet/pom.xml | 5 + .../CondorcetVoteCountingStrategy.java | 16 ++ .../CondorcetVoteCountingStrategyTest.java | 192 +++++++++------- pollen-votecounting-coombs/pom.xml | 5 + .../votecounting/CoombsVoteCountingStrategy.java | 16 ++ .../CoombsVoteCountingStrategyTest.java | 127 +++++------ pollen-votecounting-instant-runoff/pom.xml | 5 + .../InstantRunoffVoteCountingStrategy.java | 16 ++ .../InstantRunoffVoteCountingStrategyTest.java | 136 +++++------- pollen-votecounting-normal/pom.xml | 6 + .../votecounting/NormalVoteCountingStrategy.java | 18 ++ .../NormalVoteCountingStrategyTest.java | 241 +++++++++------------ pollen-votecounting-number/pom.xml | 5 + .../votecounting/NumberVoteCountingStrategy.java | 16 ++ .../NumberVoteCountingStrategyTest.java | 173 +++++++++------ pollen-votecounting-percentage/pom.xml | 5 + .../PercentageVoteCountingStrategy.java | 28 +++ .../PercentageVoteCountingStrategyTest.java | 191 +++++++++------- pom.xml | 6 + 31 files changed, 979 insertions(+), 793 deletions(-) diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoteCountingApi.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoteCountingApi.java index ef3fbf4..87ec05e 100644 --- a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoteCountingApi.java +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/VoteCountingApi.java @@ -22,7 +22,7 @@ package org.chorem.pollen.rest.api.v1; */ import org.chorem.pollen.persistence.entity.Poll; -import org.chorem.pollen.services.bean.GroupVoteCountingResultBean; +import org.chorem.pollen.services.bean.ListVoteCountingResultBean; import org.chorem.pollen.services.bean.PollenEntityId; import org.chorem.pollen.services.bean.VoteCountingResultBean; import org.chorem.pollen.services.service.VoteCountingService; @@ -42,7 +42,7 @@ public class VoteCountingApi extends WebMotionController { } - public GroupVoteCountingResultBean getGroupResult(VoteCountingService voteCountingService, PollenEntityId<Poll> pollId) { + public ListVoteCountingResultBean getGroupResult(VoteCountingService voteCountingService, PollenEntityId<Poll> pollId) { return voteCountingService.getGroupResult(pollId.getEntityId()); diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/bean/GroupVoteCountingResultBean.java b/pollen-services/src/main/java/org/chorem/pollen/services/bean/ListVoteCountingResultBean.java similarity index 72% rename from pollen-services/src/main/java/org/chorem/pollen/services/bean/GroupVoteCountingResultBean.java rename to pollen-services/src/main/java/org/chorem/pollen/services/bean/ListVoteCountingResultBean.java index 2b8ea4d..09ed52f 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/bean/GroupVoteCountingResultBean.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/bean/ListVoteCountingResultBean.java @@ -21,7 +21,7 @@ package org.chorem.pollen.services.bean; * #L% */ -import org.chorem.pollen.votecounting.model.GroupVoteCountingResult; +import org.chorem.pollen.votecounting.model.ListVoteCountingResult; import org.chorem.pollen.votecounting.model.VoteCountingResult; import java.util.ArrayList; @@ -34,24 +34,24 @@ import java.util.List; * @author Tony Chemit - dev@tchemit.fr * @since 2.0 */ -public class GroupVoteCountingResultBean { +public class ListVoteCountingResultBean { protected VoteCountingResultBean mainResult; - protected final List<VoteCountingGroupResultBean> groupResult; + protected final List<VoteCountingResultBean> subListResult; - public GroupVoteCountingResultBean() { - groupResult = new ArrayList<>(); + public ListVoteCountingResultBean() { + subListResult = new ArrayList<>(); } - public void fromResult(GroupVoteCountingResult result) { + public void fromResult(ListVoteCountingResult result) { setMainResult(result.getMainResult()); for (String groupId : result.getGroupIds()) { - VoteCountingGroupResultBean voteCountingGroupResult = new VoteCountingGroupResultBean(); - voteCountingGroupResult.fromResult(result.getGroupResult(groupId), groupId); - groupResult.add(voteCountingGroupResult); + VoteCountingResultBean voteCountingResult = new VoteCountingResultBean(); + voteCountingResult.fromResult(result.getListResult(groupId)); + subListResult.add(voteCountingResult); } } diff --git a/pollen-services/src/main/java/org/chorem/pollen/services/service/VoteCountingService.java b/pollen-services/src/main/java/org/chorem/pollen/services/service/VoteCountingService.java index 97a014d..0023bfa 100644 --- a/pollen-services/src/main/java/org/chorem/pollen/services/service/VoteCountingService.java +++ b/pollen-services/src/main/java/org/chorem/pollen/services/service/VoteCountingService.java @@ -22,7 +22,7 @@ package org.chorem.pollen.services.service; */ import com.google.common.base.Preconditions; -import org.apache.commons.lang3.tuple.Pair; +import com.google.common.collect.Sets; import org.chorem.pollen.persistence.entity.Poll; import org.chorem.pollen.persistence.entity.Polls; import org.chorem.pollen.persistence.entity.Vote; @@ -30,7 +30,7 @@ import org.chorem.pollen.persistence.entity.VoteToChoice; import org.chorem.pollen.persistence.entity.VoterList; import org.chorem.pollen.persistence.entity.VoterListMember; import org.chorem.pollen.services.bean.ChoiceBean; -import org.chorem.pollen.services.bean.GroupVoteCountingResultBean; +import org.chorem.pollen.services.bean.ListVoteCountingResultBean; import org.chorem.pollen.services.bean.VoteBean; import org.chorem.pollen.services.bean.VoteCountingResultBean; import org.chorem.pollen.services.bean.VoteToChoiceBean; @@ -38,16 +38,17 @@ import org.chorem.pollen.services.service.security.PermissionVerb; import org.chorem.pollen.votecounting.VoteCounting; import org.chorem.pollen.votecounting.VoteCountingFactory; import org.chorem.pollen.votecounting.VoteCountingStrategy; -import org.chorem.pollen.votecounting.model.GroupOfVoter; -import org.chorem.pollen.votecounting.model.GroupOfVoterBuilder; -import org.chorem.pollen.votecounting.model.GroupVoteCountingResult; -import org.chorem.pollen.votecounting.model.SimpleVoterBuilder; +import org.chorem.pollen.votecounting.model.ListOfVoter; +import org.chorem.pollen.votecounting.model.ListVoteCountingResult; +import org.chorem.pollen.votecounting.model.SimpleVoter; import org.chorem.pollen.votecounting.model.VoteCountingResult; +import org.chorem.pollen.votecounting.model.VoteForChoice; import org.chorem.pollen.votecounting.model.Voter; import org.chorem.pollen.votecounting.model.VoterBuilder; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; @@ -70,7 +71,7 @@ public class VoteCountingService extends PollenServiceSupport { if (Polls.isPollRestricted(poll)) { - GroupVoteCountingResultBean groupResult = getGroupResult(pollId); + ListVoteCountingResultBean groupResult = getGroupResult(pollId); mainResult = groupResult.getMainResult(); } else { @@ -92,9 +93,9 @@ public class VoteCountingService extends PollenServiceSupport { VoteCounting voteCounting = getVoteCounting(poll); VoteCountingStrategy strategy = voteCounting.newStrategy(); - GroupOfVoter groupOfVoter = toSimpleVotersGroup(votes); + ListOfVoter groupOfVoter = toSimpleVotersGroup(votes); - GroupVoteCountingResult groupVoteCountingResult = strategy.votecount(groupOfVoter); + ListVoteCountingResult groupVoteCountingResult = strategy.votecount(groupOfVoter); VoteCountingResult mainResult = groupVoteCountingResult.getMainResult(); @@ -105,7 +106,7 @@ public class VoteCountingService extends PollenServiceSupport { } - public GroupVoteCountingResultBean getGroupResult(String pollId) { + public ListVoteCountingResultBean getGroupResult(String pollId) { Preconditions.checkNotNull(pollId); @@ -117,12 +118,12 @@ public class VoteCountingService extends PollenServiceSupport { VoteCountingStrategy strategy = voteCounting.newStrategy(); // Create a groupVoter including of the root voters - GroupOfVoter group = toGroupOfVoters(poll, mainVoterList, votes); + ListOfVoter listOfVoter = toListOfVoters(poll, mainVoterList, votes); - GroupVoteCountingResult groupVoteCountingResult = strategy.votecount(group); + ListVoteCountingResult groupVoteCountingResult = strategy.votecount(listOfVoter); - GroupVoteCountingResultBean result = new GroupVoteCountingResultBean(); + ListVoteCountingResultBean result = new ListVoteCountingResultBean(); result.fromResult(groupVoteCountingResult); return result; @@ -142,21 +143,19 @@ public class VoteCountingService extends PollenServiceSupport { * @return the aggregate group of voter containing simple voters with * their votes. */ - protected GroupOfVoter toSimpleVotersGroup(List<Vote> votes) { + protected ListOfVoter toSimpleVotersGroup(List<Vote> votes) { Preconditions.checkNotNull(votes); - SimpleVoterBuilder builder = new SimpleVoterBuilder(); + Set<Voter> voters = Sets.newHashSet(); for (Vote vote : votes) { - fillSimpleVoter(builder, vote); - + SimpleVoter simpleVoter = toSimpleVoter(vote); + voters.add(simpleVoter); } - Set<Voter> voter = builder.getVoters(); - - return GroupOfVoter.newVoter(null, 1.0, voter); + return ListOfVoter.newVoter(null, 1.0, voters); } @@ -167,54 +166,77 @@ public class VoteCountingService extends PollenServiceSupport { * <strong>Note:</strong> This method can only be used for a group poll. * * @param poll the poll to scan - * @param mainVoterList voterList to scan + * @param voterList voterList to scan * @param votes votes to scan * @return the aggregate group of group of voters with their votes */ - protected GroupOfVoter toGroupOfVoters(Poll poll, VoterList mainVoterList, List<Vote> votes) { + protected ListOfVoter toListOfVoters(Poll poll, VoterList voterList, List<Vote> votes) { Preconditions.checkNotNull(poll); - Preconditions.checkNotNull(mainVoterList); + Preconditions.checkNotNull(voterList); Preconditions.checkNotNull(votes); Preconditions.checkArgument(Polls.isPollRestricted(poll), "Can only use this method for a restricted poll"); - GroupOfVoterBuilder builder = new GroupOfVoterBuilder(); + return toListOfVoters(voterList, votes); + + } - // create group of voter - Map<VoterListMember, Vote> voteByVoterListMember = votes.stream() - .flatMap(vote -> vote.getVoterListMember().stream() - .map(member -> Pair.of(member, vote))) - .collect(Collectors.toMap(Pair::getKey, Pair::getValue)); + protected ListOfVoter toListOfVoters(VoterList voterList, List<Vote> votes) { - List<VoterList> voterLists = getVoterListDao().forParentEquals(mainVoterList).findAll(); + Set<Voter> voters = Sets.newHashSet(); - for (VoterList voterList : voterLists) { + List<VoterList> subVoterLists = getVoterListDao().forParentEquals(voterList).findAll(); - builder.newGroupVoter(voterList.getTopiaId(), - voterList.getWeight()); + for (VoterList subVoterList : subVoterLists) { - List<VoterListMember> voterListMembers = getVoterListService().getVoterListMembers0(voterList); - for (VoterListMember voterListMember : voterListMembers) { + ListOfVoter subListOfVoter = toListOfVoters(subVoterList, votes); + voters.add(subListOfVoter); + } - Vote vote = voteByVoterListMember.get(voterListMember); + List<VoterListMember> voterListMembers = getVoterListService().getVoterListMembers0(voterList); + for (VoterListMember voterListMember : voterListMembers) { - if (vote != null) { + Optional<Vote> voteOptional = votes.stream() + .filter(vote -> vote.getVoterListMember().contains(voterListMember)) + .findFirst(); - fillSimpleVoter(builder, vote); + if (voteOptional.isPresent()) { - } + SimpleVoter simpleVoter = toSimpleVoter(voteOptional.get()); + simpleVoter.setWeight(voterListMember.getWeight()); + voters.add(simpleVoter); } } - Set<Voter> voter = builder.getVoters(); + return ListOfVoter.newVoter(voterList.getTopiaId(), voterList.getWeight(), voters); + } - return GroupOfVoter.newVoter(null, 1.0, voter); + protected SimpleVoter toSimpleVoter(Vote vote) { + + Set<VoteForChoice> voteForChoices = Sets.newHashSet(); + + if (!vote.isVoteToChoiceEmpty()) { + + for (VoteToChoice voteToChoice : vote.getVoteToChoice()) { + + VoteForChoice voteForChoice = VoteForChoice.newVote( + voteToChoice.getChoice().getTopiaId(), + voteToChoice.getVoteValue()); + + voteForChoices.add(voteForChoice); + + } + + } + + return SimpleVoter.newVoter(vote.getVoter().getTopiaId(), vote.getWeight(), voteForChoices); } + protected void fillSimpleVoter(VoterBuilder builder, Vote vote) { builder.newVoter(vote.getVoter().getTopiaId(), vote.getWeight()); diff --git a/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/AbstractVoteCountingStrategy.java b/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/AbstractVoteCountingStrategy.java index 8b0b19b..3ad7b92 100644 --- a/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/AbstractVoteCountingStrategy.java +++ b/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/AbstractVoteCountingStrategy.java @@ -27,8 +27,8 @@ import com.google.common.collect.Multimaps; import com.google.common.collect.Sets; import org.chorem.pollen.votecounting.model.ChoiceIdAble; import org.chorem.pollen.votecounting.model.ChoiceScore; -import org.chorem.pollen.votecounting.model.GroupOfVoter; -import org.chorem.pollen.votecounting.model.GroupVoteCountingResult; +import org.chorem.pollen.votecounting.model.ListOfVoter; +import org.chorem.pollen.votecounting.model.ListVoteCountingResult; import org.chorem.pollen.votecounting.model.VoteCountingResult; import org.chorem.pollen.votecounting.model.VoteForChoice; import org.chorem.pollen.votecounting.model.Voter; @@ -65,13 +65,13 @@ public abstract class AbstractVoteCountingStrategy implements VoteCountingStrate }; @Override - public final GroupVoteCountingResult votecount(GroupOfVoter group) { + public final ListVoteCountingResult votecount(ListOfVoter listOfVoter) { - Set<GroupOfVoter> groups = Sets.newHashSet(); - voteCount(group, groups); + Set<ListOfVoter> lists = Sets.newHashSet(); + voteCount(listOfVoter, lists); - return GroupVoteCountingResult.newResult( - group, groups); + return ListVoteCountingResult.newResult( + listOfVoter, lists); } public Map<String, ChoiceScore> newEmptyChoiceScoreMap(Set<Voter> voters) { @@ -170,19 +170,19 @@ public abstract class AbstractVoteCountingStrategy implements VoteCountingStrate return result; } - protected void voteCount(GroupOfVoter group, Set<GroupOfVoter> groups) { + protected void voteCount(ListOfVoter listOfVoter, Set<ListOfVoter> listOfVoters) { - groups.add(group); + listOfVoters.add(listOfVoter); // all childs of this group - Set<Voter> voters = group.getVoters(); + Set<Voter> voters = listOfVoter.getVoters(); // treat before all his group childs for (Voter voter : voters) { - if (voter instanceof GroupOfVoter) { + if (voter instanceof ListOfVoter) { // treat group child before all - voteCount((GroupOfVoter) voter, groups); + voteCount((ListOfVoter) voter, listOfVoters); } } @@ -190,6 +190,10 @@ public abstract class AbstractVoteCountingStrategy implements VoteCountingStrate VoteCountingResult voteCountingResult = votecount(voters); // store the result for this group - group.setResult(voteCountingResult); + listOfVoter.setResult(voteCountingResult); + + Set<VoteForChoice> voteForChoices = toVoteForChoices(voteCountingResult); + + listOfVoter.getVoteForChoices().addAll(voteForChoices); } } diff --git a/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/VoteCountingStrategy.java b/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/VoteCountingStrategy.java index 73d4e9f..93f9de1 100644 --- a/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/VoteCountingStrategy.java +++ b/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/VoteCountingStrategy.java @@ -20,9 +20,10 @@ */ package org.chorem.pollen.votecounting; -import org.chorem.pollen.votecounting.model.GroupOfVoter; -import org.chorem.pollen.votecounting.model.GroupVoteCountingResult; +import org.chorem.pollen.votecounting.model.ListOfVoter; +import org.chorem.pollen.votecounting.model.ListVoteCountingResult; import org.chorem.pollen.votecounting.model.VoteCountingResult; +import org.chorem.pollen.votecounting.model.VoteForChoice; import org.chorem.pollen.votecounting.model.Voter; import java.util.Set; @@ -39,10 +40,10 @@ public interface VoteCountingStrategy { * Vote count for the given {@code group} of voters and return the * result of it. * - * @param group the group of voters and their votes. + * @param listOfVoter the list of voters and their votes. * @return the result of the group vote counting for the given voter. */ - GroupVoteCountingResult votecount(GroupOfVoter group); + ListVoteCountingResult votecount(ListOfVoter listOfVoter); /** * Vote count for the given {@code voter} and return the result of it. @@ -52,4 +53,11 @@ public interface VoteCountingStrategy { */ VoteCountingResult votecount(Set<Voter> voter); + /** + * transform the result of vote to vote + * + * @param voteCountingResult the result of vote + * @return the vot for choices + */ + Set<VoteForChoice> toVoteForChoices(VoteCountingResult voteCountingResult); } diff --git a/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/ChoiceScore.java b/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/ChoiceScore.java index 5ea0467..59e70b4 100644 --- a/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/ChoiceScore.java +++ b/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/ChoiceScore.java @@ -22,6 +22,7 @@ package org.chorem.pollen.votecounting.model; import java.io.Serializable; import java.math.BigDecimal; +import java.util.Objects; import java.util.function.Function; /** @@ -59,9 +60,14 @@ public class ChoiceScore implements ChoiceIdAble, Comparable<ChoiceScore>, Seria private int scoreOrder; public static ChoiceScore newScore(String choiceId, BigDecimal scoreValue) { + return newScore(choiceId, scoreValue, 0); + } + + public static ChoiceScore newScore(String choiceId, BigDecimal scoreValue, int scoreOrder) { ChoiceScore choiceScore = new ChoiceScore(); choiceScore.setChoiceId(choiceId); choiceScore.setScoreValue(scoreValue); + choiceScore.setScoreOrder(scoreOrder); return choiceScore; } @@ -105,4 +111,28 @@ public class ChoiceScore implements ChoiceIdAble, Comparable<ChoiceScore>, Seria public int compareTo(ChoiceScore o) { return scoreOrder - o.scoreOrder; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ChoiceScore that = (ChoiceScore) o; + return scoreOrder == that.scoreOrder && + Objects.equals(choiceId, that.choiceId) && + Objects.equals(scoreValue, that.scoreValue); + } + + @Override + public int hashCode() { + return Objects.hash(choiceId, scoreValue, scoreOrder); + } + + @Override + public String toString() { + return "ChoiceScore{" + + "choiceId='" + choiceId + '\'' + + ", scoreValue=" + scoreValue + + ", scoreOrder=" + scoreOrder + + '}'; + } } diff --git a/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/GroupOfVoterBuilder.java b/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/GroupOfVoterBuilder.java deleted file mode 100644 index fb3ad5c..0000000 --- a/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/GroupOfVoterBuilder.java +++ /dev/null @@ -1,90 +0,0 @@ -package org.chorem.pollen.votecounting.model; - -/* - * #%L - * Pollen :: VoteCounting (Api) - * %% - * Copyright (C) 2009 - 2017 Code Lutin, Tony Chemit - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * #L% - */ - -import com.google.common.base.Preconditions; -import com.google.common.collect.Sets; - -import java.util.Set; - -/** - * {@link GroupOfVoter} builder. - * - * @author Tony Chemit - dev@tchemit.fr - * @since 1.4.6 - */ -public class GroupOfVoterBuilder implements VoterBuilder { - - private final Set<Voter> voters = Sets.newHashSet(); - - private GroupOfVoter groupOfVoter; - - private Voter voter; - - @Override - public Set<Voter> getVoters() { - flush(); - return voters; - } - - @Override - public GroupOfVoterBuilder newVoter(String voterId, double weight) { - Preconditions.checkState(groupOfVoter != null, "No groupOfVoter defined, use method newGroupVoter before"); - flushVoter(); - voter = SimpleVoter.newVoter(voterId, weight, null); - return this; - } - - @Override - public GroupOfVoterBuilder addVoteForChoice(String choiceId, Double voteValue) { - Preconditions.checkState(voter != null, "No voter defined, use method newVoter before this one"); - VoteForChoice voteForChoice = VoteForChoice.newVote(choiceId, voteValue); - voter.addVoteForChoice(voteForChoice); - return this; - } - - public GroupOfVoterBuilder newGroupVoter(String voterId, double weight) { - flush(); - groupOfVoter = GroupOfVoter.newVoter(voterId, weight, null); - return this; - } - - public void flushVoter() { - if (voter != null) { - groupOfVoter.addVoter(voter); - voter = null; - } - } - - @Override - public void flush() { - if (groupOfVoter != null) { - flushVoter(); - voters.add(groupOfVoter); - groupOfVoter = null; - } - } - - public GroupOfVoter getRoot() { - return (GroupOfVoter) getVoters().iterator().next(); - } -} diff --git a/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/GroupOfVoter.java b/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/ListOfVoter.java similarity index 75% rename from pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/GroupOfVoter.java rename to pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/ListOfVoter.java index 3ec2266..9025956 100644 --- a/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/GroupOfVoter.java +++ b/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/ListOfVoter.java @@ -22,7 +22,6 @@ package org.chorem.pollen.votecounting.model; import com.google.common.collect.Sets; -import java.util.Collection; import java.util.Iterator; import java.util.Set; @@ -32,7 +31,7 @@ import java.util.Set; * @author Tony Chemit - dev@tchemit.fr * @since 1.4.5 */ -public class GroupOfVoter implements Voter, Iterable<Voter> { +public class ListOfVoter implements Voter, Iterable<Voter> { private static final long serialVersionUID = 1L; @@ -44,10 +43,15 @@ public class GroupOfVoter implements Voter, Iterable<Voter> { /** Result for this group. */ private VoteCountingResult result; - public static GroupOfVoter newVoter(String voterId, - double weight, - Set<Voter> voters) { - GroupOfVoter result = new GroupOfVoter(); + public static ListOfVoter newVoter(String voterId, + double weight) { + return newVoter(voterId, weight, Sets.newHashSet()); + } + + public static ListOfVoter newVoter(String voterId, + double weight, + Set<Voter> voters) { + ListOfVoter result = new ListOfVoter(); result.setVoterId(voterId); result.setWeight(weight); result.setVoters(voters); @@ -106,15 +110,15 @@ public class GroupOfVoter implements Voter, Iterable<Voter> { public void setResult(VoteCountingResult result) { this.result = result; this.result.setNbVotants(voters.size()); - Collection<ChoiceScore> winners = result.getTopRanking(); - for (ChoiceScore choiceScore : result.getScores()) { - - double score = winners.contains(choiceScore) ? 1d : 0d; - VoteForChoice voteForChoice = VoteForChoice.newVote( - choiceScore.getChoiceId(), - score); - addVoteForChoice(voteForChoice); - } +// Collection<ChoiceScore> winners = result.getTopRanking(); +// for (ChoiceScore choiceScore : result.getScores()) { +// +// double score = winners.contains(choiceScore) ? 1d : 0d; +// VoteForChoice voteForChoice = VoteForChoice.newVote( +// choiceScore.getChoiceId(), +// score); +// addVoteForChoice(voteForChoice); +// } } @Override diff --git a/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/GroupVoteCountingResult.java b/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/ListVoteCountingResult.java similarity index 64% rename from pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/GroupVoteCountingResult.java rename to pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/ListVoteCountingResult.java index 3cb828b..e521bb7 100644 --- a/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/GroupVoteCountingResult.java +++ b/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/ListVoteCountingResult.java @@ -33,20 +33,20 @@ import java.util.Set; * @author Tony Chemit - dev@tchemit.fr * @since 1.4.5 */ -public class GroupVoteCountingResult implements Serializable { +public class ListVoteCountingResult implements Serializable { private static final long serialVersionUID = 1L; - protected GroupOfVoter voter; + protected ListOfVoter voter; - protected Map<String, GroupOfVoter> groups; + protected Map<String, ListOfVoter> lists; - public static GroupVoteCountingResult newResult(GroupOfVoter voter, - Set<GroupOfVoter> groups) { - GroupVoteCountingResult result = new GroupVoteCountingResult(); + public static ListVoteCountingResult newResult(ListOfVoter voter, + Set<ListOfVoter> lists) { + ListVoteCountingResult result = new ListVoteCountingResult(); result.setVoter(voter); - groups.remove(voter); - result.setGroups(groups); + lists.remove(voter); + result.setLists(lists); return result; } @@ -55,19 +55,19 @@ public class GroupVoteCountingResult implements Serializable { } public Set<String> getGroupIds() { - return Sets.newHashSet(groups.keySet()); + return Sets.newHashSet(lists.keySet()); } - public VoteCountingResult getGroupResult(String groupId) { - GroupOfVoter groupOfVoter = groups.get(groupId); + public VoteCountingResult getListResult(String listId) { + ListOfVoter groupOfVoter = lists.get(listId); return groupOfVoter.getResult(); } - public void setVoter(GroupOfVoter voter) { + public void setVoter(ListOfVoter voter) { this.voter = voter; } - public void setGroups(Set<GroupOfVoter> groups) { - this.groups = Maps.uniqueIndex(groups, GroupOfVoter::getVoterId); + public void setLists(Set<ListOfVoter> lists) { + this.lists = Maps.uniqueIndex(lists, ListOfVoter::getVoterId); } } diff --git a/pollen-votecounting-borda/pom.xml b/pollen-votecounting-borda/pom.xml index 38ed44e..6f81554 100644 --- a/pollen-votecounting-borda/pom.xml +++ b/pollen-votecounting-borda/pom.xml @@ -70,6 +70,11 @@ <artifactId>junit</artifactId> </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + </dependency> + </dependencies> <build> diff --git a/pollen-votecounting-borda/src/main/java/org/chorem/pollen/votecounting/BordaVoteCountingStrategy.java b/pollen-votecounting-borda/src/main/java/org/chorem/pollen/votecounting/BordaVoteCountingStrategy.java index 22150b8..36da314 100644 --- a/pollen-votecounting-borda/src/main/java/org/chorem/pollen/votecounting/BordaVoteCountingStrategy.java +++ b/pollen-votecounting-borda/src/main/java/org/chorem/pollen/votecounting/BordaVoteCountingStrategy.java @@ -20,10 +20,12 @@ */ package org.chorem.pollen.votecounting; +import com.google.common.collect.Sets; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.chorem.pollen.votecounting.model.ChoiceScore; import org.chorem.pollen.votecounting.model.VoteCountingResult; +import org.chorem.pollen.votecounting.model.VoteForChoice; import org.chorem.pollen.votecounting.model.Voter; import java.util.List; @@ -80,4 +82,19 @@ public class BordaVoteCountingStrategy extends AbstractVoteCountingStrategy { // order scores (using their value) and return result return orderByValues(scores.values()); } + + @Override + public Set<VoteForChoice> toVoteForChoices(VoteCountingResult voteCountingResult) { + Set<VoteForChoice> voteForChoices = Sets.newHashSet(); + + for (ChoiceScore choiceScore : voteCountingResult.getScores()) { + + double score = choiceScore.getScoreOrder(); + VoteForChoice voteForChoice = VoteForChoice.newVote( + choiceScore.getChoiceId(), + score); + voteForChoices.add(voteForChoice); + } + return voteForChoices; + } } diff --git a/pollen-votecounting-borda/src/test/java/org/chorem/pollen/votecounting/BordaVoteCountingStrategyTest.java b/pollen-votecounting-borda/src/test/java/org/chorem/pollen/votecounting/BordaVoteCountingStrategyTest.java index 27d71d6..bb9404f 100644 --- a/pollen-votecounting-borda/src/test/java/org/chorem/pollen/votecounting/BordaVoteCountingStrategyTest.java +++ b/pollen-votecounting-borda/src/test/java/org/chorem/pollen/votecounting/BordaVoteCountingStrategyTest.java @@ -20,23 +20,24 @@ */ package org.chorem.pollen.votecounting; -import com.google.common.collect.Maps; import com.google.common.collect.Sets; -import org.chorem.pollen.votecounting.model.ChoiceIdAble; import org.chorem.pollen.votecounting.model.ChoiceScore; +import org.chorem.pollen.votecounting.model.ListOfVoter; +import org.chorem.pollen.votecounting.model.ListVoteCountingResult; +import org.chorem.pollen.votecounting.model.SimpleVoter; import org.chorem.pollen.votecounting.model.SimpleVoterBuilder; import org.chorem.pollen.votecounting.model.VoteCountingResult; +import org.chorem.pollen.votecounting.model.VoteForChoice; import org.chorem.pollen.votecounting.model.Voter; -import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import java.math.BigDecimal; -import java.util.List; -import java.util.Map; import java.util.Set; +import static org.assertj.core.api.Assertions.assertThat; + /** * Tests the {@link BordaVoteCountingStrategy}. * @@ -105,15 +106,15 @@ public class BordaVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(4, scores.size()); - - assertChoiceScore(scores.get(0), CHOICE_B, BigDecimal.valueOf(294.)); - assertChoiceScore(scores.get(1), CHOICE_C, BigDecimal.valueOf(273.)); - assertChoiceScore(scores.get(2), CHOICE_A, BigDecimal.valueOf(226.)); - assertChoiceScore(scores.get(3), CHOICE_D, BigDecimal.valueOf(207.)); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(226.0), 2), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(294.0), 0), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(273.0), 1), + ChoiceScore.newScore(CHOICE_D, BigDecimal.valueOf(207.0), 3)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -141,14 +142,14 @@ public class BordaVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScore(scores.get(0), CHOICE_A, BigDecimal.valueOf(7.)); - assertChoiceScore(scores.get(1), CHOICE_C, BigDecimal.valueOf(6.)); - assertChoiceScore(scores.get(2), CHOICE_B, BigDecimal.valueOf(5.)); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(7.0), 0), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(5.0), 2), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(6.0), 1)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -177,14 +178,14 @@ public class BordaVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScore(scores.get(0), CHOICE_A, BigDecimal.valueOf(8.)); - assertChoiceScore(scores.get(1), CHOICE_C, BigDecimal.valueOf(7.)); - assertChoiceScore(scores.get(2), CHOICE_B, BigDecimal.valueOf(6.)); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(8.0), 0), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(6.0), 2), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(7.0), 1)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -213,17 +214,14 @@ public class BordaVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScoreEquals(BigDecimal.valueOf(7.), - Sets.newHashSet(scores.get(0), - scores.get(1), - scores.get(2) - ), - CHOICE_A, CHOICE_B, CHOICE_C); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(7.0), 0), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(7.0), 0), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(7.0), 0)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -252,14 +250,14 @@ public class BordaVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScore(scores.get(0), CHOICE_B, BigDecimal.valueOf(10.)); - assertChoiceScore(scores.get(1), CHOICE_A, BigDecimal.valueOf(9.)); - assertChoiceScore(scores.get(2), CHOICE_C, BigDecimal.valueOf(8.)); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(9.0), 1), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(10.0), 0), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(8.0), 2)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -288,37 +286,76 @@ public class BordaVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScore(scores.get(0), CHOICE_C, BigDecimal.valueOf(15.)); - assertChoiceScore(scores.get(1), CHOICE_B, BigDecimal.valueOf(13.)); - assertChoiceScore(scores.get(2), CHOICE_A, BigDecimal.valueOf(11.)); - } - - public static void assertChoiceScore(ChoiceScore choiceScore, - String choiceId, - BigDecimal choiceResult) { - Assert.assertNotNull(choiceScore); - Assert.assertEquals(choiceId, choiceScore.getChoiceId()); - Assert.assertEquals(choiceResult, choiceScore.getScoreValue()); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(11.0), 2), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(13.0), 1), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(15.0), 0)) + .isSortedAccordingTo(ChoiceScore::compareTo); } - public static void assertChoiceScoreEquals(BigDecimal choiceResult, - Set<ChoiceScore> choiceScores, - String... choiceIds) { - Assert.assertNotNull(choiceScores); - Map<String, ChoiceScore> choicesById = Maps.uniqueIndex( - choiceScores, new ChoiceIdAble.ChoiceIdAbleById()::apply); - for (String choiceId : choiceIds) { - ChoiceScore choiceScore = choicesById.get(choiceId); - Assert.assertNotNull(choiceScore); - assertChoiceScore(choiceScore, choiceId, choiceResult); - } + @Test + public void listVotecount() throws Exception { + // A B C + // G1U1 1 2 3 * 2 + // G1U2 1 2 2 + // G1U3 3 2 1 + // Result G1 6 4 3 + // Vote G1 1 2 3 + + // G2U1 1 2 3 * 2 + // G2U2 3 2 1 + // G2U3 3 1 2 + // Result G2 4 4 3 + // Vote G1 1 1 2 * 2 + + // Result 6 3 1 + + ListOfVoter voters = ListOfVoter.newVoter(null, 1, Sets.newHashSet( + ListOfVoter.newVoter("G1", 1, Sets.newHashSet( + SimpleVoter.newVoter("G1U1", 2, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 1d), + VoteForChoice.newVote(CHOICE_B, 2d), + VoteForChoice.newVote(CHOICE_C, 3d))), + SimpleVoter.newVoter("G1U2", 1, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 1d), + VoteForChoice.newVote(CHOICE_B, 2d), + VoteForChoice.newVote(CHOICE_C, 2d))), + SimpleVoter.newVoter("G1U3", 1, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 3d), + VoteForChoice.newVote(CHOICE_B, 2d), + VoteForChoice.newVote(CHOICE_C, 1d))))), + ListOfVoter.newVoter("G2", 2, Sets.newHashSet( + SimpleVoter.newVoter("G2U1", 2, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 1d), + VoteForChoice.newVote(CHOICE_B, 2d), + VoteForChoice.newVote(CHOICE_C, 3d))), + SimpleVoter.newVoter("G2U2", 1, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 3d), + VoteForChoice.newVote(CHOICE_B, 2d), + VoteForChoice.newVote(CHOICE_C, 1d))), + SimpleVoter.newVoter("G2U3", 1, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 3d), + VoteForChoice.newVote(CHOICE_B, 1d), + VoteForChoice.newVote(CHOICE_C, 2d))))) + )); + + ListVoteCountingResult result = strategy.votecount(voters); + + assertThat(result) + .isNotNull(); + assertThat(result.getMainResult()).isNotNull(); + assertThat(result.getMainResult().getScores()).isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(7.0), 1), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(8.0), 0), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(3.0), 2)) + .isSortedAccordingTo(ChoiceScore::compareTo); } + } diff --git a/pollen-votecounting-condorcet/pom.xml b/pollen-votecounting-condorcet/pom.xml index 15f6d71..b0fd001 100644 --- a/pollen-votecounting-condorcet/pom.xml +++ b/pollen-votecounting-condorcet/pom.xml @@ -70,6 +70,11 @@ <artifactId>junit</artifactId> </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + </dependency> + </dependencies> <build> diff --git a/pollen-votecounting-condorcet/src/main/java/org/chorem/pollen/votecounting/CondorcetVoteCountingStrategy.java b/pollen-votecounting-condorcet/src/main/java/org/chorem/pollen/votecounting/CondorcetVoteCountingStrategy.java index 011da92..bf15dcd 100644 --- a/pollen-votecounting-condorcet/src/main/java/org/chorem/pollen/votecounting/CondorcetVoteCountingStrategy.java +++ b/pollen-votecounting-condorcet/src/main/java/org/chorem/pollen/votecounting/CondorcetVoteCountingStrategy.java @@ -20,6 +20,7 @@ */ package org.chorem.pollen.votecounting; +import com.google.common.collect.Sets; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.chorem.pollen.votecounting.model.ChoiceScore; @@ -58,6 +59,21 @@ public class CondorcetVoteCountingStrategy extends AbstractVoteCountingStrategy return orderByValues(scores.values()); } + @Override + public Set<VoteForChoice> toVoteForChoices(VoteCountingResult voteCountingResult) { + Set<VoteForChoice> voteForChoices = Sets.newHashSet(); + + for (ChoiceScore choiceScore : voteCountingResult.getScores()) { + + double score = choiceScore.getScoreOrder(); + VoteForChoice voteForChoice = VoteForChoice.newVote( + choiceScore.getChoiceId(), + score); + voteForChoices.add(voteForChoice); + } + return voteForChoices; + } + protected void addVoterChoices(Voter voter, Map<String, ChoiceScore> scores) { diff --git a/pollen-votecounting-condorcet/src/test/java/org/chorem/pollen/votecounting/CondorcetVoteCountingStrategyTest.java b/pollen-votecounting-condorcet/src/test/java/org/chorem/pollen/votecounting/CondorcetVoteCountingStrategyTest.java index 9414484..d20f22c 100644 --- a/pollen-votecounting-condorcet/src/test/java/org/chorem/pollen/votecounting/CondorcetVoteCountingStrategyTest.java +++ b/pollen-votecounting-condorcet/src/test/java/org/chorem/pollen/votecounting/CondorcetVoteCountingStrategyTest.java @@ -20,23 +20,24 @@ */ package org.chorem.pollen.votecounting; -import com.google.common.collect.Maps; import com.google.common.collect.Sets; -import org.chorem.pollen.votecounting.model.ChoiceIdAble; import org.chorem.pollen.votecounting.model.ChoiceScore; +import org.chorem.pollen.votecounting.model.ListOfVoter; +import org.chorem.pollen.votecounting.model.ListVoteCountingResult; +import org.chorem.pollen.votecounting.model.SimpleVoter; import org.chorem.pollen.votecounting.model.SimpleVoterBuilder; import org.chorem.pollen.votecounting.model.VoteCountingResult; +import org.chorem.pollen.votecounting.model.VoteForChoice; import org.chorem.pollen.votecounting.model.Voter; -import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import java.math.BigDecimal; -import java.util.List; -import java.util.Map; import java.util.Set; +import static org.assertj.core.api.Assertions.assertThat; + /** * Tests the {@link CondorcetVoteCountingStrategy}. * @@ -92,14 +93,14 @@ public class CondorcetVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScore(scores.get(0), CHOICE_A, BigDecimal.valueOf(6.)); - assertChoiceScore(scores.get(1), CHOICE_C, BigDecimal.valueOf(2.)); - assertChoiceScore(scores.get(2), CHOICE_B, BigDecimal.valueOf(1.)); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(6.0), 0), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(1.0), 2), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(2.0), 1)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -128,14 +129,14 @@ public class CondorcetVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScore(scores.get(0), CHOICE_A, BigDecimal.valueOf(4.)); - assertChoiceScore(scores.get(1), CHOICE_C, BigDecimal.valueOf(3.)); - assertChoiceScore(scores.get(2), CHOICE_B, BigDecimal.valueOf(2.)); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(4.0), 0), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(2.0), 2), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(3.0), 1)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -164,16 +165,14 @@ public class CondorcetVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScoreEquals(BigDecimal.valueOf(3.), - Sets.newHashSet(scores.get(0), - scores.get(1)), - CHOICE_A, CHOICE_C); - assertChoiceScore(scores.get(2), CHOICE_B, BigDecimal.valueOf(1.)); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(3.0), 0), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(1.0), 1), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(3.0), 0)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -202,17 +201,14 @@ public class CondorcetVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScoreEquals(BigDecimal.valueOf(2.), - Sets.newHashSet(scores.get(0), - scores.get(1), - scores.get(2) - ), - CHOICE_A, CHOICE_B, CHOICE_C); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(2.0), 0), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(2.0), 0), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(2.0), 0)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -241,16 +237,14 @@ public class CondorcetVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScoreEquals(BigDecimal.valueOf(4.), - Sets.newHashSet(scores.get(0), - scores.get(1)), - CHOICE_A, CHOICE_B); - assertChoiceScore(scores.get(2), CHOICE_C, BigDecimal.valueOf(1.)); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(4.0), 0), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(4.0), 0), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(1.0), 1)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -279,37 +273,73 @@ public class CondorcetVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScore(scores.get(0), CHOICE_C, BigDecimal.valueOf(6.)); - assertChoiceScore(scores.get(1), CHOICE_B, BigDecimal.valueOf(5.)); - assertChoiceScore(scores.get(2), CHOICE_A, BigDecimal.valueOf(4.)); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(4.0), 2), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(5.0), 1), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(6.0), 0)) + .isSortedAccordingTo(ChoiceScore::compareTo); } - public static void assertChoiceScore(ChoiceScore choiceScore, - String choiceId, - BigDecimal choiceResult) { - Assert.assertNotNull(choiceScore); - Assert.assertEquals(choiceId, choiceScore.getChoiceId()); - Assert.assertEquals(choiceResult, choiceScore.getScoreValue()); - } - - public static void assertChoiceScoreEquals(BigDecimal choiceResult, - Set<ChoiceScore> choiceScores, - String... choiceIds) { - - Assert.assertNotNull(choiceScores); - Map<String, ChoiceScore> choicesById = Maps.uniqueIndex( - choiceScores, new ChoiceIdAble.ChoiceIdAbleById()::apply); - for (String choiceId : choiceIds) { - - ChoiceScore choiceScore = choicesById.get(choiceId); - Assert.assertNotNull(choiceScore); - assertChoiceScore(choiceScore, choiceId, choiceResult); - } + @Test + public void listVotecount() throws Exception { + // A B C + // G1U1 1 2 3 * 2 + // G1U2 1 2 2 + // G1U3 3 2 1 + // Result G1 6 4 1 + // Vote G1 1 2 3 + + // G2U1 1 2 3 * 2 + // G2U2 3 2 1 + // G2U3 3 1 2 + // Result G2 4 5 3 + // Vote G1 2 1 3 * 2 + + // Result 4 5 0 + + ListOfVoter voters = ListOfVoter.newVoter(null, 1, Sets.newHashSet( + ListOfVoter.newVoter("G1", 1, Sets.newHashSet( + SimpleVoter.newVoter("G1U1", 2, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 1d), + VoteForChoice.newVote(CHOICE_B, 2d), + VoteForChoice.newVote(CHOICE_C, 3d))), + SimpleVoter.newVoter("G1U2", 1, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 1d), + VoteForChoice.newVote(CHOICE_B, 2d), + VoteForChoice.newVote(CHOICE_C, 2d))), + SimpleVoter.newVoter("G1U3", 1, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 3d), + VoteForChoice.newVote(CHOICE_B, 2d), + VoteForChoice.newVote(CHOICE_C, 1d))))), + ListOfVoter.newVoter("G2", 2, Sets.newHashSet( + SimpleVoter.newVoter("G2U1", 2, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 1d), + VoteForChoice.newVote(CHOICE_B, 2d), + VoteForChoice.newVote(CHOICE_C, 3d))), + SimpleVoter.newVoter("G2U2", 1, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 3d), + VoteForChoice.newVote(CHOICE_B, 2d), + VoteForChoice.newVote(CHOICE_C, 1d))), + SimpleVoter.newVoter("G2U3", 1, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 3d), + VoteForChoice.newVote(CHOICE_B, 1d), + VoteForChoice.newVote(CHOICE_C, 2d))))) + )); + + ListVoteCountingResult result = strategy.votecount(voters); + + assertThat(result) + .isNotNull(); + assertThat(result.getMainResult()).isNotNull(); + assertThat(result.getMainResult().getScores()).isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(4.0), 1), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(5.0), 0), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(0.0), 2)) + .isSortedAccordingTo(ChoiceScore::compareTo); } } diff --git a/pollen-votecounting-coombs/pom.xml b/pollen-votecounting-coombs/pom.xml index 68cfde5..305da8c 100644 --- a/pollen-votecounting-coombs/pom.xml +++ b/pollen-votecounting-coombs/pom.xml @@ -70,6 +70,11 @@ <artifactId>junit</artifactId> </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + </dependency> + </dependencies> <build> diff --git a/pollen-votecounting-coombs/src/main/java/org/chorem/pollen/votecounting/CoombsVoteCountingStrategy.java b/pollen-votecounting-coombs/src/main/java/org/chorem/pollen/votecounting/CoombsVoteCountingStrategy.java index 3f262c9..1633744 100644 --- a/pollen-votecounting-coombs/src/main/java/org/chorem/pollen/votecounting/CoombsVoteCountingStrategy.java +++ b/pollen-votecounting-coombs/src/main/java/org/chorem/pollen/votecounting/CoombsVoteCountingStrategy.java @@ -26,6 +26,7 @@ import com.google.common.collect.Sets; import org.apache.commons.collections4.CollectionUtils; import org.chorem.pollen.votecounting.model.ChoiceScore; import org.chorem.pollen.votecounting.model.VoteCountingResult; +import org.chorem.pollen.votecounting.model.VoteForChoice; import org.chorem.pollen.votecounting.model.Voter; import java.math.BigDecimal; @@ -74,6 +75,21 @@ public class CoombsVoteCountingStrategy extends AbstractVoteCountingStrategy { return orderByValues(scores.values()); } + @Override + public Set<VoteForChoice> toVoteForChoices(VoteCountingResult voteCountingResult) { + Set<VoteForChoice> voteForChoices = Sets.newHashSet(); + + for (ChoiceScore choiceScore : voteCountingResult.getScores()) { + + double score = choiceScore.getScoreOrder(); + VoteForChoice voteForChoice = VoteForChoice.newVote( + choiceScore.getChoiceId(), + score); + voteForChoices.add(voteForChoice); + } + return voteForChoices; + } + protected void round(Map<Voter, List<Set<String>>> topRankChoices, Set<String> idsToExclude, Set<String> idsToInclude, diff --git a/pollen-votecounting-coombs/src/test/java/org/chorem/pollen/votecounting/CoombsVoteCountingStrategyTest.java b/pollen-votecounting-coombs/src/test/java/org/chorem/pollen/votecounting/CoombsVoteCountingStrategyTest.java index 8cee2e7..22b88c4 100644 --- a/pollen-votecounting-coombs/src/test/java/org/chorem/pollen/votecounting/CoombsVoteCountingStrategyTest.java +++ b/pollen-votecounting-coombs/src/test/java/org/chorem/pollen/votecounting/CoombsVoteCountingStrategyTest.java @@ -20,9 +20,6 @@ */ package org.chorem.pollen.votecounting; -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; -import org.chorem.pollen.votecounting.model.ChoiceIdAble; import org.chorem.pollen.votecounting.model.ChoiceScore; import org.chorem.pollen.votecounting.model.SimpleVoterBuilder; import org.chorem.pollen.votecounting.model.VoteCountingResult; @@ -34,10 +31,10 @@ import org.junit.Ignore; import org.junit.Test; import java.math.BigDecimal; -import java.util.List; -import java.util.Map; import java.util.Set; +import static org.assertj.core.api.Assertions.assertThat; + /** * Tests the {@link CoombsVoteCountingStrategy}. * @@ -106,15 +103,15 @@ public class CoombsVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(4, scores.size()); - - assertChoiceScore(scores.get(0), CHOICE_B, BigDecimal.valueOf(68.)); - assertChoiceScore(scores.get(1), CHOICE_A, BigDecimal.valueOf(42.)); - assertChoiceScore(scores.get(2), CHOICE_D, BigDecimal.valueOf(17.)); - assertChoiceScore(scores.get(3), CHOICE_C, BigDecimal.valueOf(15.)); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(42.0), 1), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(68.0), 0), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(15.0), 3), + ChoiceScore.newScore(CHOICE_D, BigDecimal.valueOf(17.0), 2)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -142,14 +139,14 @@ public class CoombsVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScore(scores.get(0), CHOICE_A, BigDecimal.valueOf(2.)); - assertChoiceScore(scores.get(1), CHOICE_C, BigDecimal.valueOf(1.)); - assertChoiceScore(scores.get(2), CHOICE_B, null); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(2.0), 0), + ChoiceScore.newScore(CHOICE_B, null, 2), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(1.0), 1)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -177,16 +174,14 @@ public class CoombsVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScoreEquals(BigDecimal.valueOf(2.), - Sets.newHashSet(scores.get(0), - scores.get(1)), - CHOICE_A, CHOICE_C); - assertChoiceScore(scores.get(2), CHOICE_B, null); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(2.0), 0), + ChoiceScore.newScore(CHOICE_B, null, 1), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(2.0), 0)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -214,17 +209,14 @@ public class CoombsVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScoreEquals(BigDecimal.valueOf(1.), - Sets.newHashSet(scores.get(0), - scores.get(1), - scores.get(2) - ), - CHOICE_A, CHOICE_B, CHOICE_C); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(1.0), 0), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(1.0), 0), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(1.0), 0)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -252,16 +244,14 @@ public class CoombsVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScoreEquals(BigDecimal.valueOf(2.), - Sets.newHashSet(scores.get(0), - scores.get(1)), - CHOICE_A, CHOICE_B); - assertChoiceScore(scores.get(2), CHOICE_C, null); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(2.0), 0), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(2.0), 0), + ChoiceScore.newScore(CHOICE_C, null, 1)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -289,14 +279,14 @@ public class CoombsVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScore(scores.get(0), CHOICE_C, BigDecimal.valueOf(3.)); - assertChoiceScore(scores.get(1), CHOICE_A, BigDecimal.valueOf(2.)); - assertChoiceScore(scores.get(2), CHOICE_B, BigDecimal.valueOf(1.)); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(2.0), 1), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(1.0), 2), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(3.0), 0)) + .isSortedAccordingTo(ChoiceScore::compareTo); } public static void assertChoiceScore(ChoiceScore choiceScore, @@ -307,19 +297,4 @@ public class CoombsVoteCountingStrategyTest { Assert.assertEquals(choiceResult, choiceScore.getScoreValue()); } - public static void assertChoiceScoreEquals(BigDecimal choiceResult, - Set<ChoiceScore> choiceScores, - String... choiceIds) { - - Assert.assertNotNull(choiceScores); - Map<String, ChoiceScore> choicesById = Maps.uniqueIndex( - choiceScores, new ChoiceIdAble.ChoiceIdAbleById()::apply); - for (String choiceId : choiceIds) { - - ChoiceScore choiceScore = choicesById.get(choiceId); - Assert.assertNotNull(choiceScore); - assertChoiceScore(choiceScore, choiceId, choiceResult); - } - } - } diff --git a/pollen-votecounting-instant-runoff/pom.xml b/pollen-votecounting-instant-runoff/pom.xml index 10e24b8..da20a39 100644 --- a/pollen-votecounting-instant-runoff/pom.xml +++ b/pollen-votecounting-instant-runoff/pom.xml @@ -70,6 +70,11 @@ <artifactId>junit</artifactId> </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + </dependency> + </dependencies> <build> diff --git a/pollen-votecounting-instant-runoff/src/main/java/org/chorem/pollen/votecounting/InstantRunoffVoteCountingStrategy.java b/pollen-votecounting-instant-runoff/src/main/java/org/chorem/pollen/votecounting/InstantRunoffVoteCountingStrategy.java index 194c7a5..e865104 100644 --- a/pollen-votecounting-instant-runoff/src/main/java/org/chorem/pollen/votecounting/InstantRunoffVoteCountingStrategy.java +++ b/pollen-votecounting-instant-runoff/src/main/java/org/chorem/pollen/votecounting/InstantRunoffVoteCountingStrategy.java @@ -25,6 +25,7 @@ import com.google.common.collect.Sets; import org.apache.commons.collections4.CollectionUtils; import org.chorem.pollen.votecounting.model.ChoiceScore; import org.chorem.pollen.votecounting.model.VoteCountingResult; +import org.chorem.pollen.votecounting.model.VoteForChoice; import org.chorem.pollen.votecounting.model.Voter; import java.math.BigDecimal; @@ -73,6 +74,21 @@ public class InstantRunoffVoteCountingStrategy extends AbstractVoteCountingStrat return orderByValues(scores.values()); } + @Override + public Set<VoteForChoice> toVoteForChoices(VoteCountingResult voteCountingResult) { + Set<VoteForChoice> voteForChoices = Sets.newHashSet(); + + for (ChoiceScore choiceScore : voteCountingResult.getScores()) { + + double score = choiceScore.getScoreOrder(); + VoteForChoice voteForChoice = VoteForChoice.newVote( + choiceScore.getChoiceId(), + score); + voteForChoices.add(voteForChoice); + } + return voteForChoices; + } + protected void round(Map<Voter, List<Set<String>>> topRankChoices, Set<String> idsToExclude, Set<String> idsToInclude, diff --git a/pollen-votecounting-instant-runoff/src/test/java/org/chorem/pollen/votecounting/InstantRunoffVoteCountingStrategyTest.java b/pollen-votecounting-instant-runoff/src/test/java/org/chorem/pollen/votecounting/InstantRunoffVoteCountingStrategyTest.java index c593313..f3ae2fe 100644 --- a/pollen-votecounting-instant-runoff/src/test/java/org/chorem/pollen/votecounting/InstantRunoffVoteCountingStrategyTest.java +++ b/pollen-votecounting-instant-runoff/src/test/java/org/chorem/pollen/votecounting/InstantRunoffVoteCountingStrategyTest.java @@ -20,24 +20,20 @@ */ package org.chorem.pollen.votecounting; -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; -import org.chorem.pollen.votecounting.model.ChoiceIdAble; import org.chorem.pollen.votecounting.model.ChoiceScore; import org.chorem.pollen.votecounting.model.SimpleVoterBuilder; import org.chorem.pollen.votecounting.model.VoteCountingResult; import org.chorem.pollen.votecounting.model.Voter; -import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; import java.math.BigDecimal; -import java.util.List; -import java.util.Map; import java.util.Set; +import static org.assertj.core.api.Assertions.assertThat; + /** * Tests the {@link InstantRunoffVoteCountingStrategy}. * @@ -106,15 +102,15 @@ public class InstantRunoffVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(4, scores.size()); - - assertChoiceScore(scores.get(0), CHOICE_D, BigDecimal.valueOf(58.)); - assertChoiceScore(scores.get(1), CHOICE_A, BigDecimal.valueOf(42.)); - assertChoiceScore(scores.get(2), CHOICE_B, BigDecimal.valueOf(26.)); - assertChoiceScore(scores.get(3), CHOICE_C, BigDecimal.valueOf(15.)); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(42.0), 1), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(26.0), 2), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(15.0), 3), + ChoiceScore.newScore(CHOICE_D, BigDecimal.valueOf(58.0), 0)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -142,14 +138,14 @@ public class InstantRunoffVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScore(scores.get(0), CHOICE_A, BigDecimal.valueOf(2.)); - assertChoiceScore(scores.get(1), CHOICE_C, BigDecimal.valueOf(1.)); - assertChoiceScore(scores.get(2), CHOICE_B, null); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(2.0), 0), + ChoiceScore.newScore(CHOICE_B, null, 2), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(1.0), 1)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -177,16 +173,14 @@ public class InstantRunoffVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScoreEquals(BigDecimal.valueOf(2.), - Sets.newHashSet(scores.get(0), - scores.get(1)), - CHOICE_A, CHOICE_C); - assertChoiceScore(scores.get(2), CHOICE_B, null); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(2.0), 0), + ChoiceScore.newScore(CHOICE_B, null, 1), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(2.0), 0)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -214,17 +208,14 @@ public class InstantRunoffVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScoreEquals(BigDecimal.valueOf(1.), - Sets.newHashSet(scores.get(0), - scores.get(1), - scores.get(2) - ), - CHOICE_A, CHOICE_B, CHOICE_C); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(1.0), 0), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(1.0), 0), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(1.0), 0)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -252,16 +243,14 @@ public class InstantRunoffVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScoreEquals(BigDecimal.valueOf(2.), - Sets.newHashSet(scores.get(0), - scores.get(1)), - CHOICE_A, CHOICE_B); - assertChoiceScore(scores.get(2), CHOICE_C, null); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(2.0), 0), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(2.0), 0), + ChoiceScore.newScore(CHOICE_C, null, 1)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -289,37 +278,14 @@ public class InstantRunoffVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScore(scores.get(0), CHOICE_C, BigDecimal.valueOf(3.)); - assertChoiceScore(scores.get(1), CHOICE_A, BigDecimal.valueOf(2.)); - assertChoiceScore(scores.get(2), CHOICE_B, BigDecimal.valueOf(1.)); - } - - public static void assertChoiceScore(ChoiceScore choiceScore, - String choiceId, - BigDecimal choiceResult) { - Assert.assertNotNull(choiceScore); - Assert.assertEquals(choiceId, choiceScore.getChoiceId()); - Assert.assertEquals(choiceResult, choiceScore.getScoreValue()); - } - - public static void assertChoiceScoreEquals(BigDecimal choiceResult, - Set<ChoiceScore> choiceScores, - String... choiceIds) { - - Assert.assertNotNull(choiceScores); - Map<String, ChoiceScore> choicesById = Maps.uniqueIndex( - choiceScores, new ChoiceIdAble.ChoiceIdAbleById()::apply); - for (String choiceId : choiceIds) { - - ChoiceScore choiceScore = choicesById.get(choiceId); - Assert.assertNotNull(choiceScore); - assertChoiceScore(choiceScore, choiceId, choiceResult); - } + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(2.0), 1), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(1.0), 2), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(3.0), 0)) + .isSortedAccordingTo(ChoiceScore::compareTo); } } diff --git a/pollen-votecounting-normal/pom.xml b/pollen-votecounting-normal/pom.xml index 16d0ccf..68bc849 100644 --- a/pollen-votecounting-normal/pom.xml +++ b/pollen-votecounting-normal/pom.xml @@ -65,6 +65,12 @@ <artifactId>junit</artifactId> </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + </dependency> + + </dependencies> <build> diff --git a/pollen-votecounting-normal/src/main/java/org/chorem/pollen/votecounting/NormalVoteCountingStrategy.java b/pollen-votecounting-normal/src/main/java/org/chorem/pollen/votecounting/NormalVoteCountingStrategy.java index 404348b..2a23256 100644 --- a/pollen-votecounting-normal/src/main/java/org/chorem/pollen/votecounting/NormalVoteCountingStrategy.java +++ b/pollen-votecounting-normal/src/main/java/org/chorem/pollen/votecounting/NormalVoteCountingStrategy.java @@ -20,11 +20,13 @@ */ package org.chorem.pollen.votecounting; +import com.google.common.collect.Sets; import org.chorem.pollen.votecounting.model.ChoiceScore; import org.chorem.pollen.votecounting.model.VoteCountingResult; import org.chorem.pollen.votecounting.model.VoteForChoice; import org.chorem.pollen.votecounting.model.Voter; +import java.util.Collection; import java.util.Map; import java.util.Set; @@ -75,4 +77,20 @@ public class NormalVoteCountingStrategy extends AbstractVoteCountingStrategy { } } + @Override + public Set<VoteForChoice> toVoteForChoices(VoteCountingResult voteCountingResult) { + Set<VoteForChoice> voteForChoices = Sets.newHashSet(); + + Collection<ChoiceScore> winners = voteCountingResult.getTopRanking(); + for (ChoiceScore choiceScore : voteCountingResult.getScores()) { + + double score = winners.contains(choiceScore) ? 1d : 0d; + VoteForChoice voteForChoice = VoteForChoice.newVote( + choiceScore.getChoiceId(), + score); + voteForChoices.add(voteForChoice); + } + return voteForChoices; + } + } diff --git a/pollen-votecounting-normal/src/test/java/org/chorem/pollen/votecounting/NormalVoteCountingStrategyTest.java b/pollen-votecounting-normal/src/test/java/org/chorem/pollen/votecounting/NormalVoteCountingStrategyTest.java index b78cb3e..918b166 100644 --- a/pollen-votecounting-normal/src/test/java/org/chorem/pollen/votecounting/NormalVoteCountingStrategyTest.java +++ b/pollen-votecounting-normal/src/test/java/org/chorem/pollen/votecounting/NormalVoteCountingStrategyTest.java @@ -20,27 +20,24 @@ */ package org.chorem.pollen.votecounting; -import com.google.common.collect.Maps; import com.google.common.collect.Sets; -import org.chorem.pollen.votecounting.model.ChoiceIdAble; import org.chorem.pollen.votecounting.model.ChoiceScore; -import org.chorem.pollen.votecounting.model.GroupOfVoter; -import org.chorem.pollen.votecounting.model.GroupOfVoterBuilder; -import org.chorem.pollen.votecounting.model.GroupVoteCountingResult; +import org.chorem.pollen.votecounting.model.ListOfVoter; +import org.chorem.pollen.votecounting.model.ListVoteCountingResult; +import org.chorem.pollen.votecounting.model.SimpleVoter; import org.chorem.pollen.votecounting.model.SimpleVoterBuilder; import org.chorem.pollen.votecounting.model.VoteCountingResult; +import org.chorem.pollen.votecounting.model.VoteForChoice; import org.chorem.pollen.votecounting.model.Voter; -import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; -import org.junit.Ignore; import org.junit.Test; import java.math.BigDecimal; -import java.util.List; -import java.util.Map; import java.util.Set; +import static org.assertj.core.api.Assertions.assertThat; + /** * Tests the {@link NormalVoteCountingStrategy}. * @@ -96,80 +93,75 @@ public class NormalVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScore(scores.get(0), CHOICE_B, BigDecimal.valueOf(2.), 0); - - assertChoiceScoreEquals(BigDecimal.valueOf(1.), 1, - Sets.newHashSet(scores.get(1), - scores.get(2)), - CHOICE_A, CHOICE_C); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(1.0), 1), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(2.0), 0), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(1.0), 1)) + .isSortedAccordingTo(ChoiceScore::compareTo); } - //TODO Finish me - @Ignore @Test - public void groupVotecount() throws Exception { + public void listVotecount() throws Exception { // Group poll (all weight to 1) - // G1U1 (a=1 b=null c=null) - // G1U2 (a=null b=1 c=null) - // G1U3 (a=null b=1 c=1) - // Result G1 (a=1 b=2 c=1) - // G2U1 (a=null b=1 c=null) - // G2U2 (a=1 b=null c=null) - // G2U3 (a=null b=1 c=1) - // Result G2 (a=1 b=2 c=1) - // Result (a=1 b=2 c=1) - - GroupOfVoter voters = new GroupOfVoterBuilder(). - newGroupVoter("G1", 1.). - newVoter("G1U1", 1.). - addVoteForChoice(CHOICE_A, 1.). - addVoteForChoice(CHOICE_B, null). - addVoteForChoice(CHOICE_C, null). - newVoter("G1U2", 2.). - addVoteForChoice(CHOICE_A, null). - addVoteForChoice(CHOICE_B, 1.). - addVoteForChoice(CHOICE_C, null). - newVoter("G1U3", 3.). - addVoteForChoice(CHOICE_A, null). - addVoteForChoice(CHOICE_B, 1.). - addVoteForChoice(CHOICE_C, 1.). - newGroupVoter("G2", 1.). - newVoter("G2U1", 1.). - addVoteForChoice(CHOICE_A, null). - addVoteForChoice(CHOICE_B, 1.0). - addVoteForChoice(CHOICE_C, null). - newVoter("G2U2", 1.). - addVoteForChoice(CHOICE_A, 1.0). - addVoteForChoice(CHOICE_B, null). - addVoteForChoice(CHOICE_C, null). - newVoter("G2U3", 1.). - addVoteForChoice(CHOICE_A, null). - addVoteForChoice(CHOICE_B, 1.). - addVoteForChoice(CHOICE_C, 1.). - getRoot(); - - GroupVoteCountingResult result = strategy.votecount(voters); - - Assert.assertNotNull(result); - VoteCountingResult mainResult = result.getMainResult(); - Assert.assertNotNull(mainResult); - - List<ChoiceScore> scores = mainResult.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScore(scores.get(0), CHOICE_B, BigDecimal.valueOf(2.), 0); - - assertChoiceScoreEquals(BigDecimal.valueOf(1.), 1, - Sets.newHashSet(scores.get(1), - scores.get(2)), - CHOICE_A, CHOICE_C); + + // G1U1 a * 2 + // G1U2 a + // G1U3 b + // Result G1 a => 2 b => 1 c => 0 + // Vote G1 a + + // G2U1 a * 2 + // G2U2 c + // G2U3 c + // Result G2 a => 2 b => 0 c => 2 + // Vote G1 a, c * 2 + + // Result a => 3 b => 1 c => 2 + + ListOfVoter voters = ListOfVoter.newVoter(null, 1, Sets.newHashSet( + ListOfVoter.newVoter("G1", 1, Sets.newHashSet( + SimpleVoter.newVoter("G1U1", 2, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 1d), + VoteForChoice.newVote(CHOICE_B, 0d), + VoteForChoice.newVote(CHOICE_C, 0d))), + SimpleVoter.newVoter("G1U2", 1, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 1d), + VoteForChoice.newVote(CHOICE_B, 0d), + VoteForChoice.newVote(CHOICE_C, 0d))), + SimpleVoter.newVoter("G1U3", 1, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 0d), + VoteForChoice.newVote(CHOICE_B, 1d), + VoteForChoice.newVote(CHOICE_C, 0d))))), + ListOfVoter.newVoter("G2", 2, Sets.newHashSet( + SimpleVoter.newVoter("G2U1", 2, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 1d), + VoteForChoice.newVote(CHOICE_B, 0d), + VoteForChoice.newVote(CHOICE_C, 0d))), + SimpleVoter.newVoter("G2U2", 1, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 0d), + VoteForChoice.newVote(CHOICE_B, 0d), + VoteForChoice.newVote(CHOICE_C, 1d))), + SimpleVoter.newVoter("G2U3", 1, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 0d), + VoteForChoice.newVote(CHOICE_B, 0d), + VoteForChoice.newVote(CHOICE_C, 1d))))) + )); + + ListVoteCountingResult result = strategy.votecount(voters); + + assertThat(result) + .isNotNull(); + assertThat(result.getMainResult()).isNotNull(); + assertThat(result.getMainResult().getScores()).isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(3.0), 0), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(0.0), 2), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(2.0), 1)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -197,14 +189,15 @@ public class NormalVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(3.0), 0), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(2.0), 1), + ChoiceScore.newScore(CHOICE_C, null, 2)) + .isSortedAccordingTo(ChoiceScore::compareTo); - assertChoiceScore(scores.get(0), CHOICE_A, BigDecimal.valueOf(3.), 0); - assertChoiceScore(scores.get(1), CHOICE_B, BigDecimal.valueOf(2.), 1); - assertChoiceScore(scores.get(2), CHOICE_C, null, 2); } @Test @@ -232,16 +225,14 @@ public class NormalVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScoreEquals(BigDecimal.valueOf(3.), 0, - Sets.newHashSet(scores.get(0), - scores.get(1)), - CHOICE_A, CHOICE_B); - assertChoiceScore(scores.get(2), CHOICE_C, null, 1); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(3.0), 0), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(3.0), 0), + ChoiceScore.newScore(CHOICE_C, null, 1)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -269,16 +260,14 @@ public class NormalVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScoreEquals(BigDecimal.valueOf(2.), 0, - Sets.newHashSet(scores.get(0), - scores.get(1)), - CHOICE_A, CHOICE_B); - assertChoiceScore(scores.get(2), CHOICE_C, BigDecimal.valueOf(1.), 1); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(2.0), 0), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(2.0), 0), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(1.0), 1)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -306,40 +295,14 @@ public class NormalVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScore(scores.get(0), CHOICE_B, BigDecimal.valueOf(4.), 0); - assertChoiceScore(scores.get(1), CHOICE_C, BigDecimal.valueOf(3.), 1); - assertChoiceScore(scores.get(2), CHOICE_A, BigDecimal.valueOf(2.), 2); - } - - public static void assertChoiceScore(ChoiceScore choiceScore, - String choiceId, - BigDecimal choiceResult, - int rank) { - Assert.assertNotNull(choiceScore); - Assert.assertEquals(choiceId, choiceScore.getChoiceId()); - Assert.assertEquals(choiceResult, choiceScore.getScoreValue()); - Assert.assertEquals(rank, choiceScore.getScoreOrder()); - } - - public static void assertChoiceScoreEquals(BigDecimal choiceResult, - int rank, - Set<ChoiceScore> choiceScores, - String... choiceIds) { - - Assert.assertNotNull(choiceScores); - Map<String, ChoiceScore> choicesById = Maps.uniqueIndex( - choiceScores, new ChoiceIdAble.ChoiceIdAbleById()::apply); - for (String choiceId : choiceIds) { - - ChoiceScore choiceScore = choicesById.get(choiceId); - Assert.assertNotNull(choiceScore); - assertChoiceScore(choiceScore, choiceId, choiceResult, rank); - } + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(2.0), 2), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(4.0), 0), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(3.0), 1)) + .isSortedAccordingTo(ChoiceScore::compareTo); } } diff --git a/pollen-votecounting-number/pom.xml b/pollen-votecounting-number/pom.xml index 0f301a9..3d79522 100644 --- a/pollen-votecounting-number/pom.xml +++ b/pollen-votecounting-number/pom.xml @@ -65,6 +65,11 @@ <artifactId>junit</artifactId> </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + </dependency> + </dependencies> <build> diff --git a/pollen-votecounting-number/src/main/java/org/chorem/pollen/votecounting/NumberVoteCountingStrategy.java b/pollen-votecounting-number/src/main/java/org/chorem/pollen/votecounting/NumberVoteCountingStrategy.java index 1c0484f..bde7d21 100644 --- a/pollen-votecounting-number/src/main/java/org/chorem/pollen/votecounting/NumberVoteCountingStrategy.java +++ b/pollen-votecounting-number/src/main/java/org/chorem/pollen/votecounting/NumberVoteCountingStrategy.java @@ -20,6 +20,7 @@ */ package org.chorem.pollen.votecounting; +import com.google.common.collect.Sets; import org.chorem.pollen.votecounting.model.ChoiceScore; import org.chorem.pollen.votecounting.model.VoteCountingResult; import org.chorem.pollen.votecounting.model.VoteForChoice; @@ -52,6 +53,21 @@ public class NumberVoteCountingStrategy extends AbstractVoteCountingStrategy { return orderByValues(scores.values()); } + @Override + public Set<VoteForChoice> toVoteForChoices(VoteCountingResult voteCountingResult) { + Set<VoteForChoice> voteForChoices = Sets.newHashSet(); + + for (ChoiceScore choiceScore : voteCountingResult.getScores()) { + + double score = choiceScore.getScoreValue().doubleValue(); + VoteForChoice voteForChoice = VoteForChoice.newVote( + choiceScore.getChoiceId(), + score); + voteForChoices.add(voteForChoice); + } + return voteForChoices; + } + protected void addVoterChoices(Voter voter, Map<String, ChoiceScore> scores) { diff --git a/pollen-votecounting-number/src/test/java/org/chorem/pollen/votecounting/NumberVoteCountingStrategyTest.java b/pollen-votecounting-number/src/test/java/org/chorem/pollen/votecounting/NumberVoteCountingStrategyTest.java index 1222013..7706453 100644 --- a/pollen-votecounting-number/src/test/java/org/chorem/pollen/votecounting/NumberVoteCountingStrategyTest.java +++ b/pollen-votecounting-number/src/test/java/org/chorem/pollen/votecounting/NumberVoteCountingStrategyTest.java @@ -20,23 +20,24 @@ */ package org.chorem.pollen.votecounting; -import com.google.common.collect.Maps; import com.google.common.collect.Sets; -import org.chorem.pollen.votecounting.model.ChoiceIdAble; import org.chorem.pollen.votecounting.model.ChoiceScore; +import org.chorem.pollen.votecounting.model.ListOfVoter; +import org.chorem.pollen.votecounting.model.ListVoteCountingResult; +import org.chorem.pollen.votecounting.model.SimpleVoter; import org.chorem.pollen.votecounting.model.SimpleVoterBuilder; import org.chorem.pollen.votecounting.model.VoteCountingResult; +import org.chorem.pollen.votecounting.model.VoteForChoice; import org.chorem.pollen.votecounting.model.Voter; -import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import java.math.BigDecimal; -import java.util.List; -import java.util.Map; import java.util.Set; +import static org.assertj.core.api.Assertions.assertThat; + /** * Tests the {@link NumberVoteCountingStrategy}. * @@ -92,14 +93,14 @@ public class NumberVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScore(scores.get(0), CHOICE_C, BigDecimal.valueOf(23.)); - assertChoiceScore(scores.get(1), CHOICE_B, BigDecimal.valueOf(14.)); - assertChoiceScore(scores.get(2), CHOICE_A, BigDecimal.valueOf(10.)); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(10.0), 2), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(14.0), 1), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(23.0), 0)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -127,14 +128,14 @@ public class NumberVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScore(scores.get(0), CHOICE_A, BigDecimal.valueOf(3.)); - assertChoiceScore(scores.get(1), CHOICE_B, BigDecimal.valueOf(2.)); - assertChoiceScore(scores.get(2), CHOICE_C, null); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(3.0), 0), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(2.0), 1), + ChoiceScore.newScore(CHOICE_C, null, 2)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -162,16 +163,14 @@ public class NumberVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScoreEquals(BigDecimal.valueOf(3.), - Sets.newHashSet(scores.get(0), - scores.get(1)), - CHOICE_A, CHOICE_B); - assertChoiceScore(scores.get(2), CHOICE_C, null); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(3.0), 0), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(3.0), 0), + ChoiceScore.newScore(CHOICE_C, null, 1)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -199,16 +198,14 @@ public class NumberVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScoreEquals(BigDecimal.valueOf(2.), - Sets.newHashSet(scores.get(0), - scores.get(1)), - CHOICE_A, CHOICE_B); - assertChoiceScore(scores.get(2), CHOICE_C, BigDecimal.valueOf(1.)); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(2.0), 0), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(2.0), 0), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(1.0), 1)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -236,37 +233,73 @@ public class NumberVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScore(scores.get(0), CHOICE_B, BigDecimal.valueOf(4.)); - assertChoiceScore(scores.get(1), CHOICE_C, BigDecimal.valueOf(3.)); - assertChoiceScore(scores.get(2), CHOICE_A, BigDecimal.valueOf(2.)); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(2.0), 2), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(4.0), 0), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(3.0), 1)) + .isSortedAccordingTo(ChoiceScore::compareTo); } - public static void assertChoiceScore(ChoiceScore choiceScore, - String choiceId, - BigDecimal choiceResult) { - Assert.assertNotNull(choiceScore); - Assert.assertEquals(choiceId, choiceScore.getChoiceId()); - Assert.assertEquals(choiceResult, choiceScore.getScoreValue()); - } - - public static void assertChoiceScoreEquals(BigDecimal choiceResult, - Set<ChoiceScore> choiceScores, - String... choiceIds) { - - Assert.assertNotNull(choiceScores); - Map<String, ChoiceScore> choicesById = Maps.uniqueIndex( - choiceScores, new ChoiceIdAble.ChoiceIdAbleById()::apply); - for (String choiceId : choiceIds) { - - ChoiceScore choiceScore = choicesById.get(choiceId); - Assert.assertNotNull(choiceScore); - assertChoiceScore(choiceScore, choiceId, choiceResult); - } + @Test + public void listVotecount() throws Exception { + // A B C + // G1U1 10 20 5 * 2 + // G1U2 0 7 19 + // G1U3 3 2 1 + // Result G1 23 51 30 + // Vote G1 23 51 30 + + // G2U1 10 20 5 * 2 + // G2U2 13 11 7 + // G2U3 23 27 17 + // Result G2 56 80 34 + // Vote G2 56 80 34 * 2 + + // Result 135 211 98 + + ListOfVoter voters = ListOfVoter.newVoter(null, 1, Sets.newHashSet( + ListOfVoter.newVoter("G1", 1, Sets.newHashSet( + SimpleVoter.newVoter("G1U1", 2, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 10d), + VoteForChoice.newVote(CHOICE_B, 21d), + VoteForChoice.newVote(CHOICE_C, 5d))), + SimpleVoter.newVoter("G1U2", 1, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 0d), + VoteForChoice.newVote(CHOICE_B, 7d), + VoteForChoice.newVote(CHOICE_C, 19d))), + SimpleVoter.newVoter("G1U3", 1, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 3d), + VoteForChoice.newVote(CHOICE_B, 2d), + VoteForChoice.newVote(CHOICE_C, 1d))))), + ListOfVoter.newVoter("G2", 2, Sets.newHashSet( + SimpleVoter.newVoter("G2U1", 2, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 10d), + VoteForChoice.newVote(CHOICE_B, 21d), + VoteForChoice.newVote(CHOICE_C, 5d))), + SimpleVoter.newVoter("G2U2", 1, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 13d), + VoteForChoice.newVote(CHOICE_B, 11d), + VoteForChoice.newVote(CHOICE_C, 7d))), + SimpleVoter.newVoter("G2U3", 1, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 23d), + VoteForChoice.newVote(CHOICE_B, 27d), + VoteForChoice.newVote(CHOICE_C, 17d))))) + )); + + ListVoteCountingResult result = strategy.votecount(voters); + + assertThat(result) + .isNotNull(); + assertThat(result.getMainResult()).isNotNull(); + assertThat(result.getMainResult().getScores()).isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(135.0), 1), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(211.0), 0), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(98.0), 2)) + .isSortedAccordingTo(ChoiceScore::compareTo); } } diff --git a/pollen-votecounting-percentage/pom.xml b/pollen-votecounting-percentage/pom.xml index 79c8dd8..4f32de4 100644 --- a/pollen-votecounting-percentage/pom.xml +++ b/pollen-votecounting-percentage/pom.xml @@ -65,6 +65,11 @@ <artifactId>junit</artifactId> </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + </dependency> + </dependencies> <build> diff --git a/pollen-votecounting-percentage/src/main/java/org/chorem/pollen/votecounting/PercentageVoteCountingStrategy.java b/pollen-votecounting-percentage/src/main/java/org/chorem/pollen/votecounting/PercentageVoteCountingStrategy.java index 1efb888..eaf90a2 100644 --- a/pollen-votecounting-percentage/src/main/java/org/chorem/pollen/votecounting/PercentageVoteCountingStrategy.java +++ b/pollen-votecounting-percentage/src/main/java/org/chorem/pollen/votecounting/PercentageVoteCountingStrategy.java @@ -20,11 +20,13 @@ */ package org.chorem.pollen.votecounting; +import com.google.common.collect.Sets; import org.chorem.pollen.votecounting.model.ChoiceScore; import org.chorem.pollen.votecounting.model.VoteCountingResult; import org.chorem.pollen.votecounting.model.VoteForChoice; import org.chorem.pollen.votecounting.model.Voter; +import java.math.BigDecimal; import java.util.Map; import java.util.Set; @@ -42,12 +44,21 @@ public class PercentageVoteCountingStrategy extends AbstractVoteCountingStrategy // get empty result by choice Map<String, ChoiceScore> scores = newEmptyChoiceScoreMap(voters); + BigDecimal weightSum = BigDecimal.ZERO; for (Voter voter : voters) { + weightSum = weightSum.add(new BigDecimal(voter.getWeight())); // add this voter votes to result addVoterChoices(voter, scores); } + for (ChoiceScore choiceScore : scores.values()) { + BigDecimal scoreValue = choiceScore.getScoreValue(); + if (scoreValue != null) { + choiceScore.setScoreValue(scoreValue.divide(weightSum, BigDecimal.ROUND_HALF_UP)); + } + } + // order scores (using their value) and return result return orderByValues(scores.values()); } @@ -73,4 +84,21 @@ public class PercentageVoteCountingStrategy extends AbstractVoteCountingStrategy } } } + + @Override + public Set<VoteForChoice> toVoteForChoices(VoteCountingResult voteCountingResult) { + Set<VoteForChoice> voteForChoices = Sets.newHashSet(); + + for (ChoiceScore choiceScore : voteCountingResult.getScores()) { + + double score = choiceScore.getScoreValue().doubleValue(); + VoteForChoice voteForChoice = VoteForChoice.newVote( + choiceScore.getChoiceId(), + score); + voteForChoices.add(voteForChoice); + } + return voteForChoices; + } + + } diff --git a/pollen-votecounting-percentage/src/test/java/org/chorem/pollen/votecounting/PercentageVoteCountingStrategyTest.java b/pollen-votecounting-percentage/src/test/java/org/chorem/pollen/votecounting/PercentageVoteCountingStrategyTest.java index f8d2df8..bcb8856 100644 --- a/pollen-votecounting-percentage/src/test/java/org/chorem/pollen/votecounting/PercentageVoteCountingStrategyTest.java +++ b/pollen-votecounting-percentage/src/test/java/org/chorem/pollen/votecounting/PercentageVoteCountingStrategyTest.java @@ -20,23 +20,24 @@ */ package org.chorem.pollen.votecounting; -import com.google.common.collect.Maps; import com.google.common.collect.Sets; -import org.chorem.pollen.votecounting.model.ChoiceIdAble; import org.chorem.pollen.votecounting.model.ChoiceScore; +import org.chorem.pollen.votecounting.model.ListOfVoter; +import org.chorem.pollen.votecounting.model.ListVoteCountingResult; +import org.chorem.pollen.votecounting.model.SimpleVoter; import org.chorem.pollen.votecounting.model.SimpleVoterBuilder; import org.chorem.pollen.votecounting.model.VoteCountingResult; +import org.chorem.pollen.votecounting.model.VoteForChoice; import org.chorem.pollen.votecounting.model.Voter; -import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import java.math.BigDecimal; -import java.util.List; -import java.util.Map; import java.util.Set; +import static org.assertj.core.api.Assertions.assertThat; + /** * Tests the {@link PercentageVoteCountingStrategy}. * @@ -74,7 +75,7 @@ public class PercentageVoteCountingStrategyTest { // 1 (a=100 b=null c=null) // 2 (a=null b=100 c=null) // 3 (a=50 b=50 c=null) - // Result (a=100 b=150 c=null) + // Result (a=50 b=50 c=null) Set<Voter> voters = new SimpleVoterBuilder(). newVoter("1", 1.). @@ -93,27 +94,24 @@ public class PercentageVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScoreEquals(BigDecimal.valueOf(150.), - Sets.newHashSet(scores.get(0), - scores.get(1)), - CHOICE_A, CHOICE_B); - - assertChoiceScore(scores.get(2), CHOICE_C, null); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(50.0), 0), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(50.0), 0), + ChoiceScore.newScore(CHOICE_C, null, 1)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test public void simpleVotecount2() throws Exception { // Simple poll (all weight to 1) - // 1 (a=20 b=30 c=50) - // 2 (a=50 b=20 c=30) - // 3 (a=10 b=50 c=40) - // Result (a=80 b=100 c=120) + // 1 (a=20 b=30 c=50) + // 2 (a=50 b=20 c=30) + // 3 (a=10 b=50 c=40) + // Result (a=26.67 b=33.33 c=40) Set<Voter> voters = new SimpleVoterBuilder(). newVoter("1", 1.). addVoteForChoice(CHOICE_A, 20.). @@ -131,14 +129,14 @@ public class PercentageVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScore(scores.get(0), CHOICE_C, BigDecimal.valueOf(120.)); - assertChoiceScore(scores.get(1), CHOICE_B, BigDecimal.valueOf(100.)); - assertChoiceScore(scores.get(2), CHOICE_A, BigDecimal.valueOf(80.)); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(26.7),2), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(33.3),1), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(40.0), 0)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -148,7 +146,7 @@ public class PercentageVoteCountingStrategyTest { // 1 (a=50 b=50 c=null) // 2 (a=50 b=50 c=null) // 3 (a=50 b=50 c=null) - // Result (a=150 b=150 c=null) + // Result (a=50 b=50 c=null) Set<Voter> voters = new SimpleVoterBuilder(). newVoter("1", 1.). addVoteForChoice(CHOICE_A, 50.). @@ -166,16 +164,14 @@ public class PercentageVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScoreEquals(BigDecimal.valueOf(150.), - Sets.newHashSet(scores.get(0), - scores.get(1)), - CHOICE_A, CHOICE_B); - assertChoiceScore(scores.get(2), CHOICE_C, null); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(50.0),0), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(50.0),0), + ChoiceScore.newScore(CHOICE_C, null, 1)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -185,7 +181,7 @@ public class PercentageVoteCountingStrategyTest { // 1 (x2) (a=100 b=null c=null) // 2 (x1) (a=null b=100 c=null) // 3 (x1) (a=null b=50 c=50) - // Result (a=200 b=150 c=50) + // Result (a=50 b=37.5 c=12.5) Set<Voter> voters = new SimpleVoterBuilder(). newVoter("1", 2.). addVoteForChoice(CHOICE_A, 100.). @@ -203,13 +199,14 @@ public class PercentageVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - assertChoiceScore(scores.get(0), CHOICE_A, BigDecimal.valueOf(200.)); - assertChoiceScore(scores.get(1), CHOICE_B, BigDecimal.valueOf(150.)); - assertChoiceScore(scores.get(2), CHOICE_C, BigDecimal.valueOf(50.)); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(50.0),0), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(37.5),1), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(12.5), 2)) + .isSortedAccordingTo(ChoiceScore::compareTo); } @Test @@ -219,7 +216,7 @@ public class PercentageVoteCountingStrategyTest { // 1 (x2) (a=100 b=null c=null) // 2 (x1) (a=null b=100 c=null) // 3 (x3) (a=null b=50 c=50) - // Result (a=200 b=250 c=150) + // Result (a=33.3 b=41.7 c=25) Set<Voter> voters = new SimpleVoterBuilder(). newVoter("1", 2.). addVoteForChoice(CHOICE_A, 100.). @@ -237,37 +234,75 @@ public class PercentageVoteCountingStrategyTest { VoteCountingResult result = strategy.votecount(voters); - Assert.assertNotNull(result); - List<ChoiceScore> scores = result.getScores(); - Assert.assertNotNull(scores); - Assert.assertEquals(3, scores.size()); - - assertChoiceScore(scores.get(0), CHOICE_B, BigDecimal.valueOf(250.)); - assertChoiceScore(scores.get(1), CHOICE_A, BigDecimal.valueOf(200.)); - assertChoiceScore(scores.get(2), CHOICE_C, BigDecimal.valueOf(150.)); + assertThat(result).isNotNull(); + assertThat(result.getScores()) + .isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(33.3),1), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(41.7),0), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(25.0), 2)) + .isSortedAccordingTo(ChoiceScore::compareTo); } - public static void assertChoiceScore(ChoiceScore choiceScore, - String choiceId, - BigDecimal choiceResult) { - Assert.assertNotNull(choiceScore); - Assert.assertEquals(choiceId, choiceScore.getChoiceId()); - Assert.assertEquals(choiceResult, choiceScore.getScoreValue()); - } - - public static void assertChoiceScoreEquals(BigDecimal choiceResult, - Set<ChoiceScore> choiceScores, - String... choiceIds) { - - Assert.assertNotNull(choiceScores); - Map<String, ChoiceScore> choicesById = Maps.uniqueIndex( - choiceScores, new ChoiceIdAble.ChoiceIdAbleById()::apply); - for (String choiceId : choiceIds) { - - ChoiceScore choiceScore = choicesById.get(choiceId); - Assert.assertNotNull(choiceScore); - assertChoiceScore(choiceScore, choiceId, choiceResult); - } + @Test + public void listVotecount() throws Exception { + + // Group poll (all weight to 1) + + // G1U1 a => 60% b => 30% c => 10% * 2 + // G1U2 a => 50% b => 25% c => 25% + // G1U3 a => 00% b => 10% c => 90% + // Result G1 a => 42.50% b => 23.75% c => 33.75% + // Vote G1 a => 42.50% b => 23.75% c => 33.75% + + // G2U1 a => 60% b => 30% c => 10% * 2 + // G2U2 a => 10% b => 30% c => 60% + // G2U3 a => 20% b => 50% c => 30% + // Result G2 a => 37.50% b => 35.00% c => 27.50% + // Vote G1 a => 37.50% b => 35.00% c => 27.50% * 2 + + // Result a => 39.17% b => 31.25% c => 29.58% + + ListOfVoter voters = ListOfVoter.newVoter(null, 1, Sets.newHashSet( + ListOfVoter.newVoter("G1", 1, Sets.newHashSet( + SimpleVoter.newVoter("G1U1", 2, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 60d), + VoteForChoice.newVote(CHOICE_B, 30d), + VoteForChoice.newVote(CHOICE_C, 10d))), + SimpleVoter.newVoter("G1U2", 1, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 50d), + VoteForChoice.newVote(CHOICE_B, 25d), + VoteForChoice.newVote(CHOICE_C, 25d))), + SimpleVoter.newVoter("G1U3", 1, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 00d), + VoteForChoice.newVote(CHOICE_B, 10d), + VoteForChoice.newVote(CHOICE_C, 90d))))), + ListOfVoter.newVoter("G2", 2, Sets.newHashSet( + SimpleVoter.newVoter("G2U1", 2, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 60d), + VoteForChoice.newVote(CHOICE_B, 30d), + VoteForChoice.newVote(CHOICE_C, 10d))), + SimpleVoter.newVoter("G2U2", 1, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 10d), + VoteForChoice.newVote(CHOICE_B, 30d), + VoteForChoice.newVote(CHOICE_C, 60d))), + SimpleVoter.newVoter("G2U3", 1, Sets.newHashSet( + VoteForChoice.newVote(CHOICE_A, 20d), + VoteForChoice.newVote(CHOICE_B, 50d), + VoteForChoice.newVote(CHOICE_C, 30d))))) + )); + + ListVoteCountingResult result = strategy.votecount(voters); + + assertThat(result) + .isNotNull(); + assertThat(result.getMainResult()).isNotNull(); + assertThat(result.getMainResult().getScores()).isNotNull() + .containsExactlyInAnyOrder( + ChoiceScore.newScore(CHOICE_A, BigDecimal.valueOf(39.2), 0), + ChoiceScore.newScore(CHOICE_B, BigDecimal.valueOf(31.3), 1), + ChoiceScore.newScore(CHOICE_C, BigDecimal.valueOf(29.6), 2)) + .isSortedAccordingTo(ChoiceScore::compareTo); } } diff --git a/pom.xml b/pom.xml index 371aa49..617c8f8 100644 --- a/pom.xml +++ b/pom.xml @@ -674,6 +674,12 @@ <version>4.12</version> <scope>test</scope> </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + <version>3.7.0</version> + <scope>test</scope> + </dependency> </dependencies> </dependencyManagement> -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.
participants (1)
-
chorem.org scm