@SuppressWarnings("unchecked") // generic arrays and unchecked cast void testMinAndMax() { List<T> shuffledList = Lists.newArrayList(strictlyOrderedList); shuffledList = shuffledCopy(shuffledList, new Random(5)); T min = strictlyOrderedList.get(0); T max = strictlyOrderedList.get(strictlyOrderedList.size() - 1); T first = shuffledList.get(0); T second = shuffledList.get(1); T third = shuffledList.get(2); T[] rest = shuffledList.subList(3, shuffledList.size()).toArray(emptyArray); assertEquals(min, ordering.min(shuffledList)); assertEquals(min, ordering.min(shuffledList.iterator())); assertEquals(min, ordering.min(first, second, third, rest)); assertEquals(min, ordering.min(min, max)); assertEquals(min, ordering.min(max, min)); assertEquals(max, ordering.max(shuffledList)); assertEquals(max, ordering.max(shuffledList.iterator())); assertEquals(max, ordering.max(first, second, third, rest)); assertEquals(max, ordering.max(min, max)); assertEquals(max, ordering.max(max, min)); }
public void testParameterMinAndMax() { assertEquals(5, (int) numberOrdering.max(3, 5)); assertEquals(5, (int) numberOrdering.max(5, 3)); assertEquals(3, (int) numberOrdering.min(3, 5)); assertEquals(3, (int) numberOrdering.min(5, 3)); // when the values are the same, the first argument should be returned Integer a = new Integer(4); Integer b = new Integer(4); assertSame(a, numberOrdering.max(a, b)); assertSame(a, numberOrdering.min(a, b)); }
public void testIterableMinAndMax() { List<Integer> ints = Lists.newArrayList(5, 3, 0, 9); assertEquals(9, (int) numberOrdering.max(ints)); assertEquals(0, (int) numberOrdering.min(ints)); // when the values are the same, the first argument should be returned Integer a = new Integer(4); Integer b = new Integer(4); ints = Lists.newArrayList(a, b, b); assertSame(a, numberOrdering.max(ints)); assertSame(a, numberOrdering.min(ints)); }
public void testVarargsMinAndMax() { // try the min and max values in all positions, since some values are proper // parameters and others are from the varargs array assertEquals(9, (int) numberOrdering.max(9, 3, 0, 5, 8)); assertEquals(9, (int) numberOrdering.max(5, 9, 0, 3, 8)); assertEquals(9, (int) numberOrdering.max(5, 3, 9, 0, 8)); assertEquals(9, (int) numberOrdering.max(5, 3, 0, 9, 8)); assertEquals(9, (int) numberOrdering.max(5, 3, 0, 8, 9)); assertEquals(0, (int) numberOrdering.min(0, 3, 5, 9, 8)); assertEquals(0, (int) numberOrdering.min(5, 0, 3, 9, 8)); assertEquals(0, (int) numberOrdering.min(5, 3, 0, 9, 8)); assertEquals(0, (int) numberOrdering.min(5, 3, 9, 0, 8)); assertEquals(0, (int) numberOrdering.min(5, 3, 0, 9, 0)); // when the values are the same, the first argument should be returned Integer a = new Integer(4); Integer b = new Integer(4); assertSame(a, numberOrdering.max(a, b, b)); assertSame(a, numberOrdering.min(a, b, b)); }
private List<List<TPlayer>> getNonFixedSumPlayerGroups( final List<TPlayer> players, int numRoles, final Multiset<Set<TPlayer>> matchupsSoFar) { // Two-player: there's an elegant round-robin algorithm we // could use here, "rotate" all but one player in two rows // Do something naive for now // final Multiset<Set<TPlayer>> matchupsSoFar = // nonFixedSumMatchupsSoFarByNumPlayers.get(numRoles); Preconditions.checkNotNull(matchupsSoFar); // Map<Player, Multiset<Integer>> roleAssignmentsSoFar = // nonFixedSumRoleAssignmentsSoFarByNumPlayers.get(numRoles); List<TPlayer> playersToAssign = Lists.newArrayList(players); List<List<TPlayer>> results = Lists.newArrayList(); while (playersToAssign.size() >= numRoles) { TPlayer player = playersToAssign.get(0); // Grab the first available players with the fewest previous matchups against us and each // other final List<TPlayer> playersInGroup = Lists.newArrayList(player); playersToAssign.remove(player); while (playersInGroup.size() < numRoles) { Ordering<TPlayer> playerOrder = Ordering.from( new Comparator<TPlayer>() { @Override public int compare(TPlayer p1, TPlayer p2) { int sum1 = getSum(p1); int sum2 = getSum(p2); return Integer.compare(sum1, sum2); } // Sum of matchups against players already in group private int getSum(TPlayer p) { int sum = 0; for (TPlayer playerInGroup : playersInGroup) { sum += matchupsSoFar.count(ImmutableSet.of(p, playerInGroup)); } return sum; } }) .compound( new Comparator<TPlayer>() { @Override public int compare(TPlayer o1, TPlayer o2) { int seed1 = players.indexOf(o1); int seed2 = players.indexOf(o2); return Integer.compare(seed1, seed2); } }); TPlayer playerToAdd = playerOrder.min(playersToAssign); playersInGroup.add(playerToAdd); playersToAssign.remove(playerToAdd); } // TODO: Shuffle the roles intelligently, somehow // Should role shuffling be per-game? Across the tournament? results.add(playersInGroup); } for (List<TPlayer> playerGroup : results) { // TODO: Fix this for (int p1 = 0; p1 < playerGroup.size(); p1++) { for (int p2 = p1 + 1; p2 < playerGroup.size(); p2++) { matchupsSoFar.add(ImmutableSet.of(playerGroup.get(p1), playerGroup.get(p2))); } } } return results; }
private DateTime extractFirstBroadcastDate(List<Broadcast> broadcasts) { if (broadcasts.isEmpty()) { return null; } return TRANSMISSION_ORDERING.min(broadcasts).getTransmissionTime(); }
private String extractNetwork(List<Broadcast> broadcasts) { return channelResolver .fromUri(TRANSMISSION_ORDERING.min(broadcasts).getBroadcastOn()) .requireValue() .getTitle(); }
public void testIteratorMinExhaustsIterator() { List<Integer> ints = Lists.newArrayList(9, 0, 3, 5); Iterator<Integer> iterator = ints.iterator(); assertEquals(0, (int) numberOrdering.min(iterator)); assertFalse(iterator.hasNext()); }