List of usage examples for com.google.common.collect HashMultiset create
public static <E> HashMultiset<E> create(Iterable<? extends E> elements)
From source file:com.mycompany.wolf.Room.java
/** * /*from www .j a v a 2 s . c o m*/ */ private void assignRoles() { Multiset<String> roleCounts = HashMultiset.create(roleCounts()); Map<String, String> roleMap = new HashMap(); competeRoles.values().stream().filter(c -> roleCounts.remove(c.role)).forEach(c -> { roleMap.put(c.playerId, c.role); }); List<String> restPlayerId = sessions.stream().map(s -> getPlayerId(s)).filter(s -> !roleMap.containsKey(s)) .collect(Collectors.toList()); Collections.shuffle(restPlayerId); Iterator<String> restRoleIt = roleCounts.iterator(); Iterator<String> restPlayerIdIt = restPlayerId.iterator(); for (; restRoleIt.hasNext();) { String role = restRoleIt.next(); String playerId = restPlayerIdIt.next(); roleMap.put(playerId, role); } sessions.stream().forEach(s -> { s.getUserProperties().put("role", roleMap.get(getPlayerId(s))); }); List<ImmutableMap<String, String>> assignedRoles = roleMap.entrySet().stream() .map(entry -> ImmutableMap.of("playerId", entry.getKey(), "role", entry.getValue())) .collect(Collectors.toCollection(LinkedList::new)); Map<String, Object> assignRoles = ImmutableMap.of("code", "assignRoles", "properties", assignedRoles); String jsonText = JsonUtils.toString(assignRoles); sessions.stream().forEach(s -> { s.getAsyncRemote().sendText(jsonText); }); }
From source file:org.apache.aurora.scheduler.async.preemptor.PendingTaskProcessor.java
/** * Creates execution sequence for pending task groups by interleaving their unique occurrences. * For example: {G1, G1, G1, G2, G2} will be converted into {G1, G2, G1, G2, G1}. * * @param groups Multiset of task groups. * @return A task group execution sequence. */// w w w . jav a2 s . c om private static List<TaskGroupKey> getPreemptionSequence(Multiset<TaskGroupKey> groups) { Multiset<TaskGroupKey> mutableGroups = HashMultiset.create(groups); List<TaskGroupKey> instructions = Lists.newLinkedList(); Set<TaskGroupKey> keys = ImmutableSet.copyOf(groups.elementSet()); while (!mutableGroups.isEmpty()) { for (TaskGroupKey key : keys) { if (mutableGroups.contains(key)) { instructions.add(key); mutableGroups.remove(key); } } } return instructions; }
From source file:org.apache.aurora.scheduler.async.preemptor.PendingTaskProcessor.java
private List<TaskGroupKey> fetchIdlePendingGroups(StoreProvider store) { Multiset<TaskGroupKey> taskGroupCounts = HashMultiset .create(FluentIterable.from(store.getTaskStore().fetchTasks(Query.statusScoped(PENDING))) .filter(Predicates.and(isIdleTask, Predicates.not(hasCachedSlot))) .transform(Functions.compose(ASSIGNED_TO_GROUP_KEY, SCHEDULED_TO_ASSIGNED))); return getPreemptionSequence(taskGroupCounts); }
From source file:org.apache.aurora.scheduler.preemptor.PendingTaskProcessor.java
private List<TaskGroupKey> fetchIdlePendingGroups(StoreProvider store) { Multiset<TaskGroupKey> taskGroupCounts = HashMultiset .create(FluentIterable.from(store.getTaskStore().fetchTasks(Query.statusScoped(PENDING))) .filter(Predicates.and(isIdleTask, Predicates.not(hasCachedSlot))) .transform(Functions.compose(ASSIGNED_TO_GROUP_KEY, IScheduledTask::getAssignedTask))); return getPreemptionSequence(taskGroupCounts, reservationBatchSize); }
From source file:org.caleydo.vis.lineup.model.MultiCategoricalRankColumnModel.java
/** * @return// w w w . ja v a 2 s . c o m */ public Multiset<CATEGORY_TYPE> getHist() { Multiset<CATEGORY_TYPE> hist = HashMultiset.create(categories.size()); for (IRow r : getMyRanker()) { Set<CATEGORY_TYPE> vs = getCatValue(r); if (vs == null) // TODO nan continue; hist.addAll(vs); } return hist; }
From source file:org.apache.aurora.scheduler.preemptor.PendingTaskProcessor.java
/** * Creates execution sequence for pending task groups by interleaving batches of requested size of * their occurrences. For example: {G1, G1, G1, G2, G2} with batch size of 2 task per group will * be converted into {G1, G1, G2, G2, G1}. * * @param groups Multiset of task groups. * @param batchSize The batch size of tasks from each group to sequence together. * @return A task group execution sequence. *//* ww w .ja v a2 s . c om*/ @VisibleForTesting static List<TaskGroupKey> getPreemptionSequence(Multiset<TaskGroupKey> groups, int batchSize) { Preconditions.checkArgument(batchSize > 0, "batchSize should be positive."); Multiset<TaskGroupKey> mutableGroups = HashMultiset.create(groups); List<TaskGroupKey> instructions = Lists.newLinkedList(); Set<TaskGroupKey> keys = ImmutableSet.copyOf(groups.elementSet()); while (!mutableGroups.isEmpty()) { for (TaskGroupKey key : keys) { if (mutableGroups.contains(key)) { int elementCount = mutableGroups.remove(key, batchSize); int removedCount = Math.min(elementCount, batchSize); instructions.addAll(Collections.nCopies(removedCount, key)); } } } return instructions; }
From source file:org.immutables.value.processor.meta.ValueTypeComposer.java
private void checkAttributeNamesForDuplicates(ValueType type, Protoclass protoclass) { if (!type.attributes.isEmpty()) { Multiset<String> attributeNames = HashMultiset.create(type.attributes.size()); for (ValueAttribute attribute : type.attributes) { attributeNames.add(attribute.name()); }/*from w ww . ja v a2 s . co m*/ List<String> duplicates = Lists.newArrayList(); for (Multiset.Entry<String> entry : attributeNames.entrySet()) { if (entry.getCount() > 1) { duplicates.add(entry.getElement()); } } if (!duplicates.isEmpty()) { protoclass.report().error( "Duplicate attribute names %s. You should check if correct @Value.Style applied", duplicates); } } }
From source file:com.googlecode.blaisemath.graph.GraphUtils.java
/** * Computes and returns degree distribution. * @param <V> graph node type/*from w w w. j a v a2 s. c o m*/ * @param graph the graph * @return map associating degree #s with counts, sorted by degree */ public static <V> Multiset<Integer> degreeDistribution(Graph<V> graph) { return HashMultiset.create(Iterables.transform(graph.nodes(), degreeFunction(graph))); }
From source file:com.google.protoeditor.psi.ProtoTestCase.java
public void assertAnyOrder(Iterable<String> actual, Iterable<String> expected) { HashMultiset<String> actualSet = HashMultiset.create(actual); HashMultiset<String> expectedSet = HashMultiset.create(expected); String failureMessage = "Expected List: " + expectedSet + " does not match actual list: " + actualSet; assertEquals(failureMessage, expectedSet, actualSet); }
From source file:org.eclipse.gef4.mvc.parts.AbstractVisualPart.java
@Override public void detachAnchored(IVisualPart<VR, ? extends VR> anchored) { // copy anchoreds (required for the change notification) Multiset<IVisualPart<VR, ? extends VR>> oldAnchoreds = anchoreds == null ? HashMultiset.<IVisualPart<VR, ? extends VR>>create() : HashMultiset.create(anchoreds); // determine viewer before and after removing the anchored IViewer<VR> oldViewer = getViewer(); anchoreds.remove(anchored);//www. ja v a2 s. c om IViewer<VR> newViewer = getViewer(); anchoreds.add(anchored); // unregister if we lose the link to the viewer if (oldViewer != null && newViewer == null) { unregister(oldViewer); } anchoreds.remove(anchored); if (anchoreds.size() == 0) { anchoreds = null; } pcs.firePropertyChange(ANCHOREDS_PROPERTY, oldAnchoreds, getAnchoreds()); }