Class Range<T>
- All Implemented Interfaces:
Iterable<T>
,Collection<T>
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
.
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 Summary
Modifier and TypeMethodDescriptionboolean
Deprecated.Unsupported operation; this type is immutable.boolean
addAll
(Collection<? extends T> c) Deprecated.Unsupported operation; this type is immutable.void
clear()
Deprecated.Unsupported operation; this type is immutable.boolean
Returns true if this range contains the specified value.boolean
containsAll
(Collection<?> c) Returns true if this range contains all of the elements of the specified collection.static <T> Range<T>
Gets the empty finite range.static <T> Range<T>
Gets the empty infinite range.boolean
Checks if this object is equal to another one.Gets whether this range is infinite.Gets the values that comprise this range.int
hashCode()
A hash code for this object.boolean
isEmpty()
Returns true if this range contains no elements.iterator()
Returns an iterator over the values in this range in proper sequence.static <T> Range<T>
ofFiniteValues
(Collection<T> values) Provides a finite range for the given values.static <T> Range<T>
ofFiniteValues
(T... values) Provides a finite range for the given values.static <T> Range<T>
ofInfiniteValues
(Collection<T> values) Provides an infinite range for the given values.static <T> Range<T>
ofInfiniteValues
(T... values) Provides an infinite range for the given values.boolean
Deprecated.Unsupported operation; this type is immutable.boolean
removeAll
(Collection<?> c) Deprecated.Unsupported operation; this type is immutable.boolean
retainAll
(Collection<?> c) Deprecated.Unsupported operation; this type is immutable.int
size()
Returns the number of elements in this range.Object[]
toArray()
Returns an array containing all of the values in this range in proper sequence (from first to last value).<T1> T1[]
toArray
(T1[] a) Returns an array containing all of the values in this range in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.toString()
Generates aString
representation of this object.Methods inherited from interface java.util.Collection
parallelStream, removeIf, spliterator, stream, toArray
-
Method Details
-
ofInfiniteValues
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- Returns:
- an infinite range, not null
-
ofInfiniteValues
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, may be null- Returns:
- an infinite range, not null
-
ofFiniteValues
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- Returns:
- a finite range, not null
-
ofFiniteValues
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, may be null- Returns:
- a finite range, not null
-
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
Gets the empty infinite range.- Type Parameters:
T
- the type of values contained in the range- Returns:
- the empty infinite range, not null
-
size
Returns the number of elements in this range.- Specified by:
size
in interfaceCollection<T>
- Returns:
- the number of elements in this range
-
isEmpty
Returns true if this range contains no elements.- Specified by:
isEmpty
in interfaceCollection<T>
- Returns:
- true if this range contains no elements
-
contains
Returns true if this range contains the specified value.More formally, returns true if and only if this range contains at least one value v such that
(o==null ? v==null : o.equals(v))
.- Specified by:
contains
in interfaceCollection<T>
- Parameters:
value
- value whose presence in this range is to be tested- Returns:
- true if this range contains the specified value
-
iterator
Returns an iterator over the values in this range in proper sequence.The returned iterator is fail-fast.
-
toArray
Returns an array containing all of the values in this range in proper sequence (from first to last value).The returned array will be "safe" in that no references to it are maintained by this range. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array.
This method acts as bridge between array-based and collection-based APIs.
- Specified by:
toArray
in interfaceCollection<T>
- Returns:
- an array containing all of the values in this range in proper sequence, not null
-
toArray
Returns an array containing all of the values in this range in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the range fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this range.If the range fits in the specified array with room to spare (i.e., the array has more elements than the range), the element in the array immediately following the end of the collection is set to null. (This is useful in determining the length of the range only if the caller knows that the range does not contain any null elements.)
- Specified by:
toArray
in interfaceCollection<T>
- Type Parameters:
T1
- the runtime type of the array to contain the collection- Parameters:
a
- the array into which the values of the range are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose. not null- Returns:
- an array containing the values of the range, not null
-
add
Deprecated.Unsupported operation; this type is immutable.Guaranteed to throw an exception and leave the range unmodified.- Specified by:
add
in interfaceCollection<T>
- Parameters:
t
- the value to add, ignored- Returns:
- no return value; this method always throws UnsupportedOperationException
- Throws:
UnsupportedOperationException
- always
-
remove
Deprecated.Unsupported operation; this type is immutable.Guaranteed to throw an exception and leave the range unmodified.- Specified by:
remove
in interfaceCollection<T>
- Parameters:
o
- the value to remove, ignored- Returns:
- no return value; this method always throws UnsupportedOperationException
- Throws:
UnsupportedOperationException
- always
-
containsAll
Returns true if this range contains all of the elements of the specified collection.- Specified by:
containsAll
in interfaceCollection<T>
- Parameters:
c
- collection to be checked for containment in this range, not null- Returns:
- true if this range contains all of the elements of the specified collection
-
addAll
Deprecated.Unsupported operation; this type is immutable.Guaranteed to throw an exception and leave the range unmodified.- Specified by:
addAll
in interfaceCollection<T>
- Parameters:
c
- collection containing elements to be added to this range, ignored- Returns:
- no return value; this method always throws UnsupportedOperationException
- Throws:
UnsupportedOperationException
- always
-
removeAll
Deprecated.Unsupported operation; this type is immutable.Guaranteed to throw an exception and leave the range unmodified.- Specified by:
removeAll
in interfaceCollection<T>
- Parameters:
c
- collection containing elements to be removed from this range, ignored- Returns:
- no return value; this method always throws UnsupportedOperationException
- Throws:
UnsupportedOperationException
- always
-
retainAll
Deprecated.Unsupported operation; this type is immutable.Guaranteed to throw an exception and leave the range unmodified.- Specified by:
retainAll
in interfaceCollection<T>
- Parameters:
c
- collection containing elements to be retained in this range, ignored- Returns:
- no return value; this method always throws UnsupportedOperationException
- Throws:
UnsupportedOperationException
- always
-
clear
Deprecated.Unsupported operation; this type is immutable.Guaranteed to throw an exception and leave the range unmodified.- Specified by:
clear
in interfaceCollection<T>
- Throws:
UnsupportedOperationException
- always
-
toString
Generates aString
representation of this object. -
equals
Checks if this object is equal to another one.- Specified by:
equals
in interfaceCollection<T>
- Overrides:
equals
in classObject
- Parameters:
other
- the object to check, null returns false- Returns:
- true if this is equal to the other object, false otherwise
-
hashCode
A hash code for this object.- Specified by:
hashCode
in interfaceCollection<T>
- Overrides:
hashCode
in classObject
- Returns:
- a suitable hash code
-
getValues
Gets the values that comprise this range.- Returns:
- the values that comprise this range, not null
-
getInfinite
Gets whether this range is infinite.- Returns:
- whether this range is infinite - that is, whether the range's pattern repeats indefinitely, not null
-