OrderedPermutationCollection(Iterable<E> input, Comparator<? super E> comparator) { this.inputList = ImmutableList.sortedCopyOf(comparator, input); this.comparator = comparator; this.size = calculateSize(inputList, comparator); }
/** * Returns an <b>immutable</b> list containing {@code elements} sorted by this ordering. The input * is not modified. * * <p>Unlike {@link Sets#newTreeSet(Iterable)}, this method does not discard elements that are * duplicates according to the comparator. The sort performed is <i>stable</i>, meaning that such * elements will appear in the returned list in the same order they appeared in {@code elements}. * * <p><b>Performance note:</b> According to our * benchmarking * on Open JDK 7, this method is the most efficient way to make a sorted copy of a collection. * * @throws NullPointerException if any element of {@code elements} is {@code null} * @since 3.0 */ // TODO(kevinb): rerun benchmarks including new options @CanIgnoreReturnValue // TODO(kak): Consider removing this before internal migration public <E extends T> ImmutableList<E> immutableSortedCopy(Iterable<E> elements) { return ImmutableList.sortedCopyOf(this, elements); }
OrderedPermutationCollection(Iterable<E> input, Comparator<? super E> comparator) { this.inputList = ImmutableList.sortedCopyOf(comparator, input); this.comparator = comparator; this.size = calculateSize(inputList, comparator); }
private static <R, C, V> RegularImmutableTable<R, C, V> forCellsInternal( Iterable<Cell<R, C, V>> cells, @Nullable Comparator<? super R> rowComparator, @Nullable Comparator<? super C> columnComparator) { Set<R> rowSpaceBuilder = new LinkedHashSet<>(); Set<C> columnSpaceBuilder = new LinkedHashSet<>(); ImmutableList<Cell<R, C, V>> cellList = ImmutableList.copyOf(cells); for (Cell<R, C, V> cell : cells) { rowSpaceBuilder.add(cell.getRowKey()); columnSpaceBuilder.add(cell.getColumnKey()); } ImmutableSet<R> rowSpace = (rowComparator == null) ? ImmutableSet.copyOf(rowSpaceBuilder) : ImmutableSet.copyOf(ImmutableList.sortedCopyOf(rowComparator, rowSpaceBuilder)); ImmutableSet<C> columnSpace = (columnComparator == null) ? ImmutableSet.copyOf(columnSpaceBuilder) : ImmutableSet.copyOf(ImmutableList.sortedCopyOf(columnComparator, columnSpaceBuilder)); return forOrderedComponents(cellList, rowSpace, columnSpace); }
/** Creates an ImmutableListMultimap from an asMap.entrySet. */ static <K, V> ImmutableListMultimap<K, V> fromMapEntries( Collection<? extends Map.Entry<? extends K, ? extends Collection<? extends V>>> mapEntries, @Nullable Comparator<? super V> valueComparator) { if (mapEntries.isEmpty()) { return of(); } ImmutableMap.Builder<K, ImmutableList<V>> builder = new ImmutableMap.Builder<>(mapEntries.size()); int size = 0; for (Entry<? extends K, ? extends Collection<? extends V>> entry : mapEntries) { K key = entry.getKey(); Collection<? extends V> values = entry.getValue(); ImmutableList<V> list = (valueComparator == null) ? ImmutableList.copyOf(values) : ImmutableList.sortedCopyOf(valueComparator, values); if (!list.isEmpty()) { builder.put(key, list); size += list.size(); } } return new ImmutableListMultimap<>(builder.build(), size); }
OrderedPermutationCollection(Iterable<E> input, Comparator<? super E> comparator) { this.inputList = ImmutableList.sortedCopyOf(comparator, input); this.comparator = comparator; this.size = calculateSize(inputList, comparator); }
/** * Returns an <b>immutable</b> list containing {@code elements} sorted by this ordering. The input * is not modified. * * <p>Unlike {@link Sets#newTreeSet(Iterable)}, this method does not discard elements that are * duplicates according to the comparator. The sort performed is <i>stable</i>, meaning that such * elements will appear in the returned list in the same order they appeared in {@code elements}. * * <p><b>Performance note:</b> According to our * benchmarking * on Open JDK 7, this method is the most efficient way to make a sorted copy of a collection. * * @throws NullPointerException if any element of {@code elements} is {@code null} * @since 3.0 */ // TODO(kevinb): rerun benchmarks including new options @CanIgnoreReturnValue // TODO(kak): Consider removing this before internal migration public <E extends T> ImmutableList<E> immutableSortedCopy(Iterable<E> elements) { return ImmutableList.sortedCopyOf(this, elements); }
private static ImmutableList<MethodTree> sortedByArity(Iterable<MethodTree> methodTrees) { return sortedCopyOf(comparingArity().thenComparing(comparingPositions()), methodTrees); }
/** * Adds the specified {@link Endpoint} to current {@link Endpoint} list. */ protected final void addEndpoint(Endpoint e) { final List<Endpoint> newEndpoints; endpointsLock.lock(); try { List<Endpoint> newEndpointsUnsorted = Lists.newArrayList(endpoints); newEndpointsUnsorted.add(e); endpoints = newEndpoints = ImmutableList.sortedCopyOf(newEndpointsUnsorted); } finally { endpointsLock.unlock(); } notifyListeners(newEndpoints); completeInitialEndpointsFuture(newEndpoints); }
private static <R, C, V> RegularImmutableTable<R, C, V> forCellsInternal( Iterable<Cell<R, C, V>> cells, @NullableDecl Comparator<? super R> rowComparator, @NullableDecl Comparator<? super C> columnComparator) { Set<R> rowSpaceBuilder = new LinkedHashSet<>(); Set<C> columnSpaceBuilder = new LinkedHashSet<>(); ImmutableList<Cell<R, C, V>> cellList = ImmutableList.copyOf(cells); for (Cell<R, C, V> cell : cells) { rowSpaceBuilder.add(cell.getRowKey()); columnSpaceBuilder.add(cell.getColumnKey()); } ImmutableSet<R> rowSpace = (rowComparator == null) ? ImmutableSet.copyOf(rowSpaceBuilder) : ImmutableSet.copyOf(ImmutableList.sortedCopyOf(rowComparator, rowSpaceBuilder)); ImmutableSet<C> columnSpace = (columnComparator == null) ? ImmutableSet.copyOf(columnSpaceBuilder) : ImmutableSet.copyOf(ImmutableList.sortedCopyOf(columnComparator, columnSpaceBuilder)); return forOrderedComponents(cellList, rowSpace, columnSpace); }
/** Creates an ImmutableListMultimap from an asMap.entrySet. */ static <K, V> ImmutableListMultimap<K, V> fromMapEntries( Collection<? extends Map.Entry<? extends K, ? extends Collection<? extends V>>> mapEntries, @NullableDecl Comparator<? super V> valueComparator) { if (mapEntries.isEmpty()) { return of(); } ImmutableMap.Builder<K, ImmutableList<V>> builder = new ImmutableMap.Builder<>(mapEntries.size()); int size = 0; for (Entry<? extends K, ? extends Collection<? extends V>> entry : mapEntries) { K key = entry.getKey(); Collection<? extends V> values = entry.getValue(); ImmutableList<V> list = (valueComparator == null) ? ImmutableList.copyOf(values) : ImmutableList.sortedCopyOf(valueComparator, values); if (!list.isEmpty()) { builder.put(key, list); size += list.size(); } } return new ImmutableListMultimap<>(builder.build(), size); }
public void testSortedCopyOf() { Collection<String> c = MinimalCollection.of("a", "b", "A", "c"); List<String> list = ImmutableList.sortedCopyOf(String.CASE_INSENSITIVE_ORDER, c); assertEquals(asList("a", "A", "b", "c"), list); }
public void testSortedCopyOf_empty() { Collection<String> c = MinimalCollection.of(); List<String> list = ImmutableList.sortedCopyOf(String.CASE_INSENSITIVE_ORDER, c); assertEquals(asList(), list); }
public void testSortedCopyOf_natural() { Collection<Integer> c = MinimalCollection.of(4, 16, 10, -1, 5); ImmutableList<Integer> list = ImmutableList.sortedCopyOf(c); assertEquals(asList(-1, 4, 5, 10, 16), list); }
public void testSortedCopyOf_natural_singleton() { Collection<Integer> c = MinimalCollection.of(100); ImmutableList<Integer> list = ImmutableList.sortedCopyOf(c); assertEquals(asList(100), list); }
public void testSortedCopyOf_singleton() { Collection<String> c = MinimalCollection.of("a"); List<String> list = ImmutableList.sortedCopyOf(String.CASE_INSENSITIVE_ORDER, c); assertEquals(asList("a"), list); }
public void testSortedCopyOf_natural_empty() { Collection<Integer> c = MinimalCollection.of(); ImmutableList<Integer> list = ImmutableList.sortedCopyOf(c); assertEquals(asList(), list); }
public void testSortedCopyOf_containsNull() { Collection<String> c = MinimalCollection.of("a", "b", "A", null, "c"); try { ImmutableList.sortedCopyOf(String.CASE_INSENSITIVE_ORDER, c); fail("Expected NPE"); } catch (NullPointerException expected) { } }
List<Rectangle> sortedItems = ImmutableList.sortedCopyOf(splitDimension.getComparator(), items);
public void testSortedCopyOf_natural_containsNull() { Collection<Integer> c = MinimalCollection.of(1, 3, null, 2); try { ImmutableList.sortedCopyOf(c); fail("Expected NPE"); } catch (NullPointerException expected) { } }