Class Range<T>

java.lang.Object
com.lokalized.Range<T>
Type Parameters:
T - the type of values contained in the range
All Implemented Interfaces:
Iterable<@NonNull T>

@NotThreadSafe public final class Range<T> extends Object implements Iterable<@NonNull T>
Represents a structurally immutable, ordered range of values.

This class is not designed to hold large or "infinite" ranges; it is not stream-based. Instead, you might supply a small representative range of values and specify the range is "infinite" if it is understood that the value pattern repeats indefinitely.

For example, you might generate an infinite powers-of-ten range with the 4 values 1, 10, 100, 1_000.

A range is Iterable, but deliberately does not implement Collection: mutation is not part of its contract. Use getValues() when list operations are needed.

The range copies its input collection and never mutates or exposes its internal list, but it does not copy the elements themselves. Mutable elements can therefore change this object's observed equality, hash code, and string representation. Elements should be immutable or otherwise safely shared when a range is used concurrently or as a map key or set member.

Ranges are constructed via static methods.

Examples:

  • Range.ofFiniteValues("a", "b", "c")
  • Range.ofInfiniteValues(1, 10, 100, 1_000, 10_000)
  • Range.emptyFiniteRange()
  • Range.emptyInfiniteRange()
Author:
Mark Allen
  • Method Details

    • ofInfiniteValues

      public static <T> @NonNull Range<T> ofInfiniteValues(@NonNull Collection<@NonNull T> values)
      Provides an infinite range for the given values.
      Type Parameters:
      T - the type of values contained in the range
      Parameters:
      values - the values of the range, not null and containing no null elements
      Returns:
      an infinite range, not null
    • ofInfiniteValues

      @SafeVarargs public static <T> @NonNull Range<T> ofInfiniteValues(@NonNull T @NonNull ... values)
      Provides an infinite range for the given values.
      Type Parameters:
      T - the type of values contained in the range
      Parameters:
      values - the values of the range, not null and containing no null elements
      Returns:
      an infinite range, not null
    • ofFiniteValues

      public static <T> @NonNull Range<T> ofFiniteValues(@NonNull Collection<@NonNull T> values)
      Provides a finite range for the given values.
      Type Parameters:
      T - the type of values contained in the range
      Parameters:
      values - the values of the range, not null and containing no null elements
      Returns:
      a finite range, not null
    • ofFiniteValues

      @SafeVarargs public static <T> @NonNull Range<T> ofFiniteValues(@NonNull T @NonNull ... values)
      Provides a finite range for the given values.
      Type Parameters:
      T - the type of values contained in the range
      Parameters:
      values - the values of the range, not null and containing no null elements
      Returns:
      a finite range, not null
    • emptyFiniteRange

      public static <T> @NonNull Range<T> emptyFiniteRange()
      Gets the empty finite range.
      Type Parameters:
      T - the type of values contained in the range
      Returns:
      the empty finite range, not null
    • emptyInfiniteRange

      public static <T> @NonNull Range<T> emptyInfiniteRange()
      Gets the empty infinite range.
      Type Parameters:
      T - the type of values contained in the range
      Returns:
      the empty infinite range, not null
    • iterator

      Returns an iterator over the values in this range in proper sequence.
      Specified by:
      iterator in interface Iterable<T>
      Returns:
      an immutable iterator over the values in this range, not null
    • toString

      Generates a String representation of this object.
      Overrides:
      toString in class Object
      Returns:
      a string representation of this object, not null
    • equals

      public boolean equals(@Nullable Object other)
      Checks if this object is equal to another one.
      Overrides:
      equals in class Object
      Parameters:
      other - the object to check, null returns false
      Returns:
      true if this is equal to the other object, false otherwise
    • hashCode

      public int hashCode()
      A hash code for this object.
      Overrides:
      hashCode in class Object
      Returns:
      a suitable hash code
    • getValues

      Gets the ordered values that comprise this range.
      Returns:
      an immutable list of the values that comprise this range, not null
    • isInfinite

      Gets whether this range is infinite.
      Returns:
      whether this range's pattern repeats indefinitely, not null
      Since:
      3.0.0