private static <E extends Comparable<? super E>> SortedSet<E> nullCheckedTreeSet(E[] elements) { SortedSet<E> set = newTreeSet(); for (E element : elements) { // Explicit null check because TreeSet wrongly accepts add(null) when empty. set.add(checkNotNull(element)); } return set; }
/** * Find the requested number of available ports for this {@code SocketType}, * each randomly selected from the range [{@code minPort}, {@code maxPort}]. * @param numRequested the number of available ports to find * @param minPort the minimum port number * @param maxPort the maximum port number * @return a sorted set of available port numbers for this socket type * @throws IllegalStateException if the requested number of available ports could not be found */ SortedSet<Integer> findAvailablePorts(int numRequested, int minPort, int maxPort) { Assert.isTrue(minPort > 0, "'minPort' must be greater than 0"); Assert.isTrue(maxPort > minPort, "'maxPort' must be greater than 'minPort'"); Assert.isTrue(maxPort <= PORT_RANGE_MAX, "'maxPort' must be less than or equal to " + PORT_RANGE_MAX); Assert.isTrue(numRequested > 0, "'numRequested' must be greater than 0"); Assert.isTrue((maxPort - minPort) >= numRequested, "'numRequested' must not be greater than 'maxPort' - 'minPort'"); SortedSet<Integer> availablePorts = new TreeSet<>(); int attemptCount = 0; while ((++attemptCount <= numRequested + 100) && availablePorts.size() < numRequested) { availablePorts.add(findAvailablePort(minPort, maxPort)); } if (availablePorts.size() != numRequested) { throw new IllegalStateException(String.format( "Could not find %d available %s ports in the range [%d, %d]", numRequested, name(), minPort, maxPort)); } return availablePorts; } }
@Override protected SortedSet<Integer> create(Integer[] elements) { SortedSet<Integer> set = nullCheckedTreeSet(elements); if (set.isEmpty()) { /* * The (tooLow + 1, tooHigh) arguments below would be invalid because tooLow would be * greater than tooHigh. */ return ContiguousSet.create(Range.openClosed(0, 1), DiscreteDomain.integers()).subSet(0, 1); } int tooHigh = set.last() + 1; int tooLow = set.first() - 1; set.add(tooHigh); set.add(tooLow); return checkedCreate(set).subSet(tooLow + 1, tooHigh); } }
void ensureNotDirectlyModifiable(SortedSet<Integer> unmod) { try { unmod.add(4); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { } try { unmod.remove(4); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { } try { unmod.addAll(Collections.singleton(4)); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { } try { Iterator<Integer> iterator = unmod.iterator(); iterator.next(); iterator.remove(); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { } }
public void testSortedKeySet() { TreeMultimap<String, Integer> multimap = createPopulate(); SortedSet<String> keySet = multimap.keySet(); assertEquals(null, keySet.first()); assertEquals("google", keySet.last()); assertEquals(StringLength.COMPARATOR, keySet.comparator()); assertEquals(Sets.newHashSet(null, "tree"), keySet.headSet("yahoo")); assertEquals(Sets.newHashSet("google"), keySet.tailSet("yahoo")); assertEquals(Sets.newHashSet("tree"), keySet.subSet("ask", "yahoo")); }
@GwtIncompatible // SerializableTester public void testSeveral_serialization() { SortedSet<String> set = new SafeTreeSet<>(); set.add("a"); set.add("b"); set.add("c"); SortedSet<String> copy = SerializableTester.reserializeAndAssert(set); assertEquals(set.comparator(), copy.comparator()); } }
/** * Adds the given servers to the group. */ public void addAllServers(Collection<Address> hostPort){ servers.addAll(hostPort); }
@Override public KinesisMessageId getNextFailedMessageToRetry() { KinesisMessageId result = null; // return the first message to be retried from the set. It will return the message with the earliest retry time <= current time if (!retryMessageSet.isEmpty() ) { result = retryMessageSet.first(); if (!(retryTimes.get(result) <= System.nanoTime())) { result = null; } } LOG.debug("Returning {} to spout for retrying.", result); return result; }
public synchronized int size() { return permissions.size(); }
public void testAsMapSortedReadsThrough() { SortedSet<String> strings = new NonNavigableSortedSet(); Collections.addAll(strings, "one", "two", "three"); SortedMap<String, Integer> map = Maps.asMap(strings, LENGTH_FUNCTION); assertNull(map.comparator()); assertEquals(ImmutableSortedMap.of("one", 3, "two", 3, "three", 5), map); assertNull(map.get("four")); strings.add("four"); assertEquals(ImmutableSortedMap.of("one", 3, "two", 3, "three", 5, "four", 4), map); assertEquals(Integer.valueOf(4), map.get("four")); SortedMap<String, Integer> headMap = map.headMap("two"); assertEquals(ImmutableSortedMap.of("four", 4, "one", 3, "three", 5), headMap); strings.add("five"); strings.remove("one"); assertEquals(ImmutableSortedMap.of("five", 4, "four", 4, "three", 5), headMap); assertThat(map.entrySet()) .containsExactly( mapEntry("five", 4), mapEntry("four", 4), mapEntry("three", 5), mapEntry("two", 3)) .inOrder(); }
@Override public K firstKey() { // correctly throws NoSuchElementException when filtered map is empty. return keySet().iterator().next(); }
@Override public E first() { synchronized (mutex) { return delegate().first(); } }
@SuppressWarnings("unchecked") // list cannot be properly parameterized as it breaks other tests @Test public void setCollectionPropertyWithIntegerValue() { IndexedTestBean target = new IndexedTestBean(); AbstractPropertyAccessor accessor = createAccessor(target); Collection<Integer> coll = new HashSet<>(); coll.add(0); accessor.setPropertyValue("collection", new Integer(0)); List<Integer> set = new LinkedList<>(); set.add(1); accessor.setPropertyValue("set", new Integer(1)); List<Integer> sortedSet = new ArrayList<>(); sortedSet.add(2); accessor.setPropertyValue("sortedSet", new Integer(2)); Set<Integer> list = new HashSet<>(); list.add(3); accessor.setPropertyValue("list", new Integer(3)); assertEquals(1, target.getCollection().size()); assertTrue(target.getCollection().containsAll(coll)); assertEquals(1, target.getSet().size()); assertTrue(target.getSet().containsAll(set)); assertEquals(1, target.getSortedSet().size()); assertTrue(target.getSortedSet().containsAll(sortedSet)); assertEquals(1, target.getList().size()); assertTrue(target.getList().containsAll(list)); }
@Override public E last() { synchronized (mutex) { return delegate().last(); } }
public void testNullAcceptingComparator() throws Exception { Comparator<String> comparator = Ordering.<String>natural().nullsFirst(); TreeMultiset<String> ms = TreeMultiset.create(comparator); ms.add("b"); ms.add(null); ms.add("a"); ms.add("b"); ms.add(null, 2); assertThat(ms).containsExactly(null, null, null, "a", "b", "b").inOrder(); assertEquals(3, ms.count(null)); SortedSet<String> elementSet = ms.elementSet(); assertEquals(null, elementSet.first()); assertEquals("b", elementSet.last()); assertEquals(comparator, elementSet.comparator()); }
@Test public void setCollectionPropertyWithStringValueAndCustomEditor() { IndexedTestBean target = new IndexedTestBean(); AbstractPropertyAccessor accessor = createAccessor(target); accessor.registerCustomEditor(String.class, "set", new StringTrimmerEditor(false)); accessor.registerCustomEditor(String.class, "list", new StringTrimmerEditor(false)); accessor.setPropertyValue("set", "set1 "); accessor.setPropertyValue("sortedSet", "sortedSet1"); accessor.setPropertyValue("list", "list1 "); assertEquals(1, target.getSet().size()); assertTrue(target.getSet().contains("set1")); assertEquals(1, target.getSortedSet().size()); assertTrue(target.getSortedSet().contains("sortedSet1")); assertEquals(1, target.getList().size()); assertTrue(target.getList().contains("list1")); accessor.setPropertyValue("list", Collections.singletonList("list1 ")); assertTrue(target.getList().contains("list1")); }