/** * Adds one vote to a candidate in the elect */ public void vote(ServerResource agent, CollectionSettingName name, Serializable value) { vote(agent, new CollectionSettingGroup(name, value, agent)); }
/** * Tallies the results of the election, sub-divides the CollectionSettings into * those that are global and those that are specific to agents. */ public void tally() { int votesRequiredForGlobal=uniqueAgents.size(); ServerResource[] agents=(votesRequiredForGlobal <= 0) ? EMPTY_SERVERS : ArrayUtil.toArray(uniqueAgents, ServerResource.class); for (CollectionSettingCandidate forced : forcedGlobal) { elect.put(forced, new CollectionSettingGroup(forced.getName(), forced.getValue(), agents)); } List<CollectionSettingCandidate> all=new LinkedList<CollectionSettingCandidate>(elect.keySet()); List<CollectionSettingGroup> global=new LinkedList<CollectionSettingGroup>(); List<CollectionSettingGroup> local=new LinkedList<CollectionSettingGroup>(); for (CollectionSettingCandidate candidate : all) { CollectionSettingGroup votes = elect.get(candidate); if (votes.getVotes() == votesRequiredForGlobal) { global.add(votes); } else { local.add(votes); } } globalSettings = global; localSettings = local; }
/** * Merge this set of votes and the given set into a new set of votes. */ public CollectionSettingGroup merge(CollectionSettingGroup newVotes) { if ((!ObjectUtil.typedEquals(getName(), newVotes.getName())) || (!ObjectUtil.typedEquals(getValue(), newVotes.getValue()))) { throw new IllegalStateException("Setting name and value do not match"); } Set<ServerResource> newAgents = new HashSet<ServerResource>(); newAgents.addAll(newVotes.getInternalAgents()); newAgents.addAll(getInternalAgents()); return new CollectionSettingGroup(newVotes.getName(), newVotes.getValue(), newAgents); }