Author: tchemit Date: 2012-06-26 05:16:11 +0200 (Tue, 26 Jun 2012) New Revision: 3559 Url: http://chorem.org/repositories/revision/pollen/3559 Log: refs #590 (improve packaging + add aggregator module with all strategies) Added: trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/ChoiceToVoteRenderType.java trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/strategy/ trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/strategy/AbstractVoteCountingStrategy.java trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/strategy/VoteCountigStrategyNotFound.java trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategy.java trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProvider.java trunk/pollen-votecounting-api/src/site/ trunk/pollen-votecounting-api/src/test/java/org/chorem/pollen/votecounting/strategy/ trunk/pollen-votecounting-api/src/test/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProviderTest.java trunk/pollen-votecounting-strategy-condorcet/src/main/java/org/chorem/pollen/votecounting/strategy/ trunk/pollen-votecounting-strategy-condorcet/src/main/java/org/chorem/pollen/votecounting/strategy/CondorcetStrategy.java trunk/pollen-votecounting-strategy-condorcet/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.strategy.VoteCountingStrategy trunk/pollen-votecounting-strategy-condorcet/src/site/ trunk/pollen-votecounting-strategy-condorcet/src/test/java/org/chorem/pollen/votecounting/strategy/ trunk/pollen-votecounting-strategy-condorcet/src/test/java/org/chorem/pollen/votecounting/strategy/CondorcetStrategyTest.java trunk/pollen-votecounting-strategy-condorcet/src/test/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProviderTest.java trunk/pollen-votecounting-strategy-normal/src/main/java/org/chorem/pollen/votecounting/strategy/ trunk/pollen-votecounting-strategy-normal/src/main/java/org/chorem/pollen/votecounting/strategy/NormalStrategy.java trunk/pollen-votecounting-strategy-normal/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.strategy.VoteCountingStrategy trunk/pollen-votecounting-strategy-normal/src/test/java/org/chorem/pollen/votecounting/strategy/ trunk/pollen-votecounting-strategy-normal/src/test/java/org/chorem/pollen/votecounting/strategy/NormalStrategyTest.java trunk/pollen-votecounting-strategy-normal/src/test/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProviderTest.java trunk/pollen-votecounting-strategy-number/src/main/java/org/chorem/pollen/votecounting/strategy/ trunk/pollen-votecounting-strategy-number/src/main/java/org/chorem/pollen/votecounting/strategy/NumberStrategy.java trunk/pollen-votecounting-strategy-number/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.strategy.VoteCountingStrategy trunk/pollen-votecounting-strategy-number/src/test/java/org/chorem/pollen/votecounting/strategy/ trunk/pollen-votecounting-strategy-number/src/test/java/org/chorem/pollen/votecounting/strategy/NumberStrategyTest.java trunk/pollen-votecounting-strategy-number/src/test/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProviderTest.java trunk/pollen-votecounting-strategy-percentage/src/main/java/org/chorem/pollen/votecounting/strategy/ trunk/pollen-votecounting-strategy-percentage/src/main/java/org/chorem/pollen/votecounting/strategy/PercentageStrategy.java trunk/pollen-votecounting-strategy-percentage/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.strategy.VoteCountingStrategy trunk/pollen-votecounting-strategy-percentage/src/test/java/org/chorem/pollen/votecounting/strategy/ trunk/pollen-votecounting-strategy-percentage/src/test/java/org/chorem/pollen/votecounting/strategy/PercentageStrategyTest.java trunk/pollen-votecounting-strategy-percentage/src/test/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProviderTest.java trunk/pollen-votecounting-strategy/ trunk/pollen-votecounting-strategy/LICENSE.txt trunk/pollen-votecounting-strategy/README.txt trunk/pollen-votecounting-strategy/changelog.txt trunk/pollen-votecounting-strategy/pom.xml trunk/pollen-votecounting-strategy/src/ trunk/pollen-votecounting-strategy/src/site/ trunk/pollen-votecounting-strategy/src/site/site_en.xml trunk/pollen-votecounting-strategy/src/site/site_fr.xml Removed: trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/AbstractVoteCountingStrategy.java trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/ChoiceToVoteRenderType.java trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/VoteCountigStrategyNotFound.java trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/VoteCountingService.java trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/VoteCountingStrategy.java trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/VoteCountingStrategyProvider.java trunk/pollen-votecounting-api/src/src/site/ trunk/pollen-votecounting-api/src/test/java/org/chorem/pollen/votecounting/VoteCountingStrategyProviderTest.java trunk/pollen-votecounting-strategy-condorcet/src/main/java/org/chorem/pollen/votecounting/CondorcetStrategy.java trunk/pollen-votecounting-strategy-condorcet/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.VoteCountingStrategy trunk/pollen-votecounting-strategy-condorcet/src/src/site/ trunk/pollen-votecounting-strategy-condorcet/src/test/java/org/chorem/pollen/votecounting/CondorcetStrategyTest.java trunk/pollen-votecounting-strategy-condorcet/src/test/java/org/chorem/pollen/votecounting/VoteCountingStrategyProviderTest.java trunk/pollen-votecounting-strategy-normal/src/main/java/org/chorem/pollen/votecounting/NormalStrategy.java trunk/pollen-votecounting-strategy-normal/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.VoteCountingStrategy trunk/pollen-votecounting-strategy-normal/src/test/java/org/chorem/pollen/votecounting/NormalStrategyTest.java trunk/pollen-votecounting-strategy-normal/src/test/java/org/chorem/pollen/votecounting/VoteCountingStrategyProviderTest.java trunk/pollen-votecounting-strategy-number/src/main/java/org/chorem/pollen/votecounting/NumberStrategy.java trunk/pollen-votecounting-strategy-number/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.VoteCountingStrategy trunk/pollen-votecounting-strategy-number/src/test/java/org/chorem/pollen/votecounting/NumberStrategyTest.java trunk/pollen-votecounting-strategy-number/src/test/java/org/chorem/pollen/votecounting/VoteCountingStrategyProviderTest.java trunk/pollen-votecounting-strategy-percentage/src/main/java/org/chorem/pollen/votecounting/PercentageStrategy.java trunk/pollen-votecounting-strategy-percentage/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.VoteCountingStrategy trunk/pollen-votecounting-strategy-percentage/src/test/java/org/chorem/pollen/votecounting/PercentageStrategyTest.java trunk/pollen-votecounting-strategy-percentage/src/test/java/org/chorem/pollen/votecounting/VoteCountingStrategyProviderTest.java Modified: trunk/pollen-services/src/main/java/org/chorem/pollen/PollenApplicationContext.java trunk/pollen-services/src/main/java/org/chorem/pollen/services/DefaultPollenServiceContext.java trunk/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceContext.java trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollVoteCountingService.java trunk/pollen-services/src/test/java/org/chorem/pollen/services/FakeServiceContext.java trunk/pollen-ui-struts2/pom.xml trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/PollenApplicationListener.java trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/PollenActionSupport.java trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractPollForm.java trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractVoteAction.java trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/ResultForPoll.java trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/VoteForPoll.java Modified: trunk/pollen-services/src/main/java/org/chorem/pollen/PollenApplicationContext.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/PollenApplicationContext.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/PollenApplicationContext.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -24,7 +24,7 @@ import com.opensymphony.xwork2.ActionContext; import org.chorem.pollen.services.PollenNotifierWorker; -import org.chorem.pollen.votecounting.VoteCountingStrategyProvider; +import org.chorem.pollen.votecounting.strategy.VoteCountingStrategyProvider; import org.nuiton.topia.TopiaContext; import javax.servlet.ServletContext; Modified: trunk/pollen-services/src/main/java/org/chorem/pollen/services/DefaultPollenServiceContext.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/services/DefaultPollenServiceContext.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/DefaultPollenServiceContext.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -24,7 +24,7 @@ import com.google.common.base.Preconditions; import org.chorem.pollen.PollenConfiguration; -import org.chorem.pollen.votecounting.VoteCountingStrategyProvider; +import org.chorem.pollen.votecounting.strategy.VoteCountingStrategyProvider; import org.nuiton.topia.TopiaContext; import java.net.URL; Modified: trunk/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceContext.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceContext.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/PollenServiceContext.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -24,7 +24,7 @@ package org.chorem.pollen.services; import org.chorem.pollen.PollenConfiguration; -import org.chorem.pollen.votecounting.VoteCountingStrategyProvider; +import org.chorem.pollen.votecounting.strategy.VoteCountingStrategyProvider; import org.nuiton.topia.TopiaContext; import java.net.URL; Modified: trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollVoteCountingService.java =================================================================== --- trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollVoteCountingService.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-services/src/main/java/org/chorem/pollen/services/impl/PollVoteCountingService.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -22,11 +22,23 @@ */ package org.chorem.pollen.services.impl; +import com.google.common.base.Preconditions; +import com.google.common.collect.Maps; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.chorem.pollen.services.PollenServiceSupport; -import org.chorem.pollen.votecounting.VoteCountingService; +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.VoteCountingResult; +import org.chorem.pollen.votecounting.model.VoteForChoice; +import org.chorem.pollen.votecounting.model.Voter; +import org.chorem.pollen.votecounting.strategy.VoteCountingStrategy; +import org.chorem.pollen.votecounting.strategy.VoteCountingStrategyProvider; +import java.util.Map; +import java.util.Set; + /** * New Poll vote counting service. * @@ -39,14 +51,107 @@ private static final Log log = LogFactory.getLog(PollVoteCountingService.class); - protected VoteCountingService delegateService; + /** + * Provider of {@link VoteCountingStrategy}. + * <p/> + * <strong>Note:</strong> this provider should be injected via method + * {@link #setProvider(VoteCountingStrategyProvider)}. + */ + private VoteCountingStrategyProvider provider; - public VoteCountingService getDelegateService() { - if (delegateService == null) { - delegateService = new VoteCountingService(); - delegateService.setProvider( - serviceContext.getVoteCountingStrategyProvider()); + public VoteCountingStrategyProvider getProvider() { + if (provider == null) { + provider = serviceContext.getVoteCountingStrategyProvider(); } - return delegateService; + return provider; } + + public void setProvider(VoteCountingStrategyProvider provider) { + this.provider = provider; + } + + /** + * Execute a vote counting for a given strategy (by his id) and the + * given {@code voters}. + * + * @param strategyId the id of the vote counting strategy to use + * @param voter the votes to vote count + * @return vote counting result + */ + public VoteCountingResult voteCount(int strategyId, Set<Voter> voter) { + + Preconditions.checkNotNull(getProvider()); + Preconditions.checkNotNull(voter); + + VoteCountingStrategy strategy = getProvider().getStrategy(strategyId); + Preconditions.checkNotNull(strategy); + + VoteCountingResult result = strategy.votecount(voter); + return result; + } + + /** + * Execute a vote counting for a given strategy (by his id) and the + * given {@code voters}. + * + * @param strategyId the id of the vote counting strategy to use + * @param voter the votes to vote count + * @return vote counting result + */ + public GroupVoteCountingResult voteCountByGroup(int strategyId, Set<Voter> voter) { + + Preconditions.checkNotNull(getProvider()); + Preconditions.checkNotNull(voter); + + VoteCountingStrategy strategy = getProvider().getStrategy(strategyId); + Preconditions.checkNotNull(strategy); + + // Create a groupVoter including of the root voters + GroupOfVoter group = GroupOfVoter.newVoter(null, 1.0, null, voter); + + + Map<GroupOfVoter, VoteCountingResult> groupResults = Maps.newHashMap(); + voteCount(strategy, group, groupResults); + + // get result for main group (and remove it from groups) + VoteCountingResult mainResult = groupResults.remove(group); + + GroupVoteCountingResult result = GroupVoteCountingResult.newResult( + mainResult, groupResults); + return result; + } + + protected void voteCount(VoteCountingStrategy strategy, + GroupOfVoter group, + Map<GroupOfVoter, VoteCountingResult> results) { + + // all childs of this group + Set<Voter> voters = group.getVoters(); + + // treat before all his group childs + for (Voter voter : voters) { + if (voter instanceof GroupOfVoter) { + + // treat group child before all + voteCount(strategy, (GroupOfVoter) voter, results); + } + } + + // once here, all childs has been treated, can votecount this group + VoteCountingResult voteCountingResult = strategy.votecount(voters); + + // store the result for this group + results.put(group, voteCountingResult); + + // result of the group is now the voteForChoice for it + + for (ChoiceScore choiceScore : voteCountingResult.getScores()) { + VoteForChoice voteForChoice = VoteForChoice.newVote( + choiceScore.getChoiceId(), + //FIXME-tchemit-2012-06-26 Which is the value to set as choice for each result? + choiceScore.getScoreValue().doubleValue()); + group.addVoteForChoice(voteForChoice); + } + + } } Modified: trunk/pollen-services/src/test/java/org/chorem/pollen/services/FakeServiceContext.java =================================================================== --- trunk/pollen-services/src/test/java/org/chorem/pollen/services/FakeServiceContext.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-services/src/test/java/org/chorem/pollen/services/FakeServiceContext.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -28,7 +28,7 @@ import org.chorem.pollen.PollenConfiguration; import org.chorem.pollen.PollenIOUtil; import org.chorem.pollen.PollenTopiaRootContextFactory; -import org.chorem.pollen.votecounting.VoteCountingStrategyProvider; +import org.chorem.pollen.votecounting.strategy.VoteCountingStrategyProvider; import org.junit.rules.TestWatcher; import org.junit.runner.Description; import org.nuiton.topia.TopiaContext; Modified: trunk/pollen-ui-struts2/pom.xml =================================================================== --- trunk/pollen-ui-struts2/pom.xml 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-ui-struts2/pom.xml 2012-06-26 03:16:11 UTC (rev 3559) @@ -26,30 +26,13 @@ </dependency> <dependency> <groupId>${project.groupId}</groupId> - <artifactId>pollen-votecounting-strategy-condorcet</artifactId> + <artifactId>pollen-votecounting-strategy-all</artifactId> <version>${project.version}</version> + <type>pom</type> <scope>runtime</scope> </dependency> <dependency> <groupId>${project.groupId}</groupId> - <artifactId>pollen-votecounting-strategy-normal</artifactId> - <version>${project.version}</version> - <scope>runtime</scope> - </dependency> - <dependency> - <groupId>${project.groupId}</groupId> - <artifactId>pollen-votecounting-strategy-number</artifactId> - <version>${project.version}</version> - <scope>runtime</scope> - </dependency> - <dependency> - <groupId>${project.groupId}</groupId> - <artifactId>pollen-votecounting-strategy-percentage</artifactId> - <version>${project.version}</version> - <scope>runtime</scope> - </dependency> - <dependency> - <groupId>${project.groupId}</groupId> <artifactId>pollen-votecounting</artifactId> <version>${project.version}</version> </dependency> Modified: trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/PollenApplicationListener.java =================================================================== --- trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/PollenApplicationListener.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/PollenApplicationListener.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -37,7 +37,7 @@ import org.chorem.pollen.services.PollenServiceContext; import org.chorem.pollen.services.PollenServiceFactory; import org.chorem.pollen.services.impl.UserService; -import org.chorem.pollen.votecounting.VoteCountingStrategyProvider; +import org.chorem.pollen.votecounting.strategy.VoteCountingStrategyProvider; import org.nuiton.i18n.I18n; import org.nuiton.i18n.init.DefaultI18nInitializer; import org.nuiton.topia.TopiaContext; Modified: trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/PollenActionSupport.java =================================================================== --- trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/PollenActionSupport.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/PollenActionSupport.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -46,8 +46,8 @@ import org.chorem.pollen.services.impl.UserService; import org.chorem.pollen.services.impl.VoteService; import org.chorem.pollen.ui.PollenSession; -import org.chorem.pollen.votecounting.VoteCountingStrategy; -import org.chorem.pollen.votecounting.VoteCountingStrategyProvider; +import org.chorem.pollen.votecounting.strategy.VoteCountingStrategy; +import org.chorem.pollen.votecounting.strategy.VoteCountingStrategyProvider; import org.nuiton.topia.TopiaContext; import org.nuiton.topia.framework.TopiaTransactionAware; import org.nuiton.web.filter.TopiaTransactionFilter; Modified: trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractPollForm.java =================================================================== --- trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractPollForm.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractPollForm.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -66,8 +66,8 @@ import org.chorem.pollen.ui.actions.PageSkin; import org.chorem.pollen.ui.actions.PollenActionSupport; import org.chorem.pollen.ui.converters.DateConverter; -import org.chorem.pollen.votecounting.VoteCountingStrategy; -import org.chorem.pollen.votecounting.VoteCountingStrategyProvider; +import org.chorem.pollen.votecounting.strategy.VoteCountingStrategy; +import org.chorem.pollen.votecounting.strategy.VoteCountingStrategyProvider; import org.nuiton.util.StringUtil; import javax.servlet.http.HttpServletRequest; Modified: trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractVoteAction.java =================================================================== --- trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractVoteAction.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/AbstractVoteAction.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -47,7 +47,7 @@ import org.chorem.pollen.services.impl.SecurityService; import org.chorem.pollen.ui.PollenUIUtils; import org.chorem.pollen.ui.actions.PageSkin; -import org.chorem.pollen.votecounting.VoteCountingStrategy; +import org.chorem.pollen.votecounting.strategy.VoteCountingStrategy; import javax.servlet.http.HttpServletRequest; import java.io.UnsupportedEncodingException; Modified: trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/ResultForPoll.java =================================================================== --- trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/ResultForPoll.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/ResultForPoll.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -43,7 +43,7 @@ import org.chorem.pollen.ui.PollenUIUtils; import org.chorem.pollen.ui.actions.PageSkin; import org.chorem.pollen.ui.converters.DateConverter; -import org.chorem.pollen.votecounting.VoteCountingStrategy; +import org.chorem.pollen.votecounting.strategy.VoteCountingStrategy; import org.nuiton.util.StringUtil; import java.net.URL; Modified: trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/VoteForPoll.java =================================================================== --- trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/VoteForPoll.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-ui-struts2/src/main/java/org/chorem/pollen/ui/actions/poll/VoteForPoll.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -34,7 +34,7 @@ import org.chorem.pollen.business.persistence.VoteToChoice; import org.chorem.pollen.common.PollType; import org.chorem.pollen.services.impl.VoteService; -import org.chorem.pollen.votecounting.VoteCountingStrategy; +import org.chorem.pollen.votecounting.strategy.VoteCountingStrategy; /** * Votes to a poll. Deleted: trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/AbstractVoteCountingStrategy.java =================================================================== --- trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/AbstractVoteCountingStrategy.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/AbstractVoteCountingStrategy.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -1,103 +0,0 @@ -/* - * #%L - * Pollen :: VoteCounting Api - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 CodeLutin - * %% - * 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% - */ -package org.chorem.pollen.votecounting; - -import com.google.common.collect.Iterables; -import com.google.common.collect.Lists; -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.VoteCountingResult; -import org.chorem.pollen.votecounting.model.Voter; - -import java.util.Collections; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Set; - -import static org.nuiton.i18n.I18n.l_; - -/** - * Base abstract implementation of a {@link VoteCountingStrategy}. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.5 - */ -public abstract class AbstractVoteCountingStrategy implements VoteCountingStrategy { - - @Override - public String getStrategyName(Locale locale) { - String result = l_(locale, getI18nName()); - return result; - } - - @Override - public String getStrategyHelp(Locale locale) { - String voteName = l_(locale, getI18nName()); - String voteHelp = l_(locale, getI18nHelp()); - String result = l_(locale, "pollen.voteCountingType.help", voteName, voteHelp); - return result; - } - - public Map<String, ChoiceScore> votersToResult(Set<Voter> voters) { - // get all choice Id - Set<String> choiceIds = getAllChoiceIds(voters); - - Map<String, ChoiceScore> resultByChoice = Maps.newTreeMap(); - - // creates all empty result for choice - for (String choiceId : choiceIds) { - ChoiceScore choiceScore = ChoiceScore.newScore(choiceId, null); - resultByChoice.put(choiceId, choiceScore); - } - return resultByChoice; - } - - public VoteCountingResult resultToList(Voter voter, - Map<String, ChoiceScore> resultByChoice) { - List<ChoiceScore> score = toChoiceScore(resultByChoice); - return VoteCountingResult.newResult(score); - } - - public List<ChoiceScore> toChoiceScore(Map<String, ChoiceScore> resultByChoice) { - List<ChoiceScore> score = Lists.newArrayList(resultByChoice.values()); - Collections.sort(score); - Collections.reverse(score); - return score; - } - - public Set<String> getAllChoiceIds(Set<Voter> voters) { - ChoiceIdAble.ChoiceIdAbleById function = new ChoiceIdAble.ChoiceIdAbleById(); - Set<String> result = Sets.newHashSet(); - for (Voter voter : voters) { - Iterable<String> transform = Iterables.transform( - voter.getVoteForChoices(), function); - Iterables.addAll(result, transform); - - } - return result; - } - -} Deleted: trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/ChoiceToVoteRenderType.java =================================================================== --- trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/ChoiceToVoteRenderType.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/ChoiceToVoteRenderType.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -1,42 +0,0 @@ -/* - * #%L - * Pollen :: VoteCounting Api - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 CodeLutin - * %% - * 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% - */ -package org.chorem.pollen.votecounting; - -/** - * To define how to render a vote. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.5 - */ -public enum ChoiceToVoteRenderType { - - /** - * To render as a check box. - * <p/> - * If check box is checked, then result should be value 1, otherwise - * result will be null. - */ - CHECKBOX, - /** To render as a text field. */ - TEXTFIELD -} Deleted: trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/VoteCountigStrategyNotFound.java =================================================================== --- trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/VoteCountigStrategyNotFound.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/VoteCountigStrategyNotFound.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -1,41 +0,0 @@ -/* - * #%L - * Pollen :: VoteCounting Api - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 CodeLutin - * %% - * 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% - */ -package org.chorem.pollen.votecounting; - -/** - * Exception when a vote counting could not be found. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.5 - */ -public class VoteCountigStrategyNotFound extends RuntimeException { - private static final long serialVersionUID = 1L; - - public VoteCountigStrategyNotFound(String message) { - super(message); - } - - public VoteCountigStrategyNotFound(String message, Throwable cause) { - super(message, cause); - } -} Deleted: trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/VoteCountingService.java =================================================================== --- trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/VoteCountingService.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/VoteCountingService.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -1,141 +0,0 @@ -/* - * #%L - * Pollen :: VoteCounting Api - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 CodeLutin - * %% - * 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% - */ -package org.chorem.pollen.votecounting; - -import com.google.common.base.Preconditions; -import com.google.common.collect.Maps; -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.VoteCountingResult; -import org.chorem.pollen.votecounting.model.VoteForChoice; -import org.chorem.pollen.votecounting.model.Voter; - -import java.util.Map; -import java.util.Set; - -/** - * Service to vote count. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.5 - */ -public class VoteCountingService { - - /** - * Provider of {@link VoteCountingStrategy}. - * <p/> - * <strong>Note:</strong> this provider should be injected via method - * {@link #setProvider(VoteCountingStrategyProvider)}. - */ - private VoteCountingStrategyProvider provider; - - public void setProvider(VoteCountingStrategyProvider provider) { - this.provider = provider; - } - - /** - * Execute a vote counting for a given strategy (by his id) and the - * given {@code voters}. - * - * @param strategyId the id of the vote counting strategy to use - * @param voter the votes to vote count - * @return vote counting result - */ - public VoteCountingResult voteCount(int strategyId, Set<Voter> voter) { - - Preconditions.checkNotNull(provider); - Preconditions.checkNotNull(voter); - - VoteCountingStrategy strategy = provider.getStrategy(strategyId); - Preconditions.checkNotNull(strategy); - - VoteCountingResult result = strategy.votecount(voter); - return result; - } - - /** - * Execute a vote counting for a given strategy (by his id) and the - * given {@code voters}. - * - * @param strategyId the id of the vote counting strategy to use - * @param voter the votes to vote count - * @return vote counting result - */ - public GroupVoteCountingResult voteCountByGroup(int strategyId, Set<Voter> voter) { - - Preconditions.checkNotNull(provider); - Preconditions.checkNotNull(voter); - - VoteCountingStrategy strategy = provider.getStrategy(strategyId); - Preconditions.checkNotNull(strategy); - - // Create a groupVoter including of the root voters - GroupOfVoter group = GroupOfVoter.newVoter(null, 1.0, null, voter); - - - Map<GroupOfVoter, VoteCountingResult> groupResults = Maps.newHashMap(); - voteCount(strategy, group, groupResults); - - // get result for main group (and remove it from groups) - VoteCountingResult mainResult = groupResults.remove(group); - - GroupVoteCountingResult result = GroupVoteCountingResult.newResult( - mainResult, groupResults); - return result; - } - - protected void voteCount(VoteCountingStrategy strategy, - GroupOfVoter group, - Map<GroupOfVoter, VoteCountingResult> results) { - - // all childs of this group - Set<Voter> voters = group.getVoters(); - - // treat before all his group childs - for (Voter voter : voters) { - if (voter instanceof GroupOfVoter) { - - // treat group child before all - voteCount(strategy, (GroupOfVoter) voter, results); - } - } - - // once here, all childs has been treated, can votecount this group - VoteCountingResult voteCountingResult = strategy.votecount(voters); - - // store the result for this group - results.put(group, voteCountingResult); - - // result of the group is now the voteForChoice for it - - for (ChoiceScore choiceScore : voteCountingResult.getScores()) { - VoteForChoice voteForChoice = VoteForChoice.newVote( - choiceScore.getChoiceId(), - //FIXME-tchemit-2012-06-26 Which is the value to set as choice for each result? - choiceScore.getScoreValue().doubleValue()); - group.addVoteForChoice(voteForChoice); - } - - } -} Deleted: trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/VoteCountingStrategy.java =================================================================== --- trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/VoteCountingStrategy.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/VoteCountingStrategy.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -1,91 +0,0 @@ -/* - * #%L - * Pollen :: VoteCounting Api - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 CodeLutin - * %% - * 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% - */ -package org.chorem.pollen.votecounting; - -import org.chorem.pollen.votecounting.model.VoteCountingResult; -import org.chorem.pollen.votecounting.model.Voter; - -import java.util.Locale; -import java.util.Set; - -import static org.nuiton.i18n.I18n.l_; - -/** - * Contract of a vote counting method. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.5 - */ -public interface VoteCountingStrategy { - - /** - * Obtains the unique id of this strategy. - * - * @return the unique id of this strategy. - */ - int getId(); - - /** - * Obtains the i18n name of this strategy. - * - * @return the i18n name of this strategy. - */ - String getI18nName(); - - /** - * Obtains the i18n help of this strategy. - * - * @return the i18n help of this strategy. - */ - String getI18nHelp(); - - /** - * Vote count for the given {@code voter} and return the result of it. - * - * @param voter the voter and their votes. - * @return the result of the vote counting for the given voter. - */ - VoteCountingResult votecount(Set<Voter> voter); - - String getDisplayVoteValue(Integer voteValue); - - boolean isChoiceInVote(Integer voteValue); - - String getStrategyName(Locale locale); - - String getStrategyHelp(Locale locale); - - boolean isVoteValueValid(Integer voteValue); - - String getVoteValueNotValidMessage(); - - boolean isTotalVoteValueValid(int totalValues); - - String getTotalVoteValueNotValidMessage(); - - boolean isVoteValueNull(Integer value); - - ChoiceToVoteRenderType getRenderType(); - - boolean isDisplayResultsByChoice(); -} Deleted: trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/VoteCountingStrategyProvider.java =================================================================== --- trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/VoteCountingStrategyProvider.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/VoteCountingStrategyProvider.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -1,101 +0,0 @@ -/* - * #%L - * Pollen :: VoteCounting Api - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 CodeLutin - * %% - * 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% - */ -package org.chorem.pollen.votecounting; - -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Maps; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import java.util.Map; -import java.util.ServiceLoader; -import java.util.Set; - -/** - * Provider of {@link VoteCountingStrategy}. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.5 - */ -public class VoteCountingStrategyProvider { - - /** Logger. */ - private static final Log log = - LogFactory.getLog(VoteCountingStrategyProvider.class); - - /** - * List of available strageries detected via the {@link ServiceLoader} - * mecanism no contract {@link VoteCountingStrategy}. - */ - private final Map<Integer, VoteCountingStrategy> strategies; - - public VoteCountingStrategyProvider() { - strategies = load(); - } - - public Set<Integer> getStrategyIds() { - return ImmutableSet.copyOf(strategies.keySet()); - } - - public VoteCountingStrategy getStrategy(int strategyId) throws VoteCountigStrategyNotFound { - VoteCountingStrategy type = strategies.get(strategyId); - - if (type == null) { - throw new VoteCountigStrategyNotFound( - "Could not find strategy with id " + strategyId); - } - try { - //FIXME-tchemit-2012-06-24 : should we reinstanciate each time ? - return type.getClass().newInstance(); - } catch (Exception e) { - - throw new VoteCountigStrategyNotFound( - "Could not instanciate stragegy " + strategyId, e); - } - } - - protected Map<Integer, VoteCountingStrategy> load() { - Map<Integer, VoteCountingStrategy> result = Maps.newTreeMap(); - ServiceLoader<VoteCountingStrategy> loader = - ServiceLoader.load(VoteCountingStrategy.class); - - for (VoteCountingStrategy strategy : loader) { - int id = strategy.getId(); - if (result.containsKey(id)) { - throw new IllegalStateException( - "Strategy [" + - strategy.getI18nName() + "] with id " + id + - ", can not be used since the id is already used " + - "by strategy [" + result.get(id).getI18nName() + "] "); - } - if (log.isInfoEnabled()) { - log.info("Detected strategy [" + id + "-" + - strategy.getI18nName() + "] : " + - strategy.getClass().getName()); - } - result.put(id, strategy); - } - return result; - } - -} Copied: trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/ChoiceToVoteRenderType.java (from rev 3548, trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/ChoiceToVoteRenderType.java) =================================================================== --- trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/ChoiceToVoteRenderType.java (rev 0) +++ trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/ChoiceToVoteRenderType.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -0,0 +1,42 @@ +/* + * #%L + * Pollen :: VoteCounting Api + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * 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% + */ +package org.chorem.pollen.votecounting.model; + +/** + * To define how to render a vote. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.5 + */ +public enum ChoiceToVoteRenderType { + + /** + * To render as a check box. + * <p/> + * If check box is checked, then result should be value 1, otherwise + * result will be null. + */ + CHECKBOX, + /** To render as a text field. */ + TEXTFIELD +} Property changes on: trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/model/ChoiceToVoteRenderType.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/strategy/AbstractVoteCountingStrategy.java (from rev 3558, trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/AbstractVoteCountingStrategy.java) =================================================================== --- trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/strategy/AbstractVoteCountingStrategy.java (rev 0) +++ trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/strategy/AbstractVoteCountingStrategy.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -0,0 +1,187 @@ +/* + * #%L + * Pollen :: VoteCounting Api + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * 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% + */ +package org.chorem.pollen.votecounting.strategy; + +import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; +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.VoteCountingResult; +import org.chorem.pollen.votecounting.model.VoteForChoice; +import org.chorem.pollen.votecounting.model.Voter; + +import java.io.Serializable; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Set; + +import static org.nuiton.i18n.I18n.l_; + +/** + * Base abstract implementation of a {@link VoteCountingStrategy}. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.5 + */ +public abstract class AbstractVoteCountingStrategy implements VoteCountingStrategy { + + @Override + public String getStrategyName(Locale locale) { + String result = l_(locale, getI18nName()); + return result; + } + + @Override + public String getStrategyHelp(Locale locale) { + String voteName = l_(locale, getI18nName()); + String voteHelp = l_(locale, getI18nHelp()); + String result = l_(locale, "pollen.voteCountingType.help", voteName, voteHelp); + return result; + } + + public Map<String, ChoiceScore> votersToResult(Set<Voter> voters) { + // get all choice Id + Set<String> choiceIds = getAllChoiceIds(voters); + + Map<String, ChoiceScore> resultByChoice = Maps.newTreeMap(); + + // creates all empty result for choice + for (String choiceId : choiceIds) { + ChoiceScore choiceScore = ChoiceScore.newScore(choiceId, null); + resultByChoice.put(choiceId, choiceScore); + } + return resultByChoice; + } + + public VoteCountingResult resultToList(Map<String, ChoiceScore> resultByChoice) { + List<ChoiceScore> score = toChoiceScore(resultByChoice); + return VoteCountingResult.newResult(score); + } + + public List<ChoiceScore> toChoiceScore(Map<String, ChoiceScore> resultByChoice) { + List<ChoiceScore> score = Lists.newArrayList(resultByChoice.values()); + Collections.sort(score); + Collections.reverse(score); + return score; + } + + public Set<String> getAllChoiceIds(Set<Voter> voters) { + ChoiceIdAble.ChoiceIdAbleById function = new ChoiceIdAble.ChoiceIdAbleById(); + Set<String> result = Sets.newHashSet(); + for (Voter voter : voters) { + Iterable<String> transform = Iterables.transform( + voter.getVoteForChoices(), function); + Iterables.addAll(result, transform); + + } + return result; + } + + public Map<Voter, List<Set<String>>> buildVoterSortedChoices(Set<Voter> voters) { + + VoteForChoiceComparator comparator = new VoteForChoiceComparator(); + + Map<Voter, List<Set<String>>> voterSortedChoices = Maps.newHashMap(); + + for (Voter voter : voters) { + + List<Set<String>> sortedChoices = sortVoteForChoices( + voter.getVoteForChoices(), comparator); + voterSortedChoices.put(voter, sortedChoices); + } + return voterSortedChoices; + } + + public List<Set<String>> sortVoteForChoices(Set<VoteForChoice> voteForChoices, + Comparator<VoteForChoice> comparator) { + // get sort vote for choices + List<VoteForChoice> sortedChoices = Lists.newArrayList(voteForChoices); + Collections.sort(sortedChoices, comparator); + + // build ranks + List<Set<String>> result = Lists.newArrayList(); + + Set<String> set = Sets.newHashSet(); + result.add(set); + VoteForChoice lastVoteForChoice = null; + for (VoteForChoice voteForChoice : sortedChoices) { + if (lastVoteForChoice != null && + comparator.compare(lastVoteForChoice, voteForChoice) != 0) { + + // new rank found + // register it + result.add(set = Sets.newHashSet()); + } + + set.add(voteForChoice.getChoiceId()); + lastVoteForChoice = voteForChoice; + } + return result; + } + + public Set<String> getDirectWinners(Collection<List<Set<String>>> values) { + Set<String> result = null; + for (List<Set<String>> value : values) { + + Set<String> topChoices = value.get(0); + + if (result == null) { + // first time coming here + result = topChoices; + } else { + + if (!result.equals(topChoices)) { + + // not same first ranking, not direct winner + result = null; + break; + } + } + } + return result; + } + + public static class VoteForChoiceComparator implements Comparator<VoteForChoice>, Serializable { + + private static final long serialVersionUID = 1L; + + @Override + public int compare(VoteForChoice o1, VoteForChoice o2) { + Double v1 = o1.getVoteValue(); + Double v2 = o2.getVoteValue(); + if (v1 == null) { + v1 = Double.MAX_VALUE; + } + if (v2 == null) { + v2 = Double.MAX_VALUE; + } + return v1.intValue() - v2.intValue(); + } + } +} Property changes on: trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/strategy/AbstractVoteCountingStrategy.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/strategy/VoteCountigStrategyNotFound.java (from rev 3548, trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/VoteCountigStrategyNotFound.java) =================================================================== --- trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/strategy/VoteCountigStrategyNotFound.java (rev 0) +++ trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/strategy/VoteCountigStrategyNotFound.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -0,0 +1,41 @@ +/* + * #%L + * Pollen :: VoteCounting Api + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * 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% + */ +package org.chorem.pollen.votecounting.strategy; + +/** + * Exception when a vote counting could not be found. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.5 + */ +public class VoteCountigStrategyNotFound extends RuntimeException { + private static final long serialVersionUID = 1L; + + public VoteCountigStrategyNotFound(String message) { + super(message); + } + + public VoteCountigStrategyNotFound(String message, Throwable cause) { + super(message, cause); + } +} Property changes on: trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/strategy/VoteCountigStrategyNotFound.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategy.java (from rev 3548, trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/VoteCountingStrategy.java) =================================================================== --- trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategy.java (rev 0) +++ trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategy.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -0,0 +1,90 @@ +/* + * #%L + * Pollen :: VoteCounting Api + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * 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% + */ +package org.chorem.pollen.votecounting.strategy; + +import org.chorem.pollen.votecounting.model.ChoiceToVoteRenderType; +import org.chorem.pollen.votecounting.model.VoteCountingResult; +import org.chorem.pollen.votecounting.model.Voter; + +import java.util.Locale; +import java.util.Set; + +/** + * Contract of a vote counting method. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.5 + */ +public interface VoteCountingStrategy { + + /** + * Obtains the unique id of this strategy. + * + * @return the unique id of this strategy. + */ + int getId(); + + /** + * Obtains the i18n name of this strategy. + * + * @return the i18n name of this strategy. + */ + String getI18nName(); + + /** + * Obtains the i18n help of this strategy. + * + * @return the i18n help of this strategy. + */ + String getI18nHelp(); + + /** + * Vote count for the given {@code voter} and return the result of it. + * + * @param voter the voter and their votes. + * @return the result of the vote counting for the given voter. + */ + VoteCountingResult votecount(Set<Voter> voter); + + String getDisplayVoteValue(Integer voteValue); + + boolean isChoiceInVote(Integer voteValue); + + String getStrategyName(Locale locale); + + String getStrategyHelp(Locale locale); + + boolean isVoteValueValid(Integer voteValue); + + String getVoteValueNotValidMessage(); + + boolean isTotalVoteValueValid(int totalValues); + + String getTotalVoteValueNotValidMessage(); + + boolean isVoteValueNull(Integer value); + + ChoiceToVoteRenderType getRenderType(); + + boolean isDisplayResultsByChoice(); +} Property changes on: trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategy.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProvider.java (from rev 3548, trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/VoteCountingStrategyProvider.java) =================================================================== --- trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProvider.java (rev 0) +++ trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProvider.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -0,0 +1,101 @@ +/* + * #%L + * Pollen :: VoteCounting Api + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * 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% + */ +package org.chorem.pollen.votecounting.strategy; + +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Maps; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.util.Map; +import java.util.ServiceLoader; +import java.util.Set; + +/** + * Provider of {@link VoteCountingStrategy}. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.5 + */ +public class VoteCountingStrategyProvider { + + /** Logger. */ + private static final Log log = + LogFactory.getLog(VoteCountingStrategyProvider.class); + + /** + * List of available strageries detected via the {@link ServiceLoader} + * mecanism no contract {@link VoteCountingStrategy}. + */ + private final Map<Integer, VoteCountingStrategy> strategies; + + public VoteCountingStrategyProvider() { + strategies = load(); + } + + public Set<Integer> getStrategyIds() { + return ImmutableSet.copyOf(strategies.keySet()); + } + + public VoteCountingStrategy getStrategy(int strategyId) throws VoteCountigStrategyNotFound { + VoteCountingStrategy type = strategies.get(strategyId); + + if (type == null) { + throw new VoteCountigStrategyNotFound( + "Could not find strategy with id " + strategyId); + } + try { + //FIXME-tchemit-2012-06-24 : should we reinstanciate each time ? + return type.getClass().newInstance(); + } catch (Exception e) { + + throw new VoteCountigStrategyNotFound( + "Could not instanciate stragegy " + strategyId, e); + } + } + + protected Map<Integer, VoteCountingStrategy> load() { + Map<Integer, VoteCountingStrategy> result = Maps.newTreeMap(); + ServiceLoader<VoteCountingStrategy> loader = + ServiceLoader.load(VoteCountingStrategy.class); + + for (VoteCountingStrategy strategy : loader) { + int id = strategy.getId(); + if (result.containsKey(id)) { + throw new IllegalStateException( + "Strategy [" + + strategy.getI18nName() + "] with id " + id + + ", can not be used since the id is already used " + + "by strategy [" + result.get(id).getI18nName() + "] "); + } + if (log.isInfoEnabled()) { + log.info("Detected strategy [" + id + "-" + + strategy.getI18nName() + "] : " + + strategy.getClass().getName()); + } + result.put(id, strategy); + } + return result; + } + +} Property changes on: trunk/pollen-votecounting-api/src/main/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProvider.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Deleted: trunk/pollen-votecounting-api/src/test/java/org/chorem/pollen/votecounting/VoteCountingStrategyProviderTest.java =================================================================== --- trunk/pollen-votecounting-api/src/test/java/org/chorem/pollen/votecounting/VoteCountingStrategyProviderTest.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-votecounting-api/src/test/java/org/chorem/pollen/votecounting/VoteCountingStrategyProviderTest.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -1,50 +0,0 @@ -/* - * #%L - * Pollen :: VoteCounting Api - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 CodeLutin - * %% - * 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% - */ -package org.chorem.pollen.votecounting; - -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -/** - * Tests the {@link VoteCountingStrategyProvider}. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.5 - */ -public class VoteCountingStrategyProviderTest { - - protected static VoteCountingStrategyProvider provider; - - - @BeforeClass - public static void beforeClass() throws Exception { - provider = new VoteCountingStrategyProvider(); - } - - @Test(expected = VoteCountigStrategyNotFound.class) - public void getStrategy() throws Exception { - - Assert.assertNull(provider.getStrategy(-1)); - } -} Copied: trunk/pollen-votecounting-api/src/test/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProviderTest.java (from rev 3548, trunk/pollen-votecounting-api/src/test/java/org/chorem/pollen/votecounting/VoteCountingStrategyProviderTest.java) =================================================================== --- trunk/pollen-votecounting-api/src/test/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProviderTest.java (rev 0) +++ trunk/pollen-votecounting-api/src/test/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProviderTest.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -0,0 +1,50 @@ +/* + * #%L + * Pollen :: VoteCounting Api + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * 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% + */ +package org.chorem.pollen.votecounting.strategy; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Tests the {@link VoteCountingStrategyProvider}. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.5 + */ +public class VoteCountingStrategyProviderTest { + + protected static VoteCountingStrategyProvider provider; + + + @BeforeClass + public static void beforeClass() throws Exception { + provider = new VoteCountingStrategyProvider(); + } + + @Test(expected = VoteCountigStrategyNotFound.class) + public void getStrategy() throws Exception { + + Assert.assertNull(provider.getStrategy(-1)); + } +} Property changes on: trunk/pollen-votecounting-api/src/test/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProviderTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Property changes on: trunk/pollen-votecounting-strategy ___________________________________________________________________ Added: svn:ignore + target *.ipr *.iws *.iml .idea .project .classpath .settings Added: trunk/pollen-votecounting-strategy/LICENSE.txt =================================================================== Added: trunk/pollen-votecounting-strategy/README.txt =================================================================== Added: trunk/pollen-votecounting-strategy/changelog.txt =================================================================== Added: trunk/pollen-votecounting-strategy/pom.xml =================================================================== --- trunk/pollen-votecounting-strategy/pom.xml (rev 0) +++ trunk/pollen-votecounting-strategy/pom.xml 2012-06-26 03:16:11 UTC (rev 3559) @@ -0,0 +1,119 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <!-- ************************************************************* --> + <!-- *** POM Relationships *************************************** --> + <!-- ************************************************************* --> + + <parent> + <groupId>org.chorem</groupId> + <artifactId>pollen</artifactId> + <version>1.5-SNAPSHOT</version> + </parent> + + <groupId>org.chorem.pollen</groupId> + <artifactId>pollen-votecounting-strategy</artifactId> + + <modules> + <module>../pollen-votecounting-strategy-normal</module> + <module>../pollen-votecounting-strategy-percentage</module> + <module>../pollen-votecounting-strategy-condorcet</module> + <module>../pollen-votecounting-strategy-number</module> + <module>../pollen-votecounting-strategy-borda</module> + <module>../pollen-votecounting-strategy-instant-runoff</module> + <module>../pollen-votecounting-strategy-coombs</module> + </modules> + + <dependencies> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>pollen-votecounting-strategy-normal</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>pollen-votecounting-strategy-percentage</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>pollen-votecounting-strategy-condorcet</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>pollen-votecounting-strategy-number</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>pollen-votecounting-strategy-borda</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>pollen-votecounting-strategy-instant-runoff</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>pollen-votecounting-strategy-coombs</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + + <name>Pollen :: VoteCounting strategy</name> + <description> + Aggregator of all implementations of votecounting strategy + </description> + + + <packaging>pom</packaging> + + <profiles> + + <profile> + <id>reporting</id> + <activation> + <property> + <name>performRelease</name> + <value>true</value> + </property> + </activation> + + <build> + <plugins> + <plugin> + <artifactId>maven-site-plugin</artifactId> + <configuration> + <reportPlugins> + <plugin> + <artifactId>maven-project-info-reports-plugin</artifactId> + <reports> + <report>index</report> + <report>project-team</report> + <report>mailing-list</report> + <report>cim</report> + <report>issue-tracking</report> + <report>license</report> + <report>scm</report> + <report>dependency-convergence</report> + <report>dependency-management</report> + </reports> + </plugin> + </reportPlugins> + </configuration> + </plugin> + </plugins> + </build> + + </profile> + + </profiles> + +</project> + Added: trunk/pollen-votecounting-strategy/src/site/site_en.xml =================================================================== --- trunk/pollen-votecounting-strategy/src/site/site_en.xml (rev 0) +++ trunk/pollen-votecounting-strategy/src/site/site_en.xml 2012-06-26 03:16:11 UTC (rev 3559) @@ -0,0 +1,46 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<project name="${project.name}"> + + <body> + + <breadcrumbs> + <item name="${project.name}" href="${project.url}/index.html"/> + </breadcrumbs> + + <menu ref="parent"/> + + <menu name="User"> + <item name="About" href="./index.html"/> + </menu> + + <menu name="Language"> + <item name="Français" href="index.html"/> + <item name="English" href="../index.html"/> + </menu> + + <menu ref="reports"/> + + <footer> + + <script type="text/javascript" + src="http://maven-site.chorem.org/public/js/maven-site-chorem.org.js"> + </script> + + <div id='projectMetas' + projectversion='${project.version}' + platform='${project.platform}' + projectid='${project.projectId}' + scm='${project.scm.connection}' + scmwebeditorenabled='${project.scmwebeditorEnabled}' + scmwebeditorurl='${project.scmwebeditorUrl}' + siteSourcesType='${project.siteSourcesType}' + piwikEnabled='${project.piwikEnabled}' + piwikId='${project.piwikId}'> + </div> + </footer> + + </body> + +</project> + Added: trunk/pollen-votecounting-strategy/src/site/site_fr.xml =================================================================== --- trunk/pollen-votecounting-strategy/src/site/site_fr.xml (rev 0) +++ trunk/pollen-votecounting-strategy/src/site/site_fr.xml 2012-06-26 03:16:11 UTC (rev 3559) @@ -0,0 +1,46 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<project name="${project.name}"> + + <body> + + <breadcrumbs> + <item name="${project.name}" href="${project.url}/index.html"/> + </breadcrumbs> + + <menu ref="parent"/> + + <menu name="Utilisateur"> + <item name="A propos" href="./index.html"/> + </menu> + + <menu name="Language"> + <item name="Français" href="index.html"/> + <item name="English" href="../index.html"/> + </menu> + + <menu ref="reports"/> + + <footer> + + <script type="text/javascript" + src="http://maven-site.chorem.org/public/js/maven-site-chorem.org.js"> + </script> + + <div id='projectMetas' + projectversion='${project.version}' + platform='${project.platform}' + projectid='${project.projectId}' + scm='${project.scm.connection}' + scmwebeditorenabled='${project.scmwebeditorEnabled}' + scmwebeditorurl='${project.scmwebeditorUrl}' + siteSourcesType='${project.siteSourcesType}' + piwikEnabled='${project.piwikEnabled}' + piwikId='${project.piwikId}'> + </div> + </footer> + + </body> + +</project> + Deleted: trunk/pollen-votecounting-strategy-condorcet/src/main/java/org/chorem/pollen/votecounting/CondorcetStrategy.java =================================================================== --- trunk/pollen-votecounting-strategy-condorcet/src/main/java/org/chorem/pollen/votecounting/CondorcetStrategy.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-votecounting-strategy-condorcet/src/main/java/org/chorem/pollen/votecounting/CondorcetStrategy.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -1,331 +0,0 @@ -/* - * #%L - * Pollen :: VoteCounting strategy :: Condorcet - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 CodeLutin - * %% - * 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% - */ -package org.chorem.pollen.votecounting; - -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.chorem.pollen.votecounting.model.ChoiceIdAble; -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.io.Serializable; -import java.math.BigDecimal; -import java.util.Arrays; -import java.util.Comparator; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import static org.nuiton.i18n.I18n.n_; - -/** - * Condorcet. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.5 - */ -public class CondorcetStrategy extends AbstractVoteCountingStrategy { - - public static final int ID = 2; - - /** Logger. */ - private static final Log log = LogFactory.getLog(CondorcetStrategy.class); - - @Override - public int getId() { - return ID; - } - - @Override - public String getI18nName() { - return n_("pollen.voteCountingType.condorcet"); - } - - @Override - public String getI18nHelp() { - return n_("pollen.voteCountingType.condorcet.help"); - } - - @Override - public String getTotalVoteValueNotValidMessage() { - // no validation on total value, so no message - return null; - } - - @Override - public String getVoteValueNotValidMessage() { - return n_("pollen.error.vote.invalidCondorcetVoteValue"); - } - - @Override - public String getDisplayVoteValue(Integer voteValue) { - return voteValue == null ? "" : String.valueOf(voteValue); - } - - @Override - public ChoiceToVoteRenderType getRenderType() { - return ChoiceToVoteRenderType.TEXTFIELD; - } - - @Override - public boolean isChoiceInVote(Integer voteValue) { - return voteValue != null && voteValue > 0 && voteValue < 100; - } - - @Override - public boolean isVoteValueNull(Integer voteValue) { - return voteValue == null; - } - - @Override - public boolean isDisplayResultsByChoice() { - return false; - } - - @Override - public boolean isVoteValueValid(Integer voteValue) { - return voteValue != null && voteValue > 0; - } - - @Override - public boolean isTotalVoteValueValid(int totalValues) { - // no validation on total value - return true; - } - - @Override - public VoteCountingResult votecount(Set<Voter> voters) { - - // get empty result by choice - Map<String, ChoiceScore> resultByChoice = votersToResult(voters); - - // get order over choices (needed to get coordinates over matrix) - List<String> choiceIds = Lists.newArrayList(resultByChoice.keySet()); - - // nb of choices - int nbChoices = choiceIds.size(); - - //matrix of pairwise - double[][] matrix = new double[nbChoices][nbChoices]; - - // compute pairwise battle matrix and try to have a direct winner(s) - Set<String> winners = computePairWiseMatrix(choiceIds, voters, matrix); - - // compute nb battles wins for each choice - // and store it as choice score value - computeNbBattlesByChoice(resultByChoice, choiceIds, matrix); - - // get choice score ordered by their number of win battles - List<ChoiceScore> choiceScores = toChoiceScore(resultByChoice); - - if (winners != null) { - - // there is winner(s), re-add them to - - if (log.isDebugEnabled()) { - log.debug("Direct winners : " + winners); - } - for (String choiceId : winners) { - ChoiceScore choiceScore = resultByChoice.get(choiceId); - choiceScores.remove(choiceScore); - choiceScores.add(0, choiceScore); - } - } else { - - // no direct winner, must resolv conflicts - - resolvConflicts(choiceScores, matrix); - } - - // transform map of result to list of them (and sort them) - VoteCountingResult result = resultToList(null, resultByChoice); - return result; - } - - protected void resolvConflicts(List<ChoiceScore> choiceScores, - double[][] matrix) { - - // for the moment we use only number of win battles - // so nothing more to do here... - } - - protected void computeNbBattlesByChoice(Map<String, ChoiceScore> resultByChoice, - List<String> choiceIds, - double[][] matrix) { - - int nbChoices = choiceIds.size(); - - for (String choiceId : choiceIds) { - - int i = choiceIds.indexOf(choiceId); - - double nbBattles = 0; - for (int j = 0; j < nbChoices; j++) { - double aRow = matrix[i][j]; - nbBattles += aRow; - } - - resultByChoice.get(choiceId).setScoreValue( - BigDecimal.valueOf(nbBattles)); - - if (log.isDebugEnabled()) { - log.debug("Nb battle wins for choice " + choiceId + ": " + nbBattles); - } - } - } - - private Set<String> computePairWiseMatrix(List<String> choiceIds, - Set<Voter> voters, - double[][] matrix) { - - int nbChoices = choiceIds.size(); - - Set<String> winners = null; - - VoteForChoiceComparator comparator = new VoteForChoiceComparator(); - - double[] currentVoteWinner = new double[choiceIds.size()]; - - boolean firstVoter = true; - for (Voter voter : voters) { - - if (log.isDebugEnabled()) { - log.debug("Start count for voter " + voter.getVoterId()); - } - double voterWeight = voter.getWeight(); - - Arrays.fill(currentVoteWinner, 0); - - Map<String, VoteForChoice> voteByChoiceIds = - Maps.uniqueIndex(voter.getVoteForChoices(), - new ChoiceIdAble.ChoiceIdAbleById()); - - double maxBattleWins = 0; - - for (String choiceId : choiceIds) { - - // get vote for this choice - VoteForChoice voteForChoice = voteByChoiceIds.get(choiceId); - - int x = choiceIds.indexOf(choiceId); - - for (VoteForChoice voteForChoice1 : voter.getVoteForChoices()) { - - String choiceId1 = voteForChoice1.getChoiceId(); - if (choiceId.equals(choiceId1)) { - - // can not battle same choice - continue; - } - - int y = choiceIds.indexOf(choiceId1); - - int compare = comparator.compare(voteForChoice, - voteForChoice1); - - if (compare > 0) { - - // voteForChoice wins his battle - - int pos = x * nbChoices + y; - - matrix[x][y] = matrix[x][y] + voterWeight; - if (log.isTraceEnabled()) { - log.trace("battle [" + choiceId + "<" + - voteForChoice.getVoteValue() + "> - " + - choiceId1 + "<" + - voteForChoice1.getVoteValue() + - ">] wins (store to " + pos + - ", new value=" + matrix[x][y] + ")"); - } - maxBattleWins = Math.max( - maxBattleWins, - currentVoteWinner[x] = - currentVoteWinner[x] + voterWeight); - } - } - } - - if (firstVoter || winners != null) { - - // still have a winner, le'ts compute current winner - Set<String> currentVoteWinners = Sets.newHashSet(); - - // find winners of this vote - for (int i = 0; i < currentVoteWinner.length; i++) { - if (maxBattleWins == currentVoteWinner[i]) { - - // this is a winner for this vote - String choiceId = choiceIds.get(i); - currentVoteWinners.add(choiceId); - } - } - if (log.isDebugEnabled()) { - log.debug("Winners of this vote : " + currentVoteWinners); - } - - - if (firstVoter) { - - // keep this first result - winners = currentVoteWinners; - } else if (!winners.equals(currentVoteWinners)) { - - // current vote has not same winners than previous, - // so will need resolution - winners = null; - - if (log.isDebugEnabled()) { - log.debug("Dismatch winners (will need resolv...)"); - } - } - } - - firstVoter = false; - } - return winners; - } - - public static class VoteForChoiceComparator implements Comparator<VoteForChoice>, Serializable { - - private static final long serialVersionUID = 1L; - - @Override - public int compare(VoteForChoice o1, VoteForChoice o2) { - Double v1 = o1.getVoteValue(); - Double v2 = o2.getVoteValue(); - if (v1 == null) { - v1 = Double.MAX_VALUE; - } - if (v2 == null) { - v2 = Double.MAX_VALUE; - } - return v2.intValue() - v1.intValue(); - } - } -} Copied: trunk/pollen-votecounting-strategy-condorcet/src/main/java/org/chorem/pollen/votecounting/strategy/CondorcetStrategy.java (from rev 3557, trunk/pollen-votecounting-strategy-condorcet/src/main/java/org/chorem/pollen/votecounting/CondorcetStrategy.java) =================================================================== --- trunk/pollen-votecounting-strategy-condorcet/src/main/java/org/chorem/pollen/votecounting/strategy/CondorcetStrategy.java (rev 0) +++ trunk/pollen-votecounting-strategy-condorcet/src/main/java/org/chorem/pollen/votecounting/strategy/CondorcetStrategy.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -0,0 +1,313 @@ +/* + * #%L + * Pollen :: VoteCounting strategy :: Condorcet + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * 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% + */ +package org.chorem.pollen.votecounting.strategy; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.chorem.pollen.votecounting.model.ChoiceIdAble; +import org.chorem.pollen.votecounting.model.ChoiceScore; +import org.chorem.pollen.votecounting.model.ChoiceToVoteRenderType; +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.Arrays; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.nuiton.i18n.I18n.n_; + +/** + * Condorcet. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.5 + */ +public class CondorcetStrategy extends AbstractVoteCountingStrategy { + + public static final int ID = 2; + + /** Logger. */ + private static final Log log = LogFactory.getLog(CondorcetStrategy.class); + + @Override + public int getId() { + return ID; + } + + @Override + public String getI18nName() { + return n_("pollen.voteCountingType.condorcet"); + } + + @Override + public String getI18nHelp() { + return n_("pollen.voteCountingType.condorcet.help"); + } + + @Override + public String getTotalVoteValueNotValidMessage() { + // no validation on total value, so no message + return null; + } + + @Override + public String getVoteValueNotValidMessage() { + return n_("pollen.error.vote.invalidCondorcetVoteValue"); + } + + @Override + public String getDisplayVoteValue(Integer voteValue) { + return voteValue == null ? "" : String.valueOf(voteValue); + } + + @Override + public ChoiceToVoteRenderType getRenderType() { + return ChoiceToVoteRenderType.TEXTFIELD; + } + + @Override + public boolean isChoiceInVote(Integer voteValue) { + return voteValue != null && voteValue > 0 && voteValue < 100; + } + + @Override + public boolean isVoteValueNull(Integer voteValue) { + return voteValue == null; + } + + @Override + public boolean isDisplayResultsByChoice() { + return false; + } + + @Override + public boolean isVoteValueValid(Integer voteValue) { + return voteValue != null && voteValue > 0; + } + + @Override + public boolean isTotalVoteValueValid(int totalValues) { + // no validation on total value + return true; + } + + @Override + public VoteCountingResult votecount(Set<Voter> voters) { + + // get empty result by choice + Map<String, ChoiceScore> resultByChoice = votersToResult(voters); + + // get order over choices (needed to get coordinates over matrix) + List<String> choiceIds = Lists.newArrayList(resultByChoice.keySet()); + + // nb of choices + int nbChoices = choiceIds.size(); + + //matrix of pairwise + double[][] matrix = new double[nbChoices][nbChoices]; + + // compute pairwise battle matrix and try to have a direct winner(s) + Set<String> winners = computePairWiseMatrix(choiceIds, voters, matrix); + + // compute nb battles wins for each choice + // and store it as choice score value + computeNbBattlesByChoice(resultByChoice, choiceIds, matrix); + + // get choice score ordered by their number of win battles + List<ChoiceScore> choiceScores = toChoiceScore(resultByChoice); + + if (winners != null) { + + // there is winner(s), re-add them to + + if (log.isDebugEnabled()) { + log.debug("Direct winners : " + winners); + } + for (String choiceId : winners) { + ChoiceScore choiceScore = resultByChoice.get(choiceId); + choiceScores.remove(choiceScore); + choiceScores.add(0, choiceScore); + } + } else { + + // no direct winner, must resolv conflicts + + resolvConflicts(choiceScores, matrix); + } + + // transform map of result to list of them (and sort them) + VoteCountingResult result = resultToList(resultByChoice); + return result; + } + + protected void resolvConflicts(List<ChoiceScore> choiceScores, + double[][] matrix) { + + // for the moment we use only number of win battles + // so nothing more to do here... + } + + protected void computeNbBattlesByChoice(Map<String, ChoiceScore> resultByChoice, + List<String> choiceIds, + double[][] matrix) { + + int nbChoices = choiceIds.size(); + + for (String choiceId : choiceIds) { + + int i = choiceIds.indexOf(choiceId); + + double nbBattles = 0; + for (int j = 0; j < nbChoices; j++) { + double aRow = matrix[i][j]; + nbBattles += aRow; + } + + resultByChoice.get(choiceId).setScoreValue( + BigDecimal.valueOf(nbBattles)); + + if (log.isDebugEnabled()) { + log.debug("Nb battle wins for choice " + choiceId + ": " + nbBattles); + } + } + } + + private Set<String> computePairWiseMatrix(List<String> choiceIds, + Set<Voter> voters, + double[][] matrix) { + + int nbChoices = choiceIds.size(); + + Set<String> winners = null; + + VoteForChoiceComparator comparator = new VoteForChoiceComparator(); + + double[] currentVoteWinner = new double[choiceIds.size()]; + + boolean firstVoter = true; + for (Voter voter : voters) { + + if (log.isDebugEnabled()) { + log.debug("Start count for voter " + voter.getVoterId()); + } + double voterWeight = voter.getWeight(); + + Arrays.fill(currentVoteWinner, 0); + + Map<String, VoteForChoice> voteByChoiceIds = + Maps.uniqueIndex(voter.getVoteForChoices(), + new ChoiceIdAble.ChoiceIdAbleById()); + + double maxBattleWins = 0; + + for (String choiceId : choiceIds) { + + // get vote for this choice + VoteForChoice voteForChoice = voteByChoiceIds.get(choiceId); + + int x = choiceIds.indexOf(choiceId); + + for (VoteForChoice voteForChoice1 : voter.getVoteForChoices()) { + + String choiceId1 = voteForChoice1.getChoiceId(); + if (choiceId.equals(choiceId1)) { + + // can not battle same choice + continue; + } + + int y = choiceIds.indexOf(choiceId1); + + int compare = comparator.compare(voteForChoice, + voteForChoice1); + + if (compare < 0) { + + // voteForChoice wins his battle + + int pos = x * nbChoices + y; + + matrix[x][y] = matrix[x][y] + voterWeight; + if (log.isTraceEnabled()) { + log.trace("battle [" + choiceId + "<" + + voteForChoice.getVoteValue() + "> - " + + choiceId1 + "<" + + voteForChoice1.getVoteValue() + + ">] wins (store to " + pos + + ", new value=" + matrix[x][y] + ")"); + } + maxBattleWins = Math.max( + maxBattleWins, + currentVoteWinner[x] = + currentVoteWinner[x] + voterWeight); + } + } + } + + if (firstVoter || winners != null) { + + // still have a winner, le'ts compute current winner + Set<String> currentVoteWinners = Sets.newHashSet(); + + // find winners of this vote + for (int i = 0; i < currentVoteWinner.length; i++) { + if (maxBattleWins == currentVoteWinner[i]) { + + // this is a winner for this vote + String choiceId = choiceIds.get(i); + currentVoteWinners.add(choiceId); + } + } + if (log.isDebugEnabled()) { + log.debug("Winners of this vote : " + currentVoteWinners); + } + + + if (firstVoter) { + + // keep this first result + winners = currentVoteWinners; + } else if (!winners.equals(currentVoteWinners)) { + + // current vote has not same winners than previous, + // so will need resolution + winners = null; + + if (log.isDebugEnabled()) { + log.debug("Dismatch winners (will need resolv...)"); + } + } + } + + firstVoter = false; + } + return winners; + } + +} Property changes on: trunk/pollen-votecounting-strategy-condorcet/src/main/java/org/chorem/pollen/votecounting/strategy/CondorcetStrategy.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Deleted: trunk/pollen-votecounting-strategy-condorcet/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.VoteCountingStrategy =================================================================== --- trunk/pollen-votecounting-strategy-condorcet/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.VoteCountingStrategy 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-votecounting-strategy-condorcet/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.VoteCountingStrategy 2012-06-26 03:16:11 UTC (rev 3559) @@ -1 +0,0 @@ -org.chorem.pollen.votecounting.CondorcetStrategy Copied: trunk/pollen-votecounting-strategy-condorcet/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.strategy.VoteCountingStrategy (from rev 3552, trunk/pollen-votecounting-strategy-condorcet/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.VoteCountingStrategy) =================================================================== --- trunk/pollen-votecounting-strategy-condorcet/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.strategy.VoteCountingStrategy (rev 0) +++ trunk/pollen-votecounting-strategy-condorcet/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.strategy.VoteCountingStrategy 2012-06-26 03:16:11 UTC (rev 3559) @@ -0,0 +1 @@ +org.chorem.pollen.votecounting.strategy.CondorcetStrategy Property changes on: trunk/pollen-votecounting-strategy-condorcet/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.strategy.VoteCountingStrategy ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Deleted: trunk/pollen-votecounting-strategy-condorcet/src/test/java/org/chorem/pollen/votecounting/CondorcetStrategyTest.java =================================================================== --- trunk/pollen-votecounting-strategy-condorcet/src/test/java/org/chorem/pollen/votecounting/CondorcetStrategyTest.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-votecounting-strategy-condorcet/src/test/java/org/chorem/pollen/votecounting/CondorcetStrategyTest.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -1,316 +0,0 @@ -/* - * #%L - * Pollen :: VoteCounting strategy :: Condorcet - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 CodeLutin - * %% - * 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% - */ -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.VoteCountingResult; -import org.chorem.pollen.votecounting.model.Voter; -import org.chorem.pollen.votecounting.model.VoterBuilder; -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; - -/** - * Tests the {@link CondorcetStrategy}. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.5 - */ -public class CondorcetStrategyTest { - - public static final String CHOICE_A = "a"; - - public static final String CHOICE_B = "b"; - - public static final String CHOICE_C = "c"; - - protected static VoteCountingStrategyProvider provider; - - protected VoteCountingStrategy strategy; - - @BeforeClass - public static void beforeClass() throws Exception { - provider = new VoteCountingStrategyProvider(); - } - - @Before - public void setUp() throws Exception { - strategy = provider.getStrategy(CondorcetStrategy.ID); - } - - @Test - public void simpleVotecount() throws Exception { - - // Simple poll (all weight to 1) - // 1 (a=1 b=2 c=null) - // 2 (a=1 b=3 c=2) - // 3 (a=1 b=null c=2) - // Result (a=6 b=1 c=2) - - Set<Voter> voters = new VoterBuilder(). - newVoter("1", 1.). - addVoteForChoice(CHOICE_A, 1.). - addVoteForChoice(CHOICE_B, 2.). - addVoteForChoice(CHOICE_C, null). - newVoter("2", 1.). - addVoteForChoice(CHOICE_A, 1.). - addVoteForChoice(CHOICE_B, 3.). - addVoteForChoice(CHOICE_C, 2.). - newVoter("3", 1.). - addVoteForChoice(CHOICE_A, 1.). - addVoteForChoice(CHOICE_B, null). - addVoteForChoice(CHOICE_C, 2.). - getVoters(); - - 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.)); - } - - @Test - public void simpleVotecount0() throws Exception { - - // Simple poll (all weight to 1) - // 1 (a=1 b=2 c=null) - // 2 (a=3 b=2 c=1) - // 3 (a=1 b=null c=2) - // Result (a=4 b=2 c=3) - - Set<Voter> voters = new VoterBuilder(). - newVoter("1", 1.). - addVoteForChoice(CHOICE_A, 1.). - addVoteForChoice(CHOICE_B, 2.). - addVoteForChoice(CHOICE_C, null). - newVoter("2", 1.). - addVoteForChoice(CHOICE_A, 3.). - addVoteForChoice(CHOICE_B, 2.). - addVoteForChoice(CHOICE_C, 1.). - newVoter("3", 1.). - addVoteForChoice(CHOICE_A, 1.). - addVoteForChoice(CHOICE_B, null). - addVoteForChoice(CHOICE_C, 2.). - getVoters(); - - 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.)); - } - - @Test - public void simpleVotecount2() throws Exception { - - // Simple poll (all weight to 1) - // 1 (a=1 b=2 c=null) - // 2 (a=1 b=2 c=1) - // 3 (a=null b=null c=2) - // Result (a=3 b=1 c=3) - - Set<Voter> voters = new VoterBuilder(). - newVoter("1", 1.). - addVoteForChoice(CHOICE_A, 1.). - addVoteForChoice(CHOICE_B, 2.). - addVoteForChoice(CHOICE_C, null). - newVoter("2", 1.). - addVoteForChoice(CHOICE_A, 1.). - addVoteForChoice(CHOICE_B, 2.). - addVoteForChoice(CHOICE_C, 1.). - newVoter("3", 1.). - addVoteForChoice(CHOICE_A, null). - addVoteForChoice(CHOICE_B, null). - addVoteForChoice(CHOICE_C, 2.). - getVoters(); - - 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.)); - } - - @Test - public void simpleVotecount3() throws Exception { - - // Simple poll (all weight to 1) - // 1 (a=1 b=null c=null) - // 2 (a=null b=1 c=null) - // 3 (a=null b=null c=1) - // Result (a=2 b=2 c=2) - - Set<Voter> voters = new VoterBuilder(). - newVoter("1", 1.). - addVoteForChoice(CHOICE_A, 1.). - addVoteForChoice(CHOICE_B, null). - addVoteForChoice(CHOICE_C, null). - newVoter("2", 1.). - addVoteForChoice(CHOICE_A, null). - addVoteForChoice(CHOICE_B, 1.). - addVoteForChoice(CHOICE_C, null). - newVoter("3", 1.). - addVoteForChoice(CHOICE_A, null). - addVoteForChoice(CHOICE_B, null). - addVoteForChoice(CHOICE_C, 1.). - getVoters(); - - 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); - } - - @Test - public void weightedVotecount1() throws Exception { - - // poll with weighted vote - // 1 (x2) (a=1 b=null c=null) - // 2 (x1) (a=null b=1 c=null) - // 3 (x1) (a=null b=1 c=2) - // Result (a=2 b=2 c=1) - - Set<Voter> voters = new VoterBuilder(). - newVoter("1", 2.). - addVoteForChoice(CHOICE_A, 1.). - addVoteForChoice(CHOICE_B, null). - addVoteForChoice(CHOICE_C, null). - newVoter("2", 1.). - addVoteForChoice(CHOICE_A, null). - addVoteForChoice(CHOICE_B, 1.). - addVoteForChoice(CHOICE_C, null). - newVoter("3", 1.). - addVoteForChoice(CHOICE_A, null). - addVoteForChoice(CHOICE_B, 1.). - addVoteForChoice(CHOICE_C, 2.). - getVoters(); - - 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.)); - } - - @Test - public void weightedVotecount2() throws Exception { - - // poll with weighted vote - // 1 (x2) (a=1 b=null c=null) - // 2 (x1) (a=null b=1 c=null) - // 3 (x3) (a=null b=2 c=1) - // Result (a=2 b=4 c=3) - - Set<Voter> voters = new VoterBuilder(). - newVoter("1", 2.). - addVoteForChoice(CHOICE_A, 1.). - addVoteForChoice(CHOICE_B, null). - addVoteForChoice(CHOICE_C, null). - newVoter("2", 1.). - addVoteForChoice(CHOICE_A, null). - addVoteForChoice(CHOICE_B, 1.). - addVoteForChoice(CHOICE_C, null). - newVoter("3", 3.). - addVoteForChoice(CHOICE_A, null). - addVoteForChoice(CHOICE_B, 2.). - addVoteForChoice(CHOICE_C, 1.). - getVoters(); - - 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.)); - } - - 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()); - for (String choiceId : choiceIds) { - - ChoiceScore choiceScore = choicesById.get(choiceId); - Assert.assertNotNull(choiceScore); - assertChoiceScore(choiceScore, choiceId, choiceResult); - } - } - -} Deleted: trunk/pollen-votecounting-strategy-condorcet/src/test/java/org/chorem/pollen/votecounting/VoteCountingStrategyProviderTest.java =================================================================== --- trunk/pollen-votecounting-strategy-condorcet/src/test/java/org/chorem/pollen/votecounting/VoteCountingStrategyProviderTest.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-votecounting-strategy-condorcet/src/test/java/org/chorem/pollen/votecounting/VoteCountingStrategyProviderTest.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -1,49 +0,0 @@ -/* - * #%L - * Pollen :: VoteCounting strategy :: Condorcet - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 CodeLutin - * %% - * 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% - */ -package org.chorem.pollen.votecounting; - -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -/** - * Tests the {@link VoteCountingStrategyProvider}. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.5 - */ -public class VoteCountingStrategyProviderTest { - - protected static VoteCountingStrategyProvider provider; - - @BeforeClass - public static void beforeClass() throws Exception { - provider = new VoteCountingStrategyProvider(); - } - - @Test - public void getStrategy() throws Exception { - - Assert.assertNotNull(provider.getStrategy(CondorcetStrategy.ID)); - } -} Copied: trunk/pollen-votecounting-strategy-condorcet/src/test/java/org/chorem/pollen/votecounting/strategy/CondorcetStrategyTest.java (from rev 3557, trunk/pollen-votecounting-strategy-condorcet/src/test/java/org/chorem/pollen/votecounting/CondorcetStrategyTest.java) =================================================================== --- trunk/pollen-votecounting-strategy-condorcet/src/test/java/org/chorem/pollen/votecounting/strategy/CondorcetStrategyTest.java (rev 0) +++ trunk/pollen-votecounting-strategy-condorcet/src/test/java/org/chorem/pollen/votecounting/strategy/CondorcetStrategyTest.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -0,0 +1,316 @@ +/* + * #%L + * Pollen :: VoteCounting strategy :: Condorcet + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * 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% + */ +package org.chorem.pollen.votecounting.strategy; + +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.VoteCountingResult; +import org.chorem.pollen.votecounting.model.Voter; +import org.chorem.pollen.votecounting.model.VoterBuilder; +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; + +/** + * Tests the {@link CondorcetStrategy}. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.5 + */ +public class CondorcetStrategyTest { + + public static final String CHOICE_A = "a"; + + public static final String CHOICE_B = "b"; + + public static final String CHOICE_C = "c"; + + protected static VoteCountingStrategyProvider provider; + + protected VoteCountingStrategy strategy; + + @BeforeClass + public static void beforeClass() throws Exception { + provider = new VoteCountingStrategyProvider(); + } + + @Before + public void setUp() throws Exception { + strategy = provider.getStrategy(CondorcetStrategy.ID); + } + + @Test + public void simpleVotecount() throws Exception { + + // Simple poll (all weight to 1) + // 1 (a=1 b=2 c=null) + // 2 (a=1 b=3 c=2) + // 3 (a=1 b=null c=2) + // Result (a=6 b=1 c=2) + + Set<Voter> voters = new VoterBuilder(). + newVoter("1", 1.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, 2.). + addVoteForChoice(CHOICE_C, null). + newVoter("2", 1.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, 3.). + addVoteForChoice(CHOICE_C, 2.). + newVoter("3", 1.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, null). + addVoteForChoice(CHOICE_C, 2.). + getVoters(); + + 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.)); + } + + @Test + public void simpleVotecount0() throws Exception { + + // Simple poll (all weight to 1) + // 1 (a=1 b=2 c=null) + // 2 (a=3 b=2 c=1) + // 3 (a=1 b=null c=2) + // Result (a=4 b=2 c=3) + + Set<Voter> voters = new VoterBuilder(). + newVoter("1", 1.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, 2.). + addVoteForChoice(CHOICE_C, null). + newVoter("2", 1.). + addVoteForChoice(CHOICE_A, 3.). + addVoteForChoice(CHOICE_B, 2.). + addVoteForChoice(CHOICE_C, 1.). + newVoter("3", 1.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, null). + addVoteForChoice(CHOICE_C, 2.). + getVoters(); + + 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.)); + } + + @Test + public void simpleVotecount2() throws Exception { + + // Simple poll (all weight to 1) + // 1 (a=1 b=2 c=null) + // 2 (a=1 b=2 c=1) + // 3 (a=null b=null c=2) + // Result (a=3 b=1 c=3) + + Set<Voter> voters = new VoterBuilder(). + newVoter("1", 1.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, 2.). + addVoteForChoice(CHOICE_C, null). + newVoter("2", 1.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, 2.). + addVoteForChoice(CHOICE_C, 1.). + newVoter("3", 1.). + addVoteForChoice(CHOICE_A, null). + addVoteForChoice(CHOICE_B, null). + addVoteForChoice(CHOICE_C, 2.). + getVoters(); + + 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.)); + } + + @Test + public void simpleVotecount3() throws Exception { + + // Simple poll (all weight to 1) + // 1 (a=1 b=null c=null) + // 2 (a=null b=1 c=null) + // 3 (a=null b=null c=1) + // Result (a=2 b=2 c=2) + + Set<Voter> voters = new VoterBuilder(). + newVoter("1", 1.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, null). + addVoteForChoice(CHOICE_C, null). + newVoter("2", 1.). + addVoteForChoice(CHOICE_A, null). + addVoteForChoice(CHOICE_B, 1.). + addVoteForChoice(CHOICE_C, null). + newVoter("3", 1.). + addVoteForChoice(CHOICE_A, null). + addVoteForChoice(CHOICE_B, null). + addVoteForChoice(CHOICE_C, 1.). + getVoters(); + + 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); + } + + @Test + public void weightedVotecount1() throws Exception { + + // poll with weighted vote + // 1 (x2) (a=1 b=null c=null) + // 2 (x1) (a=null b=1 c=null) + // 3 (x1) (a=null b=1 c=2) + // Result (a=2 b=2 c=1) + + Set<Voter> voters = new VoterBuilder(). + newVoter("1", 2.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, null). + addVoteForChoice(CHOICE_C, null). + newVoter("2", 1.). + addVoteForChoice(CHOICE_A, null). + addVoteForChoice(CHOICE_B, 1.). + addVoteForChoice(CHOICE_C, null). + newVoter("3", 1.). + addVoteForChoice(CHOICE_A, null). + addVoteForChoice(CHOICE_B, 1.). + addVoteForChoice(CHOICE_C, 2.). + getVoters(); + + 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.)); + } + + @Test + public void weightedVotecount2() throws Exception { + + // poll with weighted vote + // 1 (x2) (a=1 b=null c=null) + // 2 (x1) (a=null b=1 c=null) + // 3 (x3) (a=null b=2 c=1) + // Result (a=2 b=4 c=3) + + Set<Voter> voters = new VoterBuilder(). + newVoter("1", 2.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, null). + addVoteForChoice(CHOICE_C, null). + newVoter("2", 1.). + addVoteForChoice(CHOICE_A, null). + addVoteForChoice(CHOICE_B, 1.). + addVoteForChoice(CHOICE_C, null). + newVoter("3", 3.). + addVoteForChoice(CHOICE_A, null). + addVoteForChoice(CHOICE_B, 2.). + addVoteForChoice(CHOICE_C, 1.). + getVoters(); + + 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.)); + } + + 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()); + for (String choiceId : choiceIds) { + + ChoiceScore choiceScore = choicesById.get(choiceId); + Assert.assertNotNull(choiceScore); + assertChoiceScore(choiceScore, choiceId, choiceResult); + } + } + +} Property changes on: trunk/pollen-votecounting-strategy-condorcet/src/test/java/org/chorem/pollen/votecounting/strategy/CondorcetStrategyTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: trunk/pollen-votecounting-strategy-condorcet/src/test/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProviderTest.java (from rev 3552, trunk/pollen-votecounting-strategy-condorcet/src/test/java/org/chorem/pollen/votecounting/VoteCountingStrategyProviderTest.java) =================================================================== --- trunk/pollen-votecounting-strategy-condorcet/src/test/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProviderTest.java (rev 0) +++ trunk/pollen-votecounting-strategy-condorcet/src/test/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProviderTest.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -0,0 +1,49 @@ +/* + * #%L + * Pollen :: VoteCounting strategy :: Condorcet + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * 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% + */ +package org.chorem.pollen.votecounting.strategy; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Tests the {@link VoteCountingStrategyProvider}. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.5 + */ +public class VoteCountingStrategyProviderTest { + + protected static VoteCountingStrategyProvider provider; + + @BeforeClass + public static void beforeClass() throws Exception { + provider = new VoteCountingStrategyProvider(); + } + + @Test + public void getStrategy() throws Exception { + + Assert.assertNotNull(provider.getStrategy(CondorcetStrategy.ID)); + } +} Property changes on: trunk/pollen-votecounting-strategy-condorcet/src/test/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProviderTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Deleted: trunk/pollen-votecounting-strategy-normal/src/main/java/org/chorem/pollen/votecounting/NormalStrategy.java =================================================================== --- trunk/pollen-votecounting-strategy-normal/src/main/java/org/chorem/pollen/votecounting/NormalStrategy.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-votecounting-strategy-normal/src/main/java/org/chorem/pollen/votecounting/NormalStrategy.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -1,146 +0,0 @@ -/* - * #%L - * Pollen :: VoteCounting strategy :: Normal - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 CodeLutin - * %% - * 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% - */ -package org.chorem.pollen.votecounting; - -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.Map; -import java.util.Set; - -import static org.nuiton.i18n.I18n.n_; - -/** - * Condorcet. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.5 - */ -public class NormalStrategy extends AbstractVoteCountingStrategy { - - public static final int ID = 0; - - @Override - public int getId() { - return ID; - } - - @Override - public String getI18nName() { - return n_("pollen.voteCountingType.normal"); - } - - @Override - public String getI18nHelp() { - return n_("pollen.voteCountingType.normal.help"); - } - - @Override - public VoteCountingResult votecount(Set<Voter> voters) { - - // get empty result by choice - Map<String, ChoiceScore> resultByChoice = votersToResult(voters); - - for (Voter voter : voters) { - // add this voter votes to result - addVoterChoices(voter, resultByChoice); - } - // transform map of result to list of them (and sort them) - VoteCountingResult result = resultToList(null, resultByChoice); - return result; - } - - @Override - public String getDisplayVoteValue(Integer voteValue) { - return voteValue != null ? "OK" : ""; - } - - @Override - public boolean isChoiceInVote(Integer voteValue) { - return voteValue != null && voteValue > 0; - } - - @Override - public boolean isVoteValueNull(Integer voteValue) { - return voteValue == null || voteValue == 0; - } - - @Override - public ChoiceToVoteRenderType getRenderType() { - return ChoiceToVoteRenderType.CHECKBOX; - } - - @Override - public boolean isDisplayResultsByChoice() { - return false; - } - - @Override - public boolean isVoteValueValid(Integer voteValue) { - // no validation on not null vote value - return true; - } - - @Override - public boolean isTotalVoteValueValid(int totalValues) { - // no validation on total value - return true; - } - - @Override - public String getVoteValueNotValidMessage() { - // no validation on not null vote value, so no message - return null; - } - - @Override - public String getTotalVoteValueNotValidMessage() { - // no validation on total values, so no message - return null; - } - - public void addVoterChoices(Voter voter, - Map<String, ChoiceScore> resultByChoice) { - - double voterWeight = voter.getWeight(); - - for (VoteForChoice voteForChoice : voter.getVoteForChoices()) { - Double voteValue = voteForChoice.getVoteValue(); - if (voteValue != null) { - - // get score for choice - String choiceId = voteForChoice.getChoiceId(); - ChoiceScore choiceScore = resultByChoice.get(choiceId); - - // compute score to add - double scoreToAdd = voteValue * voterWeight; - - // add to score this weighted vote - choiceScore.addScoreValue(scoreToAdd); - } - } - } - -} Copied: trunk/pollen-votecounting-strategy-normal/src/main/java/org/chorem/pollen/votecounting/strategy/NormalStrategy.java (from rev 3549, trunk/pollen-votecounting-strategy-normal/src/main/java/org/chorem/pollen/votecounting/NormalStrategy.java) =================================================================== --- trunk/pollen-votecounting-strategy-normal/src/main/java/org/chorem/pollen/votecounting/strategy/NormalStrategy.java (rev 0) +++ trunk/pollen-votecounting-strategy-normal/src/main/java/org/chorem/pollen/votecounting/strategy/NormalStrategy.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -0,0 +1,147 @@ +/* + * #%L + * Pollen :: VoteCounting strategy :: Normal + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * 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% + */ +package org.chorem.pollen.votecounting.strategy; + +import org.chorem.pollen.votecounting.model.ChoiceScore; +import org.chorem.pollen.votecounting.model.ChoiceToVoteRenderType; +import org.chorem.pollen.votecounting.model.VoteCountingResult; +import org.chorem.pollen.votecounting.model.VoteForChoice; +import org.chorem.pollen.votecounting.model.Voter; + +import java.util.Map; +import java.util.Set; + +import static org.nuiton.i18n.I18n.n_; + +/** + * Condorcet. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.5 + */ +public class NormalStrategy extends AbstractVoteCountingStrategy { + + public static final int ID = 0; + + @Override + public int getId() { + return ID; + } + + @Override + public String getI18nName() { + return n_("pollen.voteCountingType.normal"); + } + + @Override + public String getI18nHelp() { + return n_("pollen.voteCountingType.normal.help"); + } + + @Override + public VoteCountingResult votecount(Set<Voter> voters) { + + // get empty result by choice + Map<String, ChoiceScore> resultByChoice = votersToResult(voters); + + for (Voter voter : voters) { + // add this voter votes to result + addVoterChoices(voter, resultByChoice); + } + // transform map of result to list of them (and sort them) + VoteCountingResult result = resultToList(resultByChoice); + return result; + } + + @Override + public String getDisplayVoteValue(Integer voteValue) { + return voteValue != null ? "OK" : ""; + } + + @Override + public boolean isChoiceInVote(Integer voteValue) { + return voteValue != null && voteValue > 0; + } + + @Override + public boolean isVoteValueNull(Integer voteValue) { + return voteValue == null || voteValue == 0; + } + + @Override + public ChoiceToVoteRenderType getRenderType() { + return ChoiceToVoteRenderType.CHECKBOX; + } + + @Override + public boolean isDisplayResultsByChoice() { + return false; + } + + @Override + public boolean isVoteValueValid(Integer voteValue) { + // no validation on not null vote value + return true; + } + + @Override + public boolean isTotalVoteValueValid(int totalValues) { + // no validation on total value + return true; + } + + @Override + public String getVoteValueNotValidMessage() { + // no validation on not null vote value, so no message + return null; + } + + @Override + public String getTotalVoteValueNotValidMessage() { + // no validation on total values, so no message + return null; + } + + public void addVoterChoices(Voter voter, + Map<String, ChoiceScore> resultByChoice) { + + double voterWeight = voter.getWeight(); + + for (VoteForChoice voteForChoice : voter.getVoteForChoices()) { + Double voteValue = voteForChoice.getVoteValue(); + if (voteValue != null) { + + // get score for choice + String choiceId = voteForChoice.getChoiceId(); + ChoiceScore choiceScore = resultByChoice.get(choiceId); + + // compute score to add + double scoreToAdd = voteValue * voterWeight; + + // add to score this weighted vote + choiceScore.addScoreValue(scoreToAdd); + } + } + } + +} Property changes on: trunk/pollen-votecounting-strategy-normal/src/main/java/org/chorem/pollen/votecounting/strategy/NormalStrategy.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Deleted: trunk/pollen-votecounting-strategy-normal/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.VoteCountingStrategy =================================================================== --- trunk/pollen-votecounting-strategy-normal/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.VoteCountingStrategy 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-votecounting-strategy-normal/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.VoteCountingStrategy 2012-06-26 03:16:11 UTC (rev 3559) @@ -1 +0,0 @@ -org.chorem.pollen.votecounting.NormalStrategy Copied: trunk/pollen-votecounting-strategy-normal/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.strategy.VoteCountingStrategy (from rev 3549, trunk/pollen-votecounting-strategy-normal/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.VoteCountingStrategy) =================================================================== --- trunk/pollen-votecounting-strategy-normal/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.strategy.VoteCountingStrategy (rev 0) +++ trunk/pollen-votecounting-strategy-normal/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.strategy.VoteCountingStrategy 2012-06-26 03:16:11 UTC (rev 3559) @@ -0,0 +1 @@ +org.chorem.pollen.votecounting.strategy.NormalStrategy Property changes on: trunk/pollen-votecounting-strategy-normal/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.strategy.VoteCountingStrategy ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Deleted: trunk/pollen-votecounting-strategy-normal/src/test/java/org/chorem/pollen/votecounting/NormalStrategyTest.java =================================================================== --- trunk/pollen-votecounting-strategy-normal/src/test/java/org/chorem/pollen/votecounting/NormalStrategyTest.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-votecounting-strategy-normal/src/test/java/org/chorem/pollen/votecounting/NormalStrategyTest.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -1,276 +0,0 @@ -/* - * #%L - * Pollen :: VoteCounting strategy :: Normal - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 CodeLutin - * %% - * 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% - */ -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.VoteCountingResult; -import org.chorem.pollen.votecounting.model.Voter; -import org.chorem.pollen.votecounting.model.VoterBuilder; -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; - -/** - * Tests the {@link NormalStrategy}. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.5 - */ -public class NormalStrategyTest { - - public static final String CHOICE_A = "a"; - - public static final String CHOICE_B = "b"; - - public static final String CHOICE_C = "c"; - - protected static VoteCountingStrategyProvider provider; - - protected VoteCountingStrategy strategy; - - @BeforeClass - public static void beforeClass() throws Exception { - provider = new VoteCountingStrategyProvider(); - } - - @Before - public void setUp() throws Exception { - strategy = provider.getStrategy(NormalStrategy.ID); - } - - @Test - public void simpleVotecount() throws Exception { - - // Simple poll (all weight to 1) - // 1 (a=1 b=null c=null) - // 2 (a=null b=1 c=null) - // 3 (a=null b=1 c=1) - // Result (a=1 b=2 c=1) - - Set<Voter> voters = new VoterBuilder(). - newVoter("1", 1.). - addVoteForChoice(CHOICE_A, 1.). - addVoteForChoice(CHOICE_B, null). - addVoteForChoice(CHOICE_C, null). - newVoter("2", 1.). - addVoteForChoice(CHOICE_A, null). - addVoteForChoice(CHOICE_B, 1.). - addVoteForChoice(CHOICE_C, null). - newVoter("3", 1.). - addVoteForChoice(CHOICE_A, null). - addVoteForChoice(CHOICE_B, 1.). - addVoteForChoice(CHOICE_C, 1.). - getVoters(); - - 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.)); - - assertChoiceScoreEquals(BigDecimal.valueOf(1.), - Sets.newHashSet(scores.get(1), - scores.get(2)), - CHOICE_A, CHOICE_C); - } - - @Test - public void simpleVotecount2() throws Exception { - - // Simple poll (all weight to 1) - // 1 (a=1 b=null c=null) - // 2 (a=1 b=1 c=null) - // 3 (a=1 b=1 c=null) - // Result (a=3 b=2 c=null) - Set<Voter> voters = new VoterBuilder(). - newVoter("1", 1.). - addVoteForChoice(CHOICE_A, 1.). - addVoteForChoice(CHOICE_B, null). - addVoteForChoice(CHOICE_C, null). - newVoter("2", 1.). - addVoteForChoice(CHOICE_A, 1.). - addVoteForChoice(CHOICE_B, 1.). - addVoteForChoice(CHOICE_C, null). - newVoter("3", 1.). - addVoteForChoice(CHOICE_A, 1.). - addVoteForChoice(CHOICE_B, 1.). - addVoteForChoice(CHOICE_C, null). - getVoters(); - - 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); - } - - @Test - public void simpleVotecount3() throws Exception { - - // Simple poll (all weight to 1) - // 1 (a=1 b=1 c=null) - // 2 (a=1 b=1 c=null) - // 3 (a=1 b=1 c=null) - // Result (a=3 b=3 c=null) - Set<Voter> voters = new VoterBuilder(). - newVoter("1", 1.). - addVoteForChoice(CHOICE_A, 1.). - addVoteForChoice(CHOICE_B, 1.). - addVoteForChoice(CHOICE_C, null). - newVoter("2", 1.). - addVoteForChoice(CHOICE_A, 1.). - addVoteForChoice(CHOICE_B, 1.). - addVoteForChoice(CHOICE_C, null). - newVoter("3", 1.). - addVoteForChoice(CHOICE_A, 1.). - addVoteForChoice(CHOICE_B, 1.). - addVoteForChoice(CHOICE_C, null). - getVoters(); - - 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); - } - - @Test - public void weightedVotecount1() throws Exception { - - // poll with weighted vote - // 1 (x2) (a=1 b=null c=null) - // 2 (x1) (a=null b=1 c=null) - // 3 (x1) (a=null b=1 c=1) - // Result (a=2 b=2 c=1) - Set<Voter> voters = new VoterBuilder(). - newVoter("1", 2.). - addVoteForChoice(CHOICE_A, 1.). - addVoteForChoice(CHOICE_B, null). - addVoteForChoice(CHOICE_C, null). - newVoter("2", 1.). - addVoteForChoice(CHOICE_A, null). - addVoteForChoice(CHOICE_B, 1.). - addVoteForChoice(CHOICE_C, null). - newVoter("3", 1.). - addVoteForChoice(CHOICE_A, null). - addVoteForChoice(CHOICE_B, 1.). - addVoteForChoice(CHOICE_C, 1.). - getVoters(); - - 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.)); - } - - @Test - public void weightedVotecount2() throws Exception { - - // poll with weighted vote - // 1 (x2) (a=1 b=null c=null) - // 2 (x1) (a=null b=1 c=null) - // 3 (x3) (a=null b=1 c=1) - // Result (a=2 b=4 c=3) - Set<Voter> voters = new VoterBuilder(). - newVoter("1", 2.). - addVoteForChoice(CHOICE_A, 1.). - addVoteForChoice(CHOICE_B, null). - addVoteForChoice(CHOICE_C, null). - newVoter("2", 1.). - addVoteForChoice(CHOICE_A, null). - addVoteForChoice(CHOICE_B, 1.). - addVoteForChoice(CHOICE_C, null). - newVoter("3", 3.). - addVoteForChoice(CHOICE_A, null). - addVoteForChoice(CHOICE_B, 1.). - addVoteForChoice(CHOICE_C, 1.). - getVoters(); - - 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.)); - } - - 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()); - for (String choiceId : choiceIds) { - - ChoiceScore choiceScore = choicesById.get(choiceId); - Assert.assertNotNull(choiceScore); - assertChoiceScore(choiceScore, choiceId, choiceResult); - } - } - -} Deleted: trunk/pollen-votecounting-strategy-normal/src/test/java/org/chorem/pollen/votecounting/VoteCountingStrategyProviderTest.java =================================================================== --- trunk/pollen-votecounting-strategy-normal/src/test/java/org/chorem/pollen/votecounting/VoteCountingStrategyProviderTest.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-votecounting-strategy-normal/src/test/java/org/chorem/pollen/votecounting/VoteCountingStrategyProviderTest.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -1,49 +0,0 @@ -/* - * #%L - * Pollen :: VoteCounting strategy :: Normal - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 CodeLutin - * %% - * 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% - */ -package org.chorem.pollen.votecounting; - -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -/** - * Tests the {@link VoteCountingStrategyProvider}. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.5 - */ -public class VoteCountingStrategyProviderTest { - - protected static VoteCountingStrategyProvider provider; - - @BeforeClass - public static void beforeClass() throws Exception { - provider = new VoteCountingStrategyProvider(); - } - - @Test - public void getStrategy() throws Exception { - - Assert.assertNotNull(provider.getStrategy(NormalStrategy.ID)); - } -} Copied: trunk/pollen-votecounting-strategy-normal/src/test/java/org/chorem/pollen/votecounting/strategy/NormalStrategyTest.java (from rev 3558, trunk/pollen-votecounting-strategy-normal/src/test/java/org/chorem/pollen/votecounting/NormalStrategyTest.java) =================================================================== --- trunk/pollen-votecounting-strategy-normal/src/test/java/org/chorem/pollen/votecounting/strategy/NormalStrategyTest.java (rev 0) +++ trunk/pollen-votecounting-strategy-normal/src/test/java/org/chorem/pollen/votecounting/strategy/NormalStrategyTest.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -0,0 +1,276 @@ +/* + * #%L + * Pollen :: VoteCounting strategy :: Normal + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * 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% + */ +package org.chorem.pollen.votecounting.strategy; + +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.VoteCountingResult; +import org.chorem.pollen.votecounting.model.Voter; +import org.chorem.pollen.votecounting.model.VoterBuilder; +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; + +/** + * Tests the {@link NormalStrategy}. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.5 + */ +public class NormalStrategyTest { + + public static final String CHOICE_A = "a"; + + public static final String CHOICE_B = "b"; + + public static final String CHOICE_C = "c"; + + protected static VoteCountingStrategyProvider provider; + + protected VoteCountingStrategy strategy; + + @BeforeClass + public static void beforeClass() throws Exception { + provider = new VoteCountingStrategyProvider(); + } + + @Before + public void setUp() throws Exception { + strategy = provider.getStrategy(NormalStrategy.ID); + } + + @Test + public void simpleVotecount() throws Exception { + + // Simple poll (all weight to 1) + // 1 (a=1 b=null c=null) + // 2 (a=null b=1 c=null) + // 3 (a=null b=1 c=1) + // Result (a=1 b=2 c=1) + + Set<Voter> voters = new VoterBuilder(). + newVoter("1", 1.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, null). + addVoteForChoice(CHOICE_C, null). + newVoter("2", 1.). + addVoteForChoice(CHOICE_A, null). + addVoteForChoice(CHOICE_B, 1.). + addVoteForChoice(CHOICE_C, null). + newVoter("3", 1.). + addVoteForChoice(CHOICE_A, null). + addVoteForChoice(CHOICE_B, 1.). + addVoteForChoice(CHOICE_C, 1.). + getVoters(); + + 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.)); + + assertChoiceScoreEquals(BigDecimal.valueOf(1.), + Sets.newHashSet(scores.get(1), + scores.get(2)), + CHOICE_A, CHOICE_C); + } + + @Test + public void simpleVotecount2() throws Exception { + + // Simple poll (all weight to 1) + // 1 (a=1 b=null c=null) + // 2 (a=1 b=1 c=null) + // 3 (a=1 b=1 c=null) + // Result (a=3 b=2 c=null) + Set<Voter> voters = new VoterBuilder(). + newVoter("1", 1.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, null). + addVoteForChoice(CHOICE_C, null). + newVoter("2", 1.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, 1.). + addVoteForChoice(CHOICE_C, null). + newVoter("3", 1.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, 1.). + addVoteForChoice(CHOICE_C, null). + getVoters(); + + 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); + } + + @Test + public void simpleVotecount3() throws Exception { + + // Simple poll (all weight to 1) + // 1 (a=1 b=1 c=null) + // 2 (a=1 b=1 c=null) + // 3 (a=1 b=1 c=null) + // Result (a=3 b=3 c=null) + Set<Voter> voters = new VoterBuilder(). + newVoter("1", 1.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, 1.). + addVoteForChoice(CHOICE_C, null). + newVoter("2", 1.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, 1.). + addVoteForChoice(CHOICE_C, null). + newVoter("3", 1.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, 1.). + addVoteForChoice(CHOICE_C, null). + getVoters(); + + 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); + } + + @Test + public void weightedVotecount1() throws Exception { + + // poll with weighted vote + // 1 (x2) (a=1 b=null c=null) + // 2 (x1) (a=null b=1 c=null) + // 3 (x1) (a=null b=1 c=1) + // Result (a=2 b=2 c=1) + Set<Voter> voters = new VoterBuilder(). + newVoter("1", 2.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, null). + addVoteForChoice(CHOICE_C, null). + newVoter("2", 1.). + addVoteForChoice(CHOICE_A, null). + addVoteForChoice(CHOICE_B, 1.). + addVoteForChoice(CHOICE_C, null). + newVoter("3", 1.). + addVoteForChoice(CHOICE_A, null). + addVoteForChoice(CHOICE_B, 1.). + addVoteForChoice(CHOICE_C, 1.). + getVoters(); + + 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.)); + } + + @Test + public void weightedVotecount2() throws Exception { + + // poll with weighted vote + // 1 (x2) (a=1 b=null c=null) + // 2 (x1) (a=null b=1 c=null) + // 3 (x3) (a=null b=1 c=1) + // Result (a=2 b=4 c=3) + Set<Voter> voters = new VoterBuilder(). + newVoter("1", 2.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, null). + addVoteForChoice(CHOICE_C, null). + newVoter("2", 1.). + addVoteForChoice(CHOICE_A, null). + addVoteForChoice(CHOICE_B, 1.). + addVoteForChoice(CHOICE_C, null). + newVoter("3", 3.). + addVoteForChoice(CHOICE_A, null). + addVoteForChoice(CHOICE_B, 1.). + addVoteForChoice(CHOICE_C, 1.). + getVoters(); + + 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.)); + } + + 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()); + for (String choiceId : choiceIds) { + + ChoiceScore choiceScore = choicesById.get(choiceId); + Assert.assertNotNull(choiceScore); + assertChoiceScore(choiceScore, choiceId, choiceResult); + } + } + +} Property changes on: trunk/pollen-votecounting-strategy-normal/src/test/java/org/chorem/pollen/votecounting/strategy/NormalStrategyTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: trunk/pollen-votecounting-strategy-normal/src/test/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProviderTest.java (from rev 3549, trunk/pollen-votecounting-strategy-normal/src/test/java/org/chorem/pollen/votecounting/VoteCountingStrategyProviderTest.java) =================================================================== --- trunk/pollen-votecounting-strategy-normal/src/test/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProviderTest.java (rev 0) +++ trunk/pollen-votecounting-strategy-normal/src/test/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProviderTest.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -0,0 +1,49 @@ +/* + * #%L + * Pollen :: VoteCounting strategy :: Normal + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * 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% + */ +package org.chorem.pollen.votecounting.strategy; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Tests the {@link VoteCountingStrategyProvider}. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.5 + */ +public class VoteCountingStrategyProviderTest { + + protected static VoteCountingStrategyProvider provider; + + @BeforeClass + public static void beforeClass() throws Exception { + provider = new VoteCountingStrategyProvider(); + } + + @Test + public void getStrategy() throws Exception { + + Assert.assertNotNull(provider.getStrategy(NormalStrategy.ID)); + } +} Property changes on: trunk/pollen-votecounting-strategy-normal/src/test/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProviderTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Deleted: trunk/pollen-votecounting-strategy-number/src/main/java/org/chorem/pollen/votecounting/NumberStrategy.java =================================================================== --- trunk/pollen-votecounting-strategy-number/src/main/java/org/chorem/pollen/votecounting/NumberStrategy.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-votecounting-strategy-number/src/main/java/org/chorem/pollen/votecounting/NumberStrategy.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -1,145 +0,0 @@ -/* - * #%L - * Pollen :: VoteCounting strategy :: Number - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 CodeLutin - * %% - * 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% - */ -package org.chorem.pollen.votecounting; - -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.Map; -import java.util.Set; - -import static org.nuiton.i18n.I18n.n_; - -/** - * Number strategy. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.5 - */ -public class NumberStrategy extends AbstractVoteCountingStrategy { - - public static final int ID = 3; - - @Override - public int getId() { - return ID; - } - - @Override - public String getI18nName() { - return n_("pollen.voteCountingType.number"); - } - - @Override - public String getI18nHelp() { - return n_("pollen.voteCountingType.number.help"); - } - - @Override - public VoteCountingResult votecount(Set<Voter> voters) { - - // get empty result by choice - Map<String, ChoiceScore> resultByChoice = votersToResult(voters); - - for (Voter voter : voters) { - // add this voter votes to result - addVoterChoices(voter, resultByChoice); - } - // transform map of result to list of them (and sort them) - VoteCountingResult result = resultToList(null, resultByChoice); - return result; - } - - @Override - public String getDisplayVoteValue(Integer voteValue) { - return voteValue == null ? "" : String.valueOf(voteValue); - } - - @Override - public boolean isChoiceInVote(Integer voteValue) { - return voteValue != null && voteValue >= 0; - } - - @Override - public boolean isVoteValueNull(Integer voteValue) { - return voteValue == null || voteValue == 0; - } - - @Override - public ChoiceToVoteRenderType getRenderType() { - return ChoiceToVoteRenderType.TEXTFIELD; - } - - @Override - public boolean isDisplayResultsByChoice() { - return true; - } - - @Override - public boolean isVoteValueValid(Integer voteValue) { - // no validation on not null vote value - return true; - } - - @Override - public boolean isTotalVoteValueValid(int totalValues) { - // no validation on total value - return true; - } - - @Override - public String getTotalVoteValueNotValidMessage() { - // no validation on total values, so no message - return null; - } - - @Override - public String getVoteValueNotValidMessage() { - // no validation on not null vote value, so no message - return null; - } - - public void addVoterChoices(Voter voter, - Map<String, ChoiceScore> resultByChoice) { - - double voterWeight = voter.getWeight(); - - for (VoteForChoice voteForChoice : voter.getVoteForChoices()) { - Double voteValue = voteForChoice.getVoteValue(); - if (voteValue != null) { - - // get score for choice - String choiceId = voteForChoice.getChoiceId(); - ChoiceScore choiceScore = resultByChoice.get(choiceId); - - // compute score to add - double scoreToAdd = voteValue * voterWeight; - - // add to score this weighted vote - choiceScore.addScoreValue(scoreToAdd); - } - } - } -} Copied: trunk/pollen-votecounting-strategy-number/src/main/java/org/chorem/pollen/votecounting/strategy/NumberStrategy.java (from rev 3550, trunk/pollen-votecounting-strategy-number/src/main/java/org/chorem/pollen/votecounting/NumberStrategy.java) =================================================================== --- trunk/pollen-votecounting-strategy-number/src/main/java/org/chorem/pollen/votecounting/strategy/NumberStrategy.java (rev 0) +++ trunk/pollen-votecounting-strategy-number/src/main/java/org/chorem/pollen/votecounting/strategy/NumberStrategy.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -0,0 +1,146 @@ +/* + * #%L + * Pollen :: VoteCounting strategy :: Number + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * 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% + */ +package org.chorem.pollen.votecounting.strategy; + +import org.chorem.pollen.votecounting.model.ChoiceScore; +import org.chorem.pollen.votecounting.model.ChoiceToVoteRenderType; +import org.chorem.pollen.votecounting.model.VoteCountingResult; +import org.chorem.pollen.votecounting.model.VoteForChoice; +import org.chorem.pollen.votecounting.model.Voter; + +import java.util.Map; +import java.util.Set; + +import static org.nuiton.i18n.I18n.n_; + +/** + * Number strategy. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.5 + */ +public class NumberStrategy extends AbstractVoteCountingStrategy { + + public static final int ID = 3; + + @Override + public int getId() { + return ID; + } + + @Override + public String getI18nName() { + return n_("pollen.voteCountingType.number"); + } + + @Override + public String getI18nHelp() { + return n_("pollen.voteCountingType.number.help"); + } + + @Override + public VoteCountingResult votecount(Set<Voter> voters) { + + // get empty result by choice + Map<String, ChoiceScore> resultByChoice = votersToResult(voters); + + for (Voter voter : voters) { + // add this voter votes to result + addVoterChoices(voter, resultByChoice); + } + // transform map of result to list of them (and sort them) + VoteCountingResult result = resultToList(resultByChoice); + return result; + } + + @Override + public String getDisplayVoteValue(Integer voteValue) { + return voteValue == null ? "" : String.valueOf(voteValue); + } + + @Override + public boolean isChoiceInVote(Integer voteValue) { + return voteValue != null && voteValue >= 0; + } + + @Override + public boolean isVoteValueNull(Integer voteValue) { + return voteValue == null || voteValue == 0; + } + + @Override + public ChoiceToVoteRenderType getRenderType() { + return ChoiceToVoteRenderType.TEXTFIELD; + } + + @Override + public boolean isDisplayResultsByChoice() { + return true; + } + + @Override + public boolean isVoteValueValid(Integer voteValue) { + // no validation on not null vote value + return true; + } + + @Override + public boolean isTotalVoteValueValid(int totalValues) { + // no validation on total value + return true; + } + + @Override + public String getTotalVoteValueNotValidMessage() { + // no validation on total values, so no message + return null; + } + + @Override + public String getVoteValueNotValidMessage() { + // no validation on not null vote value, so no message + return null; + } + + public void addVoterChoices(Voter voter, + Map<String, ChoiceScore> resultByChoice) { + + double voterWeight = voter.getWeight(); + + for (VoteForChoice voteForChoice : voter.getVoteForChoices()) { + Double voteValue = voteForChoice.getVoteValue(); + if (voteValue != null) { + + // get score for choice + String choiceId = voteForChoice.getChoiceId(); + ChoiceScore choiceScore = resultByChoice.get(choiceId); + + // compute score to add + double scoreToAdd = voteValue * voterWeight; + + // add to score this weighted vote + choiceScore.addScoreValue(scoreToAdd); + } + } + } +} Property changes on: trunk/pollen-votecounting-strategy-number/src/main/java/org/chorem/pollen/votecounting/strategy/NumberStrategy.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Deleted: trunk/pollen-votecounting-strategy-number/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.VoteCountingStrategy =================================================================== --- trunk/pollen-votecounting-strategy-number/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.VoteCountingStrategy 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-votecounting-strategy-number/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.VoteCountingStrategy 2012-06-26 03:16:11 UTC (rev 3559) @@ -1 +0,0 @@ -org.chorem.pollen.votecounting.NumberStrategy Copied: trunk/pollen-votecounting-strategy-number/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.strategy.VoteCountingStrategy (from rev 3550, trunk/pollen-votecounting-strategy-number/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.VoteCountingStrategy) =================================================================== --- trunk/pollen-votecounting-strategy-number/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.strategy.VoteCountingStrategy (rev 0) +++ trunk/pollen-votecounting-strategy-number/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.strategy.VoteCountingStrategy 2012-06-26 03:16:11 UTC (rev 3559) @@ -0,0 +1 @@ +org.chorem.pollen.votecounting.strategy.NumberStrategy Property changes on: trunk/pollen-votecounting-strategy-number/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.strategy.VoteCountingStrategy ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Deleted: trunk/pollen-votecounting-strategy-number/src/test/java/org/chorem/pollen/votecounting/NumberStrategyTest.java =================================================================== --- trunk/pollen-votecounting-strategy-number/src/test/java/org/chorem/pollen/votecounting/NumberStrategyTest.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-votecounting-strategy-number/src/test/java/org/chorem/pollen/votecounting/NumberStrategyTest.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -1,273 +0,0 @@ -/* - * #%L - * Pollen :: VoteCounting strategy :: Number - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 CodeLutin - * %% - * 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% - */ -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.VoteCountingResult; -import org.chorem.pollen.votecounting.model.Voter; -import org.chorem.pollen.votecounting.model.VoterBuilder; -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; - -/** - * Tests the {@link NumberStrategy}. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.5 - */ -public class NumberStrategyTest { - - public static final String CHOICE_A = "a"; - - public static final String CHOICE_B = "b"; - - public static final String CHOICE_C = "c"; - - protected static VoteCountingStrategyProvider provider; - - protected VoteCountingStrategy strategy; - - @BeforeClass - public static void beforeClass() throws Exception { - provider = new VoteCountingStrategyProvider(); - } - - @Before - public void setUp() throws Exception { - strategy = provider.getStrategy(NumberStrategy.ID); - } - - @Test - public void simpleVotecount() throws Exception { - - // Simple poll (all weight to 1) - // 1 (a=1 b=2 c=null) - // 2 (a=2 b=null c=3) - // 3 (a=7 b=12 c=20) - // Result (a=10 b=14 c=23) - - Set<Voter> voters = new VoterBuilder(). - newVoter("1", 1.). - addVoteForChoice(CHOICE_A, 1.). - addVoteForChoice(CHOICE_B, 2.). - addVoteForChoice(CHOICE_C, null). - newVoter("2", 1.). - addVoteForChoice(CHOICE_A, 2.). - addVoteForChoice(CHOICE_B, null). - addVoteForChoice(CHOICE_C, 3.). - newVoter("3", 1.). - addVoteForChoice(CHOICE_A, 7.). - addVoteForChoice(CHOICE_B, 12.). - addVoteForChoice(CHOICE_C, 20.). - getVoters(); - - 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.)); - } - - @Test - public void simpleVotecount2() throws Exception { - - // Simple poll (all weight to 1) - // 1 (a=1 b=null c=null) - // 2 (a=1 b=1 c=null) - // 3 (a=1 b=1 c=null) - // Result (a=3 b=2 c=null) - Set<Voter> voters = new VoterBuilder(). - newVoter("1", 1.). - addVoteForChoice(CHOICE_A, 1.). - addVoteForChoice(CHOICE_B, null). - addVoteForChoice(CHOICE_C, null). - newVoter("2", 1.). - addVoteForChoice(CHOICE_A, 1.). - addVoteForChoice(CHOICE_B, 1.). - addVoteForChoice(CHOICE_C, null). - newVoter("3", 1.). - addVoteForChoice(CHOICE_A, 1.). - addVoteForChoice(CHOICE_B, 1.). - addVoteForChoice(CHOICE_C, null). - getVoters(); - - 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); - } - - @Test - public void simpleVotecount3() throws Exception { - - // Simple poll (all weight to 1) - // 1 (a=1 b=1 c=null) - // 2 (a=1 b=1 c=null) - // 3 (a=1 b=1 c=null) - // Result (a=3 b=3 c=null) - Set<Voter> voters = new VoterBuilder(). - newVoter("1", 1.). - addVoteForChoice(CHOICE_A, 1.). - addVoteForChoice(CHOICE_B, 1.). - addVoteForChoice(CHOICE_C, null). - newVoter("2", 1.). - addVoteForChoice(CHOICE_A, 1.). - addVoteForChoice(CHOICE_B, 1.). - addVoteForChoice(CHOICE_C, null). - newVoter("3", 1.). - addVoteForChoice(CHOICE_A, 1.). - addVoteForChoice(CHOICE_B, 1.). - addVoteForChoice(CHOICE_C, null). - getVoters(); - - 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); - } - - @Test - public void weightedVotecount1() throws Exception { - - // poll with weighted vote - // 1 (x2) (a=1 b=null c=null) - // 2 (x1) (a=null b=1 c=null) - // 3 (x1) (a=null b=1 c=1) - // Result (a=2 b=2 c=1) - Set<Voter> voters = new VoterBuilder(). - newVoter("1", 2.). - addVoteForChoice(CHOICE_A, 1.). - addVoteForChoice(CHOICE_B, null). - addVoteForChoice(CHOICE_C, null). - newVoter("2", 1.). - addVoteForChoice(CHOICE_A, null). - addVoteForChoice(CHOICE_B, 1.). - addVoteForChoice(CHOICE_C, null). - newVoter("3", 1.). - addVoteForChoice(CHOICE_A, null). - addVoteForChoice(CHOICE_B, 1.). - addVoteForChoice(CHOICE_C, 1.). - getVoters(); - - 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.)); - } - - @Test - public void weightedVotecount2() throws Exception { - - // poll with weighted vote - // 1 (x2) (a=1 b=null c=null) - // 2 (x1) (a=null b=1 c=null) - // 3 (x3) (a=null b=1 c=1) - // Result (a=2 b=4 c=3) - Set<Voter> voters = new VoterBuilder(). - newVoter("1", 2.). - addVoteForChoice(CHOICE_A, 1.). - addVoteForChoice(CHOICE_B, null). - addVoteForChoice(CHOICE_C, null). - newVoter("2", 1.). - addVoteForChoice(CHOICE_A, null). - addVoteForChoice(CHOICE_B, 1.). - addVoteForChoice(CHOICE_C, null). - newVoter("3", 3.). - addVoteForChoice(CHOICE_A, null). - addVoteForChoice(CHOICE_B, 1.). - addVoteForChoice(CHOICE_C, 1.). - getVoters(); - - 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.)); - } - - 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()); - for (String choiceId : choiceIds) { - - ChoiceScore choiceScore = choicesById.get(choiceId); - Assert.assertNotNull(choiceScore); - assertChoiceScore(choiceScore, choiceId, choiceResult); - } - } - -} Deleted: trunk/pollen-votecounting-strategy-number/src/test/java/org/chorem/pollen/votecounting/VoteCountingStrategyProviderTest.java =================================================================== --- trunk/pollen-votecounting-strategy-number/src/test/java/org/chorem/pollen/votecounting/VoteCountingStrategyProviderTest.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-votecounting-strategy-number/src/test/java/org/chorem/pollen/votecounting/VoteCountingStrategyProviderTest.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -1,49 +0,0 @@ -/* - * #%L - * Pollen :: VoteCounting strategy :: Number - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 CodeLutin - * %% - * 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% - */ -package org.chorem.pollen.votecounting; - -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -/** - * Tests the {@link VoteCountingStrategyProvider}. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.5 - */ -public class VoteCountingStrategyProviderTest { - - protected static VoteCountingStrategyProvider provider; - - @BeforeClass - public static void beforeClass() throws Exception { - provider = new VoteCountingStrategyProvider(); - } - - @Test - public void getStrategy() throws Exception { - - Assert.assertNotNull(provider.getStrategy(NumberStrategy.ID)); - } -} Copied: trunk/pollen-votecounting-strategy-number/src/test/java/org/chorem/pollen/votecounting/strategy/NumberStrategyTest.java (from rev 3558, trunk/pollen-votecounting-strategy-number/src/test/java/org/chorem/pollen/votecounting/NumberStrategyTest.java) =================================================================== --- trunk/pollen-votecounting-strategy-number/src/test/java/org/chorem/pollen/votecounting/strategy/NumberStrategyTest.java (rev 0) +++ trunk/pollen-votecounting-strategy-number/src/test/java/org/chorem/pollen/votecounting/strategy/NumberStrategyTest.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -0,0 +1,273 @@ +/* + * #%L + * Pollen :: VoteCounting strategy :: Number + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * 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% + */ +package org.chorem.pollen.votecounting.strategy; + +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.VoteCountingResult; +import org.chorem.pollen.votecounting.model.Voter; +import org.chorem.pollen.votecounting.model.VoterBuilder; +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; + +/** + * Tests the {@link NumberStrategy}. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.5 + */ +public class NumberStrategyTest { + + public static final String CHOICE_A = "a"; + + public static final String CHOICE_B = "b"; + + public static final String CHOICE_C = "c"; + + protected static VoteCountingStrategyProvider provider; + + protected VoteCountingStrategy strategy; + + @BeforeClass + public static void beforeClass() throws Exception { + provider = new VoteCountingStrategyProvider(); + } + + @Before + public void setUp() throws Exception { + strategy = provider.getStrategy(NumberStrategy.ID); + } + + @Test + public void simpleVotecount() throws Exception { + + // Simple poll (all weight to 1) + // 1 (a=1 b=2 c=null) + // 2 (a=2 b=null c=3) + // 3 (a=7 b=12 c=20) + // Result (a=10 b=14 c=23) + + Set<Voter> voters = new VoterBuilder(). + newVoter("1", 1.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, 2.). + addVoteForChoice(CHOICE_C, null). + newVoter("2", 1.). + addVoteForChoice(CHOICE_A, 2.). + addVoteForChoice(CHOICE_B, null). + addVoteForChoice(CHOICE_C, 3.). + newVoter("3", 1.). + addVoteForChoice(CHOICE_A, 7.). + addVoteForChoice(CHOICE_B, 12.). + addVoteForChoice(CHOICE_C, 20.). + getVoters(); + + 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.)); + } + + @Test + public void simpleVotecount2() throws Exception { + + // Simple poll (all weight to 1) + // 1 (a=1 b=null c=null) + // 2 (a=1 b=1 c=null) + // 3 (a=1 b=1 c=null) + // Result (a=3 b=2 c=null) + Set<Voter> voters = new VoterBuilder(). + newVoter("1", 1.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, null). + addVoteForChoice(CHOICE_C, null). + newVoter("2", 1.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, 1.). + addVoteForChoice(CHOICE_C, null). + newVoter("3", 1.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, 1.). + addVoteForChoice(CHOICE_C, null). + getVoters(); + + 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); + } + + @Test + public void simpleVotecount3() throws Exception { + + // Simple poll (all weight to 1) + // 1 (a=1 b=1 c=null) + // 2 (a=1 b=1 c=null) + // 3 (a=1 b=1 c=null) + // Result (a=3 b=3 c=null) + Set<Voter> voters = new VoterBuilder(). + newVoter("1", 1.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, 1.). + addVoteForChoice(CHOICE_C, null). + newVoter("2", 1.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, 1.). + addVoteForChoice(CHOICE_C, null). + newVoter("3", 1.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, 1.). + addVoteForChoice(CHOICE_C, null). + getVoters(); + + 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); + } + + @Test + public void weightedVotecount1() throws Exception { + + // poll with weighted vote + // 1 (x2) (a=1 b=null c=null) + // 2 (x1) (a=null b=1 c=null) + // 3 (x1) (a=null b=1 c=1) + // Result (a=2 b=2 c=1) + Set<Voter> voters = new VoterBuilder(). + newVoter("1", 2.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, null). + addVoteForChoice(CHOICE_C, null). + newVoter("2", 1.). + addVoteForChoice(CHOICE_A, null). + addVoteForChoice(CHOICE_B, 1.). + addVoteForChoice(CHOICE_C, null). + newVoter("3", 1.). + addVoteForChoice(CHOICE_A, null). + addVoteForChoice(CHOICE_B, 1.). + addVoteForChoice(CHOICE_C, 1.). + getVoters(); + + 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.)); + } + + @Test + public void weightedVotecount2() throws Exception { + + // poll with weighted vote + // 1 (x2) (a=1 b=null c=null) + // 2 (x1) (a=null b=1 c=null) + // 3 (x3) (a=null b=1 c=1) + // Result (a=2 b=4 c=3) + Set<Voter> voters = new VoterBuilder(). + newVoter("1", 2.). + addVoteForChoice(CHOICE_A, 1.). + addVoteForChoice(CHOICE_B, null). + addVoteForChoice(CHOICE_C, null). + newVoter("2", 1.). + addVoteForChoice(CHOICE_A, null). + addVoteForChoice(CHOICE_B, 1.). + addVoteForChoice(CHOICE_C, null). + newVoter("3", 3.). + addVoteForChoice(CHOICE_A, null). + addVoteForChoice(CHOICE_B, 1.). + addVoteForChoice(CHOICE_C, 1.). + getVoters(); + + 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.)); + } + + 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()); + for (String choiceId : choiceIds) { + + ChoiceScore choiceScore = choicesById.get(choiceId); + Assert.assertNotNull(choiceScore); + assertChoiceScore(choiceScore, choiceId, choiceResult); + } + } + +} Property changes on: trunk/pollen-votecounting-strategy-number/src/test/java/org/chorem/pollen/votecounting/strategy/NumberStrategyTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: trunk/pollen-votecounting-strategy-number/src/test/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProviderTest.java (from rev 3550, trunk/pollen-votecounting-strategy-number/src/test/java/org/chorem/pollen/votecounting/VoteCountingStrategyProviderTest.java) =================================================================== --- trunk/pollen-votecounting-strategy-number/src/test/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProviderTest.java (rev 0) +++ trunk/pollen-votecounting-strategy-number/src/test/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProviderTest.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -0,0 +1,49 @@ +/* + * #%L + * Pollen :: VoteCounting strategy :: Number + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * 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% + */ +package org.chorem.pollen.votecounting.strategy; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Tests the {@link VoteCountingStrategyProvider}. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.5 + */ +public class VoteCountingStrategyProviderTest { + + protected static VoteCountingStrategyProvider provider; + + @BeforeClass + public static void beforeClass() throws Exception { + provider = new VoteCountingStrategyProvider(); + } + + @Test + public void getStrategy() throws Exception { + + Assert.assertNotNull(provider.getStrategy(NumberStrategy.ID)); + } +} Property changes on: trunk/pollen-votecounting-strategy-number/src/test/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProviderTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Deleted: trunk/pollen-votecounting-strategy-percentage/src/main/java/org/chorem/pollen/votecounting/PercentageStrategy.java =================================================================== --- trunk/pollen-votecounting-strategy-percentage/src/main/java/org/chorem/pollen/votecounting/PercentageStrategy.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-votecounting-strategy-percentage/src/main/java/org/chorem/pollen/votecounting/PercentageStrategy.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -1,143 +0,0 @@ -/* - * #%L - * Pollen :: VoteCounting strategy :: Percentage - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 CodeLutin - * %% - * 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% - */ -package org.chorem.pollen.votecounting; - -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.Map; -import java.util.Set; - -import static org.nuiton.i18n.I18n.n_; - -/** - * Percentage strategy. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.5 - */ -public class PercentageStrategy extends AbstractVoteCountingStrategy { - - public static final int ID = 1; - - @Override - public int getId() { - return ID; - } - - @Override - public String getI18nName() { - return n_("pollen.voteCountingType.percentage"); - } - - @Override - public String getI18nHelp() { - return n_("pollen.voteCountingType.percentage.help"); - } - - @Override - public VoteCountingResult votecount(Set<Voter> voters) { - // get empty result by choice - Map<String, ChoiceScore> resultByChoice = votersToResult(voters); - - for (Voter voter : voters) { - // add this voter votes to result - addVoterChoices(voter, resultByChoice); - } - // transform map of result to list of them (and sort them) - VoteCountingResult result = resultToList(null, resultByChoice); - return result; - } - - @Override - public String getDisplayVoteValue(Integer voteValue) { - return voteValue == null ? "" : String.valueOf(voteValue); - } - - @Override - public boolean isChoiceInVote(Integer voteValue) { - return voteValue != null && voteValue >= 0 && voteValue <= 100; - } - - - @Override - public boolean isVoteValueNull(Integer value) { - return value == null || value == 0; - } - - @Override - public ChoiceToVoteRenderType getRenderType() { - return ChoiceToVoteRenderType.TEXTFIELD; - } - - @Override - public boolean isDisplayResultsByChoice() { - return false; - } - - @Override - public boolean isVoteValueValid(Integer voteValue) { - // no validation on not null vote value - return true; - } - - @Override - public boolean isTotalVoteValueValid(int totalValues) { - return totalValues == 100; - } - - @Override - public String getVoteValueNotValidMessage() { - // no validation on not null vote value, so no message - return null; - } - - @Override - public String getTotalVoteValueNotValidMessage() { - return n_("pollen.error.vote.percentage"); - } - - public void addVoterChoices(Voter voter, - Map<String, ChoiceScore> resultByChoice) { - - double voterWeight = voter.getWeight(); - - for (VoteForChoice voteForChoice : voter.getVoteForChoices()) { - Double voteValue = voteForChoice.getVoteValue(); - if (voteValue != null) { - - // get score for choice - String choiceId = voteForChoice.getChoiceId(); - ChoiceScore choiceScore = resultByChoice.get(choiceId); - - // compute score to add - double scoreToAdd = voteValue * voterWeight; - - // add to score this weighted vote - choiceScore.addScoreValue(scoreToAdd); - } - } - } -} Copied: trunk/pollen-votecounting-strategy-percentage/src/main/java/org/chorem/pollen/votecounting/strategy/PercentageStrategy.java (from rev 3551, trunk/pollen-votecounting-strategy-percentage/src/main/java/org/chorem/pollen/votecounting/PercentageStrategy.java) =================================================================== --- trunk/pollen-votecounting-strategy-percentage/src/main/java/org/chorem/pollen/votecounting/strategy/PercentageStrategy.java (rev 0) +++ trunk/pollen-votecounting-strategy-percentage/src/main/java/org/chorem/pollen/votecounting/strategy/PercentageStrategy.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -0,0 +1,144 @@ +/* + * #%L + * Pollen :: VoteCounting strategy :: Percentage + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * 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% + */ +package org.chorem.pollen.votecounting.strategy; + +import org.chorem.pollen.votecounting.model.ChoiceScore; +import org.chorem.pollen.votecounting.model.ChoiceToVoteRenderType; +import org.chorem.pollen.votecounting.model.VoteCountingResult; +import org.chorem.pollen.votecounting.model.VoteForChoice; +import org.chorem.pollen.votecounting.model.Voter; + +import java.util.Map; +import java.util.Set; + +import static org.nuiton.i18n.I18n.n_; + +/** + * Percentage strategy. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.5 + */ +public class PercentageStrategy extends AbstractVoteCountingStrategy { + + public static final int ID = 1; + + @Override + public int getId() { + return ID; + } + + @Override + public String getI18nName() { + return n_("pollen.voteCountingType.percentage"); + } + + @Override + public String getI18nHelp() { + return n_("pollen.voteCountingType.percentage.help"); + } + + @Override + public VoteCountingResult votecount(Set<Voter> voters) { + // get empty result by choice + Map<String, ChoiceScore> resultByChoice = votersToResult(voters); + + for (Voter voter : voters) { + // add this voter votes to result + addVoterChoices(voter, resultByChoice); + } + // transform map of result to list of them (and sort them) + VoteCountingResult result = resultToList(resultByChoice); + return result; + } + + @Override + public String getDisplayVoteValue(Integer voteValue) { + return voteValue == null ? "" : String.valueOf(voteValue); + } + + @Override + public boolean isChoiceInVote(Integer voteValue) { + return voteValue != null && voteValue >= 0 && voteValue <= 100; + } + + + @Override + public boolean isVoteValueNull(Integer value) { + return value == null || value == 0; + } + + @Override + public ChoiceToVoteRenderType getRenderType() { + return ChoiceToVoteRenderType.TEXTFIELD; + } + + @Override + public boolean isDisplayResultsByChoice() { + return false; + } + + @Override + public boolean isVoteValueValid(Integer voteValue) { + // no validation on not null vote value + return true; + } + + @Override + public boolean isTotalVoteValueValid(int totalValues) { + return totalValues == 100; + } + + @Override + public String getVoteValueNotValidMessage() { + // no validation on not null vote value, so no message + return null; + } + + @Override + public String getTotalVoteValueNotValidMessage() { + return n_("pollen.error.vote.percentage"); + } + + public void addVoterChoices(Voter voter, + Map<String, ChoiceScore> resultByChoice) { + + double voterWeight = voter.getWeight(); + + for (VoteForChoice voteForChoice : voter.getVoteForChoices()) { + Double voteValue = voteForChoice.getVoteValue(); + if (voteValue != null) { + + // get score for choice + String choiceId = voteForChoice.getChoiceId(); + ChoiceScore choiceScore = resultByChoice.get(choiceId); + + // compute score to add + double scoreToAdd = voteValue * voterWeight; + + // add to score this weighted vote + choiceScore.addScoreValue(scoreToAdd); + } + } + } +} Property changes on: trunk/pollen-votecounting-strategy-percentage/src/main/java/org/chorem/pollen/votecounting/strategy/PercentageStrategy.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Deleted: trunk/pollen-votecounting-strategy-percentage/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.VoteCountingStrategy =================================================================== --- trunk/pollen-votecounting-strategy-percentage/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.VoteCountingStrategy 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-votecounting-strategy-percentage/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.VoteCountingStrategy 2012-06-26 03:16:11 UTC (rev 3559) @@ -1 +0,0 @@ -org.chorem.pollen.votecounting.PercentageStrategy Copied: trunk/pollen-votecounting-strategy-percentage/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.strategy.VoteCountingStrategy (from rev 3551, trunk/pollen-votecounting-strategy-percentage/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.VoteCountingStrategy) =================================================================== --- trunk/pollen-votecounting-strategy-percentage/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.strategy.VoteCountingStrategy (rev 0) +++ trunk/pollen-votecounting-strategy-percentage/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.strategy.VoteCountingStrategy 2012-06-26 03:16:11 UTC (rev 3559) @@ -0,0 +1 @@ +org.chorem.pollen.votecounting.strategy.PercentageStrategy Property changes on: trunk/pollen-votecounting-strategy-percentage/src/main/resources/META-INF/services/org.chorem.pollen.votecounting.strategy.VoteCountingStrategy ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Deleted: trunk/pollen-votecounting-strategy-percentage/src/test/java/org/chorem/pollen/votecounting/PercentageStrategyTest.java =================================================================== --- trunk/pollen-votecounting-strategy-percentage/src/test/java/org/chorem/pollen/votecounting/PercentageStrategyTest.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-votecounting-strategy-percentage/src/test/java/org/chorem/pollen/votecounting/PercentageStrategyTest.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -1,273 +0,0 @@ -/* - * #%L - * Pollen :: VoteCounting strategy :: Percentage - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 CodeLutin - * %% - * 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% - */ -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.VoteCountingResult; -import org.chorem.pollen.votecounting.model.Voter; -import org.chorem.pollen.votecounting.model.VoterBuilder; -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; - -/** - * Tests the {@link PercentageStrategy}. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.5 - */ -public class PercentageStrategyTest { - - public static final String CHOICE_A = "a"; - - public static final String CHOICE_B = "b"; - - public static final String CHOICE_C = "c"; - - protected static VoteCountingStrategyProvider provider; - - protected VoteCountingStrategy strategy; - - @BeforeClass - public static void beforeClass() throws Exception { - provider = new VoteCountingStrategyProvider(); - } - - @Before - public void setUp() throws Exception { - strategy = provider.getStrategy(PercentageStrategy.ID); - } - - @Test - public void simpleVotecount() throws Exception { - - // Simple poll (all weight to 1) - // 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) - - Set<Voter> voters = new VoterBuilder(). - newVoter("1", 1.). - addVoteForChoice(CHOICE_A, 100.). - addVoteForChoice(CHOICE_B, null). - addVoteForChoice(CHOICE_C, null). - newVoter("2", 1.). - addVoteForChoice(CHOICE_A, null). - addVoteForChoice(CHOICE_B, 100.). - addVoteForChoice(CHOICE_C, null). - newVoter("3", 1.). - addVoteForChoice(CHOICE_A, 50.). - addVoteForChoice(CHOICE_B, 50.). - addVoteForChoice(CHOICE_C, null). - getVoters(); - - 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); - } - - @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) - Set<Voter> voters = new VoterBuilder(). - newVoter("1", 1.). - addVoteForChoice(CHOICE_A, 20.). - addVoteForChoice(CHOICE_B, 30.). - addVoteForChoice(CHOICE_C, 50.). - newVoter("2", 1.). - addVoteForChoice(CHOICE_A, 50.). - addVoteForChoice(CHOICE_B, 20.). - addVoteForChoice(CHOICE_C, 30.). - newVoter("3", 1.). - addVoteForChoice(CHOICE_A, 10.). - addVoteForChoice(CHOICE_B, 50.). - addVoteForChoice(CHOICE_C, 40.). - getVoters(); - - 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.)); - } - - @Test - public void simpleVotecount3() throws Exception { - - // Simple poll (all weight to 1) - // 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) - Set<Voter> voters = new VoterBuilder(). - newVoter("1", 1.). - addVoteForChoice(CHOICE_A, 50.). - addVoteForChoice(CHOICE_B, 50.). - addVoteForChoice(CHOICE_C, null). - newVoter("2", 1.). - addVoteForChoice(CHOICE_A, 50.). - addVoteForChoice(CHOICE_B, 50.). - addVoteForChoice(CHOICE_C, null). - newVoter("3", 1.). - addVoteForChoice(CHOICE_A, 50.). - addVoteForChoice(CHOICE_B, 50.). - addVoteForChoice(CHOICE_C, null). - getVoters(); - - 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); - } - - @Test - public void weightedVotecount1() throws Exception { - - // poll with weighted vote - // 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) - Set<Voter> voters = new VoterBuilder(). - newVoter("1", 2.). - addVoteForChoice(CHOICE_A, 100.). - addVoteForChoice(CHOICE_B, null). - addVoteForChoice(CHOICE_C, null). - newVoter("2", 1.). - addVoteForChoice(CHOICE_A, null). - addVoteForChoice(CHOICE_B, 100.). - addVoteForChoice(CHOICE_C, null). - newVoter("3", 1.). - addVoteForChoice(CHOICE_A, null). - addVoteForChoice(CHOICE_B, 50.). - addVoteForChoice(CHOICE_C, 50.). - getVoters(); - - 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.)); - } - - @Test - public void weightedVotecount2() throws Exception { - - // poll with weighted vote - // 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) - Set<Voter> voters = new VoterBuilder(). - newVoter("1", 2.). - addVoteForChoice(CHOICE_A, 100.). - addVoteForChoice(CHOICE_B, null). - addVoteForChoice(CHOICE_C, null). - newVoter("2", 1.). - addVoteForChoice(CHOICE_A, null). - addVoteForChoice(CHOICE_B, 100.). - addVoteForChoice(CHOICE_C, null). - newVoter("3", 3.). - addVoteForChoice(CHOICE_A, null). - addVoteForChoice(CHOICE_B, 50.). - addVoteForChoice(CHOICE_C, 50.). - getVoters(); - - 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.)); - } - - 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()); - for (String choiceId : choiceIds) { - - ChoiceScore choiceScore = choicesById.get(choiceId); - Assert.assertNotNull(choiceScore); - assertChoiceScore(choiceScore, choiceId, choiceResult); - } - } - -} Deleted: trunk/pollen-votecounting-strategy-percentage/src/test/java/org/chorem/pollen/votecounting/VoteCountingStrategyProviderTest.java =================================================================== --- trunk/pollen-votecounting-strategy-percentage/src/test/java/org/chorem/pollen/votecounting/VoteCountingStrategyProviderTest.java 2012-06-25 22:53:17 UTC (rev 3558) +++ trunk/pollen-votecounting-strategy-percentage/src/test/java/org/chorem/pollen/votecounting/VoteCountingStrategyProviderTest.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -1,49 +0,0 @@ -/* - * #%L - * Pollen :: VoteCounting strategy :: Percentage - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 CodeLutin - * %% - * 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% - */ -package org.chorem.pollen.votecounting; - -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -/** - * Tests the {@link VoteCountingStrategyProvider}. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.5 - */ -public class VoteCountingStrategyProviderTest { - - protected static VoteCountingStrategyProvider provider; - - @BeforeClass - public static void beforeClass() throws Exception { - provider = new VoteCountingStrategyProvider(); - } - - @Test - public void getStrategy() throws Exception { - - Assert.assertNotNull(provider.getStrategy(PercentageStrategy.ID)); - } -} Copied: trunk/pollen-votecounting-strategy-percentage/src/test/java/org/chorem/pollen/votecounting/strategy/PercentageStrategyTest.java (from rev 3558, trunk/pollen-votecounting-strategy-percentage/src/test/java/org/chorem/pollen/votecounting/PercentageStrategyTest.java) =================================================================== --- trunk/pollen-votecounting-strategy-percentage/src/test/java/org/chorem/pollen/votecounting/strategy/PercentageStrategyTest.java (rev 0) +++ trunk/pollen-votecounting-strategy-percentage/src/test/java/org/chorem/pollen/votecounting/strategy/PercentageStrategyTest.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -0,0 +1,273 @@ +/* + * #%L + * Pollen :: VoteCounting strategy :: Percentage + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * 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% + */ +package org.chorem.pollen.votecounting.strategy; + +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.VoteCountingResult; +import org.chorem.pollen.votecounting.model.Voter; +import org.chorem.pollen.votecounting.model.VoterBuilder; +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; + +/** + * Tests the {@link PercentageStrategy}. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.5 + */ +public class PercentageStrategyTest { + + public static final String CHOICE_A = "a"; + + public static final String CHOICE_B = "b"; + + public static final String CHOICE_C = "c"; + + protected static VoteCountingStrategyProvider provider; + + protected VoteCountingStrategy strategy; + + @BeforeClass + public static void beforeClass() throws Exception { + provider = new VoteCountingStrategyProvider(); + } + + @Before + public void setUp() throws Exception { + strategy = provider.getStrategy(PercentageStrategy.ID); + } + + @Test + public void simpleVotecount() throws Exception { + + // Simple poll (all weight to 1) + // 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) + + Set<Voter> voters = new VoterBuilder(). + newVoter("1", 1.). + addVoteForChoice(CHOICE_A, 100.). + addVoteForChoice(CHOICE_B, null). + addVoteForChoice(CHOICE_C, null). + newVoter("2", 1.). + addVoteForChoice(CHOICE_A, null). + addVoteForChoice(CHOICE_B, 100.). + addVoteForChoice(CHOICE_C, null). + newVoter("3", 1.). + addVoteForChoice(CHOICE_A, 50.). + addVoteForChoice(CHOICE_B, 50.). + addVoteForChoice(CHOICE_C, null). + getVoters(); + + 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); + } + + @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) + Set<Voter> voters = new VoterBuilder(). + newVoter("1", 1.). + addVoteForChoice(CHOICE_A, 20.). + addVoteForChoice(CHOICE_B, 30.). + addVoteForChoice(CHOICE_C, 50.). + newVoter("2", 1.). + addVoteForChoice(CHOICE_A, 50.). + addVoteForChoice(CHOICE_B, 20.). + addVoteForChoice(CHOICE_C, 30.). + newVoter("3", 1.). + addVoteForChoice(CHOICE_A, 10.). + addVoteForChoice(CHOICE_B, 50.). + addVoteForChoice(CHOICE_C, 40.). + getVoters(); + + 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.)); + } + + @Test + public void simpleVotecount3() throws Exception { + + // Simple poll (all weight to 1) + // 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) + Set<Voter> voters = new VoterBuilder(). + newVoter("1", 1.). + addVoteForChoice(CHOICE_A, 50.). + addVoteForChoice(CHOICE_B, 50.). + addVoteForChoice(CHOICE_C, null). + newVoter("2", 1.). + addVoteForChoice(CHOICE_A, 50.). + addVoteForChoice(CHOICE_B, 50.). + addVoteForChoice(CHOICE_C, null). + newVoter("3", 1.). + addVoteForChoice(CHOICE_A, 50.). + addVoteForChoice(CHOICE_B, 50.). + addVoteForChoice(CHOICE_C, null). + getVoters(); + + 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); + } + + @Test + public void weightedVotecount1() throws Exception { + + // poll with weighted vote + // 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) + Set<Voter> voters = new VoterBuilder(). + newVoter("1", 2.). + addVoteForChoice(CHOICE_A, 100.). + addVoteForChoice(CHOICE_B, null). + addVoteForChoice(CHOICE_C, null). + newVoter("2", 1.). + addVoteForChoice(CHOICE_A, null). + addVoteForChoice(CHOICE_B, 100.). + addVoteForChoice(CHOICE_C, null). + newVoter("3", 1.). + addVoteForChoice(CHOICE_A, null). + addVoteForChoice(CHOICE_B, 50.). + addVoteForChoice(CHOICE_C, 50.). + getVoters(); + + 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.)); + } + + @Test + public void weightedVotecount2() throws Exception { + + // poll with weighted vote + // 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) + Set<Voter> voters = new VoterBuilder(). + newVoter("1", 2.). + addVoteForChoice(CHOICE_A, 100.). + addVoteForChoice(CHOICE_B, null). + addVoteForChoice(CHOICE_C, null). + newVoter("2", 1.). + addVoteForChoice(CHOICE_A, null). + addVoteForChoice(CHOICE_B, 100.). + addVoteForChoice(CHOICE_C, null). + newVoter("3", 3.). + addVoteForChoice(CHOICE_A, null). + addVoteForChoice(CHOICE_B, 50.). + addVoteForChoice(CHOICE_C, 50.). + getVoters(); + + 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.)); + } + + 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()); + for (String choiceId : choiceIds) { + + ChoiceScore choiceScore = choicesById.get(choiceId); + Assert.assertNotNull(choiceScore); + assertChoiceScore(choiceScore, choiceId, choiceResult); + } + } + +} Property changes on: trunk/pollen-votecounting-strategy-percentage/src/test/java/org/chorem/pollen/votecounting/strategy/PercentageStrategyTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native Copied: trunk/pollen-votecounting-strategy-percentage/src/test/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProviderTest.java (from rev 3551, trunk/pollen-votecounting-strategy-percentage/src/test/java/org/chorem/pollen/votecounting/VoteCountingStrategyProviderTest.java) =================================================================== --- trunk/pollen-votecounting-strategy-percentage/src/test/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProviderTest.java (rev 0) +++ trunk/pollen-votecounting-strategy-percentage/src/test/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProviderTest.java 2012-06-26 03:16:11 UTC (rev 3559) @@ -0,0 +1,49 @@ +/* + * #%L + * Pollen :: VoteCounting strategy :: Percentage + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2009 - 2012 CodeLutin + * %% + * 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% + */ +package org.chorem.pollen.votecounting.strategy; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Tests the {@link VoteCountingStrategyProvider}. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.5 + */ +public class VoteCountingStrategyProviderTest { + + protected static VoteCountingStrategyProvider provider; + + @BeforeClass + public static void beforeClass() throws Exception { + provider = new VoteCountingStrategyProvider(); + } + + @Test + public void getStrategy() throws Exception { + + Assert.assertNotNull(provider.getStrategy(PercentageStrategy.ID)); + } +} Property changes on: trunk/pollen-votecounting-strategy-percentage/src/test/java/org/chorem/pollen/votecounting/strategy/VoteCountingStrategyProviderTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native