Class ContiguousSet<C extends java.lang.Comparable>

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Iterable<C>, java.util.Collection<C>, java.util.NavigableSet<C>, java.util.Set<C>, java.util.SortedSet<C>

    @GwtCompatible(emulated=true)
    public abstract class ContiguousSet<C extends java.lang.Comparable>
    extends ImmutableSortedSet<C>
    A sorted set of contiguous values in a given DiscreteDomain. Example:
    
     ContiguousSet.create(Range.closed(5, 42), DiscreteDomain.integers())
     

    Note that because bounded ranges over int and long values are so common, this particular example can be written as just:

    
     ContiguousSet.closed(5, 42)
     

    Warning: Be extremely careful what you do with conceptually large instances (such as ContiguousSet.create(Range.greaterThan(0), DiscreteDomain.integers()). Certain operations on such a set can be performed efficiently, but others (such as Set.hashCode() or Collections.frequency(java.util.Collection<?>, java.lang.Object)) can cause major performance problems.

    Since:
    10.0
    Author:
    Gregory Kick
    See Also:
    Serialized Form
    • Method Detail

      • create

        public static <C extends java.lang.Comparable> ContiguousSet<C> create​(Range<C> range,
                                                                               DiscreteDomain<C> domain)
        Returns a ContiguousSet containing the same values in the given domain contained by the range.
        Throws:
        java.lang.IllegalArgumentException - if neither range nor the domain has a lower bound, or if neither has an upper bound
        Since:
        13.0
      • closed

        public static ContiguousSet<java.lang.Integer> closed​(int lower,
                                                              int upper)
        Returns a nonempty contiguous set containing all int values from lower (inclusive) to upper (inclusive). (These are the same values contained in Range.closed(lower, upper).)
        Throws:
        java.lang.IllegalArgumentException - if lower is greater than upper
        Since:
        23.0
      • closed

        public static ContiguousSet<java.lang.Long> closed​(long lower,
                                                           long upper)
        Returns a nonempty contiguous set containing all long values from lower (inclusive) to upper (inclusive). (These are the same values contained in Range.closed(lower, upper).)
        Throws:
        java.lang.IllegalArgumentException - if lower is greater than upper
        Since:
        23.0
      • closedOpen

        public static ContiguousSet<java.lang.Integer> closedOpen​(int lower,
                                                                  int upper)
        Returns a contiguous set containing all int values from lower (inclusive) to upper (exclusive). If the endpoints are equal, an empty set is returned. (These are the same values contained in Range.closedOpen(lower, upper).)
        Throws:
        java.lang.IllegalArgumentException - if lower is greater than upper
        Since:
        23.0
      • closedOpen

        public static ContiguousSet<java.lang.Long> closedOpen​(long lower,
                                                               long upper)
        Returns a contiguous set containing all long values from lower (inclusive) to upper (exclusive). If the endpoints are equal, an empty set is returned. (These are the same values contained in Range.closedOpen(lower, upper).)
        Throws:
        java.lang.IllegalArgumentException - if lower is greater than upper
        Since:
        23.0
      • headSet

        public ContiguousSet<CheadSet​(C toElement)
        Description copied from class: ImmutableSortedSet

        This method returns a serializable ImmutableSortedSet.

        The SortedSet.headSet(E) documentation states that a subset of a subset throws an IllegalArgumentException if passed a toElement greater than an earlier toElement. However, this method doesn't throw an exception in that situation, but instead keeps the original toElement.

        Specified by:
        headSet in interface java.util.NavigableSet<C extends java.lang.Comparable>
        Specified by:
        headSet in interface java.util.SortedSet<C extends java.lang.Comparable>
        Overrides:
        headSet in class ImmutableSortedSet<C extends java.lang.Comparable>
      • subSet

        public ContiguousSet<CsubSet​(C fromElement,
                                       C toElement)
        Description copied from class: ImmutableSortedSet

        This method returns a serializable ImmutableSortedSet.

        The SortedSet.subSet(E, E) documentation states that a subset of a subset throws an IllegalArgumentException if passed a fromElement smaller than an earlier fromElement. However, this method doesn't throw an exception in that situation, but instead keeps the original fromElement. Similarly, this method keeps the original toElement, instead of throwing an exception, if passed a toElement greater than an earlier toElement.

        Specified by:
        subSet in interface java.util.NavigableSet<C extends java.lang.Comparable>
        Specified by:
        subSet in interface java.util.SortedSet<C extends java.lang.Comparable>
        Overrides:
        subSet in class ImmutableSortedSet<C extends java.lang.Comparable>
      • subSet

        @GwtIncompatible
        public ContiguousSet<CsubSet​(C fromElement,
                                       boolean fromInclusive,
                                       C toElement,
                                       boolean toInclusive)
        Specified by:
        subSet in interface java.util.NavigableSet<C extends java.lang.Comparable>
        Overrides:
        subSet in class ImmutableSortedSet<C extends java.lang.Comparable>
        Since:
        12.0
      • tailSet

        public ContiguousSet<CtailSet​(C fromElement)
        Description copied from class: ImmutableSortedSet

        This method returns a serializable ImmutableSortedSet.

        The SortedSet.tailSet(E) documentation states that a subset of a subset throws an IllegalArgumentException if passed a fromElement smaller than an earlier fromElement. However, this method doesn't throw an exception in that situation, but instead keeps the original fromElement.

        Specified by:
        tailSet in interface java.util.NavigableSet<C extends java.lang.Comparable>
        Specified by:
        tailSet in interface java.util.SortedSet<C extends java.lang.Comparable>
        Overrides:
        tailSet in class ImmutableSortedSet<C extends java.lang.Comparable>
      • range

        public abstract Range<Crange()
        Returns a range, closed on both ends, whose endpoints are the minimum and maximum values contained in this set. This is equivalent to range(CLOSED, CLOSED).
        Throws:
        java.util.NoSuchElementException - if this set is empty
      • range

        public abstract Range<Crange​(BoundType lowerBoundType,
                                       BoundType upperBoundType)
        Returns the minimal range with the given boundary types for which all values in this set are contained within the range.

        Note that this method will return ranges with unbounded endpoints if BoundType.OPEN is requested for a domain minimum or maximum. For example, if set was created from the range [1..Integer.MAX_VALUE] then set.range(CLOSED, OPEN) must return [1..∞).

        Throws:
        java.util.NoSuchElementException - if this set is empty
      • toString

        public java.lang.String toString()
        Returns a shorthand representation of the contents such as "[1..100]".
        Overrides:
        toString in class java.util.AbstractCollection<C extends java.lang.Comparable>
      • asList

        public ImmutableList<E> asList()
        Description copied from class: ImmutableCollection
        Returns an ImmutableList containing the same elements, in the same order, as this collection.

        Performance note: in most cases this method can return quickly without actually copying anything. The exact circumstances under which the copy is performed are undefined and subject to change.

        Overrides:
        asList in class ImmutableCollection<E>