RegularImmutableSortedMultiset(Comparator<? super E> comparator) { this.elementSet = ImmutableSortedSet.emptySet(comparator); this.cumulativeCounts = ZERO_CUMULATIVE_COUNTS; this.offset = 0; this.length = 0; }
RegularImmutableSortedMultiset(Comparator<? super E> comparator) { this.elementSet = ImmutableSortedSet.emptySet(comparator); this.cumulativeCounts = ZERO_CUMULATIVE_COUNTS; this.offset = 0; this.length = 0; }
RegularImmutableSortedMultiset(Comparator<? super E> comparator) { this.elementSet = ImmutableSortedSet.emptySet(comparator); this.cumulativeCounts = ZERO_CUMULATIVE_COUNTS; this.offset = 0; this.length = 0; }
private static <V> ImmutableSet<V> emptySet(@Nullable Comparator<? super V> valueComparator) { return (valueComparator == null) ? ImmutableSet.<V>of() : ImmutableSortedSet.<V>emptySet(valueComparator); }
static <K, V> ImmutableSortedMap<K, V> emptyMap(Comparator<? super K> comparator) { if (Ordering.natural().equals(comparator)) { return of(); } else { return new ImmutableSortedMap<>( ImmutableSortedSet.emptySet(comparator), ImmutableList.<V>of()); } }
@GwtIncompatible // NavigableSet ImmutableSortedSet<C> createDescendingSet() { return ImmutableSortedSet.emptySet(Ordering.natural().reverse()); } }
private static <V> ImmutableSet<V> emptySet(@NullableDecl Comparator<? super V> valueComparator) { return (valueComparator == null) ? ImmutableSet.<V>of() : ImmutableSortedSet.<V>emptySet(valueComparator); }
/** * Returns an immutable sorted set containing the elements of a sorted set, sorted by the same * {@code Comparator}. That behavior differs from {@link #copyOf(Iterable)}, which always uses the * natural ordering of the elements. * * <p>Despite the method name, this method attempts to avoid actually copying the data when it is * safe to do so. The exact circumstances under which a copy will or will not be performed are * undocumented and subject to change. * * <p>This method is safe to use even when {@code sortedSet} is a synchronized or concurrent * collection that is currently being modified by another thread. * * @throws NullPointerException if {@code sortedSet} or any of its elements is null */ public static <E> ImmutableSortedSet<E> copyOfSorted(SortedSet<E> sortedSet) { Comparator<? super E> comparator = SortedIterables.comparator(sortedSet); ImmutableList<E> list = ImmutableList.copyOf(sortedSet); if (list.isEmpty()) { return emptySet(comparator); } else { return new RegularImmutableSortedSet<E>(list, comparator); } }
@GwtIncompatible // NavigableSet ImmutableSortedSet<C> createDescendingSet() { return ImmutableSortedSet.emptySet(Ordering.natural().reverse()); } }
/** * Constructs an {@code ImmutableSortedSet} from the first {@code n} elements of {@code contents}. * If {@code k} is the size of the returned {@code ImmutableSortedSet}, then the sorted unique * elements are in the first {@code k} positions of {@code contents}, and {@code contents[i] == * null} for {@code k <= i < n}. * * <p>If {@code k == contents.length}, then {@code contents} may no longer be safe for * modification. * * @throws NullPointerException if any of the first {@code n} elements of {@code contents} is null */ static <E> ImmutableSortedSet<E> construct( Comparator<? super E> comparator, int n, E... contents) { if (n == 0) { return emptySet(comparator); } checkElementsNotNull(contents, n); Arrays.sort(contents, 0, n, comparator); int uniques = 1; for (int i = 1; i < n; i++) { E cur = contents[i]; E prev = contents[uniques - 1]; if (comparator.compare(cur, prev) != 0) { contents[uniques++] = cur; } } Arrays.fill(contents, uniques, n, null); return new RegularImmutableSortedSet<E>( ImmutableList.<E>asImmutableList(contents, uniques), comparator); }
static <K, V> ImmutableSortedMap<K, V> emptyMap(Comparator<? super K> comparator) { if (Ordering.natural().equals(comparator)) { return of(); } else { return new ImmutableSortedMap<>( ImmutableSortedSet.emptySet(comparator), ImmutableList.<V>of()); } }
@GwtIncompatible // NavigableSet ImmutableSortedSet<C> createDescendingSet() { return ImmutableSortedSet.emptySet(Ordering.natural().reverse()); } }
/** * Returns a newly-created {@code ImmutableSortedSet} based on the contents of the {@code * Builder} and its comparator. */ @Override public ImmutableSortedSet<E> build() { sortAndDedup(); if (n == 0) { return emptySet(comparator); } else { forceCopy = true; return new RegularImmutableSortedSet<E>( ImmutableList.asImmutableList(elements, n), comparator); } } }
Comparator<? super E> comparator, int n, E... contents) { if (n == 0) { return emptySet(comparator);
/** * Returns an immutable sorted set containing the elements of a sorted set, sorted by the same * {@code Comparator}. That behavior differs from {@link #copyOf(Iterable)}, which always uses the * natural ordering of the elements. * * <p>Despite the method name, this method attempts to avoid actually copying the data when it is * safe to do so. The exact circumstances under which a copy will or will not be performed are * undocumented and subject to change. * * <p>This method is safe to use even when {@code sortedSet} is a synchronized or concurrent * collection that is currently being modified by another thread. * * @throws NullPointerException if {@code sortedSet} or any of its elements is null */ public static <E> ImmutableSortedSet<E> copyOfSorted(SortedSet<E> sortedSet) { Comparator<? super E> comparator = SortedIterables.comparator(sortedSet); ImmutableList<E> list = ImmutableList.copyOf(sortedSet); if (list.isEmpty()) { return emptySet(comparator); } else { return new RegularImmutableSortedSet<E>(list, comparator); } }
/** * Constructs an {@code ImmutableSortedSet} from the first {@code n} elements of {@code contents}. * If {@code k} is the size of the returned {@code ImmutableSortedSet}, then the sorted unique * elements are in the first {@code k} positions of {@code contents}, and {@code contents[i] == * null} for {@code k <= i < n}. * * <p>If {@code k == contents.length}, then {@code contents} may no longer be safe for * modification. * * @throws NullPointerException if any of the first {@code n} elements of {@code contents} is null */ static <E> ImmutableSortedSet<E> construct( Comparator<? super E> comparator, int n, E... contents) { if (n == 0) { return emptySet(comparator); } checkElementsNotNull(contents, n); Arrays.sort(contents, 0, n, comparator); int uniques = 1; for (int i = 1; i < n; i++) { E cur = contents[i]; E prev = contents[uniques - 1]; if (comparator.compare(cur, prev) != 0) { contents[uniques++] = cur; } } Arrays.fill(contents, uniques, n, null); return new RegularImmutableSortedSet<E>( ImmutableList.<E>asImmutableList(contents, uniques), comparator); }
/** * Returns a newly-created {@code ImmutableSortedSet} based on the contents of the {@code * Builder} and its comparator. */ @Override public ImmutableSortedSet<E> build() { sortAndDedup(); if (n == 0) { return emptySet(comparator); } else { forceCopy = true; return new RegularImmutableSortedSet<E>( ImmutableList.asImmutableList(elements, n), comparator); } } }
/** * Returns the empty immutable sorted set. */ public static <E> ImmutableSortedSet<E> of() { return emptySet(); }
private static <E> ImmutableSortedSet<E> emptySet( Comparator<? super E> comparator) { if (comparator == NATURAL_ORDER) { return emptySet(); } else { return new EmptyImmutableSortedSet<E>(comparator); } }
private ImmutableSortedSet<K> createKeySet() { if (isEmpty()) { return ImmutableSortedSet.emptySet(comparator); } // TODO: For better performance, don't create a separate array. Object[] array = new Object[size()]; for (int i = fromIndex; i < toIndex; i++) { array[i - fromIndex] = entries[i].getKey(); } return new RegularImmutableSortedSet<K>(array, comparator); }