Class Interval
- All Implemented Interfaces:
Serializable
,Iterable<Integer>
,Collection<Integer>
,List<Integer>
,RandomAccess
,InternalIterable<Integer>
,LazyIterable<Integer>
,RichIterable<Integer>
public final class Interval extends AbstractLazyIterable<Integer> implements List<Integer>, Serializable, RandomAccess
- See Also:
- Serialized Form
-
Method Summary
Modifier and Type Method Description void
add(int index, Integer element)
boolean
add(Integer integer)
boolean
addAll(int index, Collection<? extends Integer> collection)
boolean
addAll(Collection<? extends Integer> collection)
<R extends Collection<Integer>>
RaddAllTo(R targetCollection)
Interval
by(int newStep)
This instanceby
method allows Interval to act as a fluent builder for itself.void
clear()
<T, R extends Collection<T>>
Rcollect(Function<? super Integer,? extends T> function, R target)
Same asRichIterable.collect(Function)
, except that the results are gathered into the specifiedtarget
collection.boolean
contains(int value)
Returns true if the Interval contains the specified int value.boolean
contains(Object object)
Returns true if the iterable has an element which responds true to element.equals(object).boolean
containsAll(int... values)
Returns true if the Interval contains all of the specified int values.boolean
containsNone(int... values)
Returns true if the Interval contains none of the specified int values.LazyIterable<Integer>
distinct()
Creates a deferred distinct iterable to get distinct elements from the current iterable.LazyIterable<Integer>
drop(int count)
Creates a deferred drop iterable for the current iterable using the specified count as the limit.void
each(Procedure<? super Integer> procedure)
The procedure is executed for each element in the iterable.boolean
equals(Object otherList)
static Interval
evensFromTo(int from, int to)
Returns an Interval representing the even values from the value from to the value to.Number
factorial()
Returns the Number result of calculating factorial for the range.void
forEach(IntProcedure procedure)
void
forEach(Procedure<? super Integer> procedure, int startIndex, int endIndex)
void
forEach(Procedure<? super Integer> procedure, Executor executor)
This method executes a void procedure against an executor, passing the current index of the interval.<P> void
forEachWith(IntObjectProcedure<? super P> procedure, P parameter)
<P> void
forEachWith(Procedure2<? super Integer,? super P> procedure, P parameter)
The procedure2 is evaluated for each element in the iterable with the specified parameter provided as the second argument.void
forEachWithIndex(IntIntProcedure procedure)
void
forEachWithIndex(ObjectIntProcedure<? super Integer> objectIntProcedure)
Iterates over the iterable passing each element and the current relative int index to the specified instance of ObjectIntProcedure.void
forEachWithIndex(ObjectIntProcedure<? super Integer> objectIntProcedure, int startIndex, int endIndex)
static Interval
from(int newFrom)
This staticfrom
method allows Interval to act as a fluent builder for itself.static Interval
fromTo(int from, int to)
Returns an Interval starting from the value from to the specified value to with a step value of 1.static Interval
fromToBy(int from, int to, int stepBy)
Returns an Interval for the range of integers inclusively between from and to with the specified stepBy value.static Interval
fromToExclusive(int from, int to)
Returns an Interval starting from the value from until the specified value to (exclusive) with a step value of 1Integer
get(int index)
Integer
getFirst()
Returns the first element of an iterable.Integer
getLast()
Returns the last element of an iterable.boolean
goForward()
int
hashCode()
int
indexOf(Object object)
double
injectInto(double injectedValue, DoubleObjectToDoubleFunction<? super Integer> function)
Returns the final double result of evaluating function using each element of the iterable and the previous evaluation result as the parameters.int
injectInto(int injectedValue, IntObjectToIntFunction<? super Integer> function)
Returns the final int result of evaluating function using each element of the iterable and the previous evaluation result as the parameters.long
injectInto(long injectedValue, LongObjectToLongFunction<? super Integer> function)
Returns the final long result of evaluating function using each element of the iterable and the previous evaluation result as the parameters.<R> R
injectInto(R injectValue, Function2<? super R,? super Integer,? extends R> function)
Returns the final result of evaluating function using each element of the iterable and the previous evaluation result as the parameters.Iterator<Integer>
iterator()
int
lastIndexOf(Object object)
ListIterator<Integer>
listIterator()
ListIterator<Integer>
listIterator(int index)
static Interval
oddsFromTo(int from, int to)
Returns an Interval representing the odd values from the value from to the value to.static Interval
oneTo(int count)
Returns an Interval starting from 1 to the specified count value with a step value of 1.static Interval
oneToBy(int count, int step)
Returns an Interval starting from 1 to the specified count value with a step value of step.Number
product()
Returns the Number result of calculating product for the range.<R extends Collection<Integer>>
Rreject(Predicate<? super Integer> predicate, R target)
Same as the reject method with one parameter but uses the specified target collection for the results.Integer
remove(int index)
boolean
remove(Object o)
boolean
removeAll(Collection<?> collection)
boolean
retainAll(Collection<?> collection)
void
reverseForEach(Procedure<? super Integer> procedure)
<R> R
reverseInjectInto(R injectValue, Function2<? super R,Integer,? extends R> function)
Interval
reverseThis()
Returns a new interval with the from and to values reversed and the step value negated.void
run(Runnable runnable)
This method runs a runnable a specified number of times against on the current thread.void
run(Runnable runnable, Executor executor)
This method runs a runnable a specified number of times against an executor.<R extends Collection<Integer>>
Rselect(Predicate<? super Integer> predicate, R target)
Same as the select method with one parameter but uses the specified target collection for the results.Integer
set(int index, Integer element)
int
size()
Returns the size of the interval.Interval
subList(int fromIndex, int toIndex)
LazyIterable<Integer>
take(int count)
Creates a deferred take iterable for the current iterable using the specified count as the limit.Interval
to(int newTo)
This instanceto
method allows Interval to act as a fluent builder for itself.Integer[]
toArray()
Converts this iterable to an array.static Integer[]
toArray(int from, int to)
Returns an Integer array with the values inclusively between from and to.MutableBag<Integer>
toBag()
Converts the collection to the default MutableBag implementation.int[]
toIntArray()
Converts the interval to an Integer array.MutableList<Integer>
toList()
Converts the collection to a MutableList implementation.static Integer[]
toReverseArray(int from, int to)
static MutableList<Integer>
toReverseList(int from, int to)
Returns a MutableList representing the Integer values from the value from to the value to in reverse.MutableSet<Integer>
toSet()
Converts the collection to a MutableSet implementation.static MutableSet<Integer>
toSet(int from, int to)
Returns an Set representing the Integer values from the value from to the value to.String
toString()
Returns a string with the elements of the iterable separated by commas with spaces and enclosed in square brackets.static Interval
zero()
Returns an Interval starting at zero.static Interval
zeroTo(int count)
Returns an Interval starting from 0 to the specified count value with a step value of 1.static Interval
zeroToBy(int count, int step)
Returns an Interval starting from 0 to the specified count value with a step value of step.Methods inherited from class org.eclipse.collections.impl.lazy.AbstractLazyIterable
aggregateInPlaceBy, asLazy, chunk, collect, collectBoolean, collectByte, collectChar, collectDouble, collectFloat, collectIf, collectInt, collectLong, collectShort, collectWith, concatenate, dropWhile, flatCollect, getOnly, groupBy, groupByEach, groupByUniqueKey, into, isEmpty, maxByOptional, maxOptional, maxOptional, minByOptional, minOptional, minOptional, partition, partitionWith, reject, rejectWith, select, selectInstancesOf, selectWith, sumByDouble, sumByFloat, sumByInt, sumByLong, takeWhile, tap, toArray, toStack, zip, zipWithIndex
Methods inherited from class org.eclipse.collections.impl.AbstractRichIterable
allSatisfy, allSatisfyWith, anySatisfy, anySatisfyWith, appendString, appendString, collectBoolean, collectByte, collectChar, collectDouble, collectFloat, collectIf, collectInt, collectLong, collectShort, collectWith, containsAll, containsAllArguments, containsAllIterable, count, countByEach, countWith, detect, detectOptional, detectWith, detectWithIfNone, detectWithOptional, flatCollect, forEach, groupBy, groupByEach, groupByUniqueKey, injectInto, max, max, maxBy, min, min, minBy, noneSatisfy, noneSatisfyWith, rejectWith, selectWith, sumOfDouble, sumOfFloat, sumOfInt, sumOfLong, toBiMap, toMap, toSortedBag, toSortedBag, toSortedBagBy, toSortedListBy, toSortedMap, toSortedMap, toSortedMapBy, toSortedSet, toSortedSet, toSortedSetBy, zip, zipWithIndex
Methods inherited from interface java.util.List
containsAll, isEmpty, replaceAll, sort, spliterator, toArray
Methods inherited from interface org.eclipse.collections.api.RichIterable
aggregateBy, aggregateBy, allSatisfy, allSatisfyWith, anySatisfy, anySatisfyWith, appendString, appendString, appendString, collectBoolean, collectByte, collectChar, collectDouble, collectFloat, collectIf, collectInt, collectLong, collectShort, collectWith, containsAll, containsAllArguments, containsAllIterable, containsBy, count, countBy, countBy, countByEach, countByEach, countByWith, countByWith, countWith, detect, detectIfNone, detectOptional, detectWith, detectWithIfNone, detectWithOptional, flatCollect, flatCollectBoolean, flatCollectByte, flatCollectChar, flatCollectDouble, flatCollectFloat, flatCollectInt, flatCollectLong, flatCollectShort, flatCollectWith, forEach, getAny, groupBy, groupByAndCollect, groupByEach, groupByUniqueKey, injectInto, makeString, makeString, makeString, max, max, maxBy, min, min, minBy, noneSatisfy, noneSatisfyWith, notEmpty, reduce, reduceInPlace, reduceInPlace, rejectWith, selectWith, summarizeDouble, summarizeFloat, summarizeInt, summarizeLong, sumOfDouble, sumOfFloat, sumOfInt, sumOfLong, toBiMap, toMap, toMap, toSortedBag, toSortedBag, toSortedBagBy, toSortedList, toSortedList, toSortedListBy, toSortedMap, toSortedMap, toSortedMapBy, toSortedSet, toSortedSet, toSortedSetBy, zip, zipWithIndex
-
Method Details
-
from
This staticfrom
method allows Interval to act as a fluent builder for itself. It works in conjunction with the instance methodsto(int)
andby(int)
.Usage Example:
Interval interval1 = Interval.from(1).to(5); // results in: 1, 2, 3, 4, 5. Interval interval2 = Interval.from(1).to(10).by(2); // results in: 1, 3, 5, 7, 9.
-
to
This instanceto
method allows Interval to act as a fluent builder for itself. It works in conjunction with the static methodfrom(int)
and instance methodby(int)
.Usage Example:
Interval interval1 = Interval.from(1).to(5); // results in: 1, 2, 3, 4, 5. Interval interval2 = Interval.from(1).to(10).by(2); // results in: 1, 3, 5, 7, 9.
-
by
This instanceby
method allows Interval to act as a fluent builder for itself. It works in conjunction with the static methodfrom(int)
and instance methodto(int)
.Usage Example:
Interval interval1 = Interval.from(1).to(5); // results in: 1, 2, 3, 4, 5. Interval interval2 = Interval.from(1).to(10).by(2); // results in: 1, 3, 5, 7, 9.
-
zero
Returns an Interval starting at zero.Usage Example:
Interval interval1 = Interval.zero().to(5); // results in: 0, 1, 2, 3, 4, 5. Interval interval2 = Interval.zero().to(10).by(2); // results in: 0, 2, 4, 6, 8, 10.
-
oneTo
Returns an Interval starting from 1 to the specified count value with a step value of 1. -
oneToBy
Returns an Interval starting from 1 to the specified count value with a step value of step. -
zeroTo
Returns an Interval starting from 0 to the specified count value with a step value of 1. -
zeroToBy
Returns an Interval starting from 0 to the specified count value with a step value of step. -
fromTo
Returns an Interval starting from the value from to the specified value to with a step value of 1. -
fromToExclusive
Returns an Interval starting from the value from until the specified value to (exclusive) with a step value of 1 -
evensFromTo
Returns an Interval representing the even values from the value from to the value to. -
oddsFromTo
Returns an Interval representing the odd values from the value from to the value to. -
toSet
Returns an Set representing the Integer values from the value from to the value to. -
toReverseList
Returns a MutableList representing the Integer values from the value from to the value to in reverse. -
toArray
Returns an Integer array with the values inclusively between from and to. -
toReverseArray
-
fromToBy
Returns an Interval for the range of integers inclusively between from and to with the specified stepBy value. -
containsAll
public boolean containsAll(int... values)Returns true if the Interval contains all of the specified int values. -
containsNone
public boolean containsNone(int... values)Returns true if the Interval contains none of the specified int values. -
contains
Description copied from interface:RichIterable
Returns true if the iterable has an element which responds true to element.equals(object).- Specified by:
contains
in interfaceCollection<Integer>
- Specified by:
contains
in interfaceList<Integer>
- Specified by:
contains
in interfaceRichIterable<Integer>
- Overrides:
contains
in classAbstractRichIterable<Integer>
-
contains
public boolean contains(int value)Returns true if the Interval contains the specified int value. -
factorial
Returns the Number result of calculating factorial for the range. -
product
Returns the Number result of calculating product for the range. -
forEachWithIndex
-
forEachWithIndex
Description copied from interface:InternalIterable
Iterates over the iterable passing each element and the current relative int index to the specified instance of ObjectIntProcedure.Example using a Java 8 lambda:
people.forEachWithIndex((Person person, int index) -> LOGGER.info("Index: " + index + " person: " + person.getName()));
Example using an anonymous inner class:
people.forEachWithIndex(new ObjectIntProcedure<Person>() { public void value(Person person, int index) { LOGGER.info("Index: " + index + " person: " + person.getName()); } });
- Specified by:
forEachWithIndex
in interfaceInternalIterable<Integer>
- Overrides:
forEachWithIndex
in classAbstractRichIterable<Integer>
-
forEachWith
-
goForward
public boolean goForward() -
forEachWith
Description copied from interface:InternalIterable
The procedure2 is evaluated for each element in the iterable with the specified parameter provided as the second argument.Example using a Java 8 lambda:
people.forEachWith((Person person, Person other) -> { if (person.isRelatedTo(other)) { LOGGER.info(person.getName()); } }, fred);
Example using an anonymous inner class:
people.forEachWith(new Procedure2<Person, Person>() { public void value(Person person, Person other) { if (person.isRelatedTo(other)) { LOGGER.info(person.getName()); } } }, fred);
- Specified by:
forEachWith
in interfaceInternalIterable<Integer>
- Overrides:
forEachWith
in classAbstractRichIterable<Integer>
-
forEach
-
each
Description copied from interface:RichIterable
The procedure is executed for each element in the iterable.Example using a Java 8 lambda expression:
people.each(person -> LOGGER.info(person.getName()));
Example using an anonymous inner class:
people.each(new Procedure<Person>() { public void value(Person person) { LOGGER.info(person.getName()); } });
This method is a variant ofInternalIterable.forEach(Procedure)
that has a signature conflict withIterable.forEach(java.util.function.Consumer)
.- Specified by:
each
in interfaceRichIterable<Integer>
- See Also:
InternalIterable.forEach(Procedure)
,Iterable.forEach(java.util.function.Consumer)
-
forEach
This method executes a void procedure against an executor, passing the current index of the interval. -
run
This method runs a runnable a specified number of times against on the current thread. -
run
This method runs a runnable a specified number of times against an executor. The method is effectively asynchronous because it does not wait for all of the runnables to finish. -
injectInto
public <R> R injectInto(R injectValue, Function2<? super R,? super Integer,? extends R> function)Description copied from interface:RichIterable
Returns the final result of evaluating function using each element of the iterable and the previous evaluation result as the parameters. The injected value is used for the first parameter of the first evaluation, and the current item in the iterable is used as the second parameter. This method is commonly called fold or sometimes reduce.- Specified by:
injectInto
in interfaceRichIterable<Integer>
- Overrides:
injectInto
in classAbstractRichIterable<Integer>
-
injectInto
Description copied from interface:RichIterable
Returns the final int result of evaluating function using each element of the iterable and the previous evaluation result as the parameters. The injected value is used for the first parameter of the first evaluation, and the current item in the iterable is used as the second parameter.- Specified by:
injectInto
in interfaceRichIterable<Integer>
- Overrides:
injectInto
in classAbstractRichIterable<Integer>
-
injectInto
Description copied from interface:RichIterable
Returns the final long result of evaluating function using each element of the iterable and the previous evaluation result as the parameters. The injected value is used for the first parameter of the first evaluation, and the current item in the iterable is used as the second parameter.- Specified by:
injectInto
in interfaceRichIterable<Integer>
- Overrides:
injectInto
in classAbstractRichIterable<Integer>
-
injectInto
public double injectInto(double injectedValue, DoubleObjectToDoubleFunction<? super Integer> function)Description copied from interface:RichIterable
Returns the final double result of evaluating function using each element of the iterable and the previous evaluation result as the parameters. The injected value is used for the first parameter of the first evaluation, and the current item in the iterable is used as the second parameter.- Specified by:
injectInto
in interfaceRichIterable<Integer>
- Overrides:
injectInto
in classAbstractRichIterable<Integer>
-
reverseForEach
-
reverseInjectInto
-
addAllTo
-
collect
public <T, R extends Collection<T>> R collect(Function<? super Integer,? extends T> function, R target)Description copied from interface:RichIterable
Same asRichIterable.collect(Function)
, except that the results are gathered into the specifiedtarget
collection.Example using a Java 8 lambda expression:
MutableList<String> names = people.collect(person -> person.getFirstName() + " " + person.getLastName(), Lists.mutable.empty());
Example using an anonymous inner class:
MutableList<String> names = people.collect(new Function<Person, String>() { public String valueOf(Person person) { return person.getFirstName() + " " + person.getLastName(); } }, Lists.mutable.empty());
- Specified by:
collect
in interfaceRichIterable<Integer>
- Overrides:
collect
in classAbstractRichIterable<Integer>
- Parameters:
function
- aFunction
to use as the collect transformation functiontarget
- the Collection to append to for all elements in thisRichIterable
that meet select criteriafunction
- Returns:
target
, which contains appended elements as a result of the collect transformation- See Also:
RichIterable.collect(Function)
-
select
Description copied from interface:RichIterable
Same as the select method with one parameter but uses the specified target collection for the results.Example using a Java 8 lambda expression:
MutableList<Person> selected = people.select(person -> person.person.getLastName().equals("Smith"), Lists.mutable.empty());
Example using an anonymous inner class:
MutableList<Person> selected = people.select(new Predicate<Person>() { public boolean accept(Person person) { return person.person.getLastName().equals("Smith"); } }, Lists.mutable.empty());
- Specified by:
select
in interfaceRichIterable<Integer>
- Overrides:
select
in classAbstractRichIterable<Integer>
- Parameters:
predicate
- aPredicate
to use as the select criteriatarget
- the Collection to append to for all elements in thisRichIterable
that meet select criteriapredicate
- Returns:
target
, which contains appended elements as a result of the select criteria- See Also:
RichIterable.select(Predicate)
-
reject
Description copied from interface:RichIterable
Same as the reject method with one parameter but uses the specified target collection for the results.Example using a Java 8 lambda expression:
MutableList<Person> rejected = people.reject(person -> person.person.getLastName().equals("Smith"), Lists.mutable.empty());
Example using an anonymous inner class:
MutableList<Person> rejected = people.reject(new Predicate<Person>() { public boolean accept(Person person) { return person.person.getLastName().equals("Smith"); } }, Lists.mutable.empty());
- Specified by:
reject
in interfaceRichIterable<Integer>
- Overrides:
reject
in classAbstractRichIterable<Integer>
- Parameters:
predicate
- aPredicate
to use as the reject criteriatarget
- the Collection to append to for all elements in thisRichIterable
that causePredicate#accept(Object)
method to evaluate to false- Returns:
target
, which contains appended elements as a result of the reject criteria
-
equals
-
hashCode
public int hashCode() -
reverseThis
Returns a new interval with the from and to values reversed and the step value negated. -
size
public int size()Returns the size of the interval.- Specified by:
size
in interfaceCollection<Integer>
- Specified by:
size
in interfaceList<Integer>
- Specified by:
size
in interfaceRichIterable<Integer>
- Overrides:
size
in classAbstractLazyIterable<Integer>
-
toArray
Description copied from interface:RichIterable
Converts this iterable to an array.- Specified by:
toArray
in interfaceCollection<Integer>
- Specified by:
toArray
in interfaceList<Integer>
- Specified by:
toArray
in interfaceRichIterable<Integer>
- Overrides:
toArray
in classAbstractRichIterable<Integer>
- See Also:
Collection.toArray()
-
toIntArray
public int[] toIntArray()Converts the interval to an Integer array. -
toString
Description copied from class:AbstractRichIterable
Returns a string with the elements of the iterable separated by commas with spaces and enclosed in square brackets.Assert.assertEquals("[]", Lists.mutable.empty().toString()); Assert.assertEquals("[1]", Lists.mutable.with(1).toString()); Assert.assertEquals("[1, 2, 3]", Lists.mutable.with(1, 2, 3).toString());
- Specified by:
toString
in interfaceRichIterable<Integer>
- Overrides:
toString
in classAbstractRichIterable<Integer>
- Returns:
- a string representation of this collection.
- See Also:
AbstractCollection.toString()
-
iterator
-
getFirst
Description copied from interface:RichIterable
Returns the first element of an iterable. In the case of a List it is the element at the first index. In the case of any other Collection, it is the first element that would be returned during an iteration. If the iterable is empty, null is returned. If null is a valid element of the container, then a developer would need to check to see if the iterable is empty to validate that a null result was not due to the container being empty.The order of Sets are not guaranteed (except for TreeSets and other Ordered Set implementations), so if you use this method, the first element could be any element from the Set.
- Specified by:
getFirst
in interfaceLazyIterable<Integer>
- Specified by:
getFirst
in interfaceRichIterable<Integer>
- Overrides:
getFirst
in classAbstractLazyIterable<Integer>
-
getLast
Description copied from interface:RichIterable
Returns the last element of an iterable. In the case of a List it is the element at the last index. In the case of any other Collection, it is the last element that would be returned during an iteration. If the iterable is empty, null is returned. If null is a valid element of the container, then a developer would need to check to see if the iterable is empty to validate that a null result was not due to the container being empty.The order of Sets are not guaranteed (except for TreeSets and other Ordered Set implementations), so if you use this method, the last element could be any element from the Set.
- Specified by:
getLast
in interfaceRichIterable<Integer>
- Overrides:
getLast
in classAbstractLazyIterable<Integer>
-
forEach
-
forEachWithIndex
public void forEachWithIndex(ObjectIntProcedure<? super Integer> objectIntProcedure, int startIndex, int endIndex) -
get
-
indexOf
-
lastIndexOf
- Specified by:
lastIndexOf
in interfaceList<Integer>
-
toList
Description copied from interface:RichIterable
Converts the collection to a MutableList implementation.- Specified by:
toList
in interfaceRichIterable<Integer>
- Overrides:
toList
in classAbstractRichIterable<Integer>
-
toSet
Description copied from interface:RichIterable
Converts the collection to a MutableSet implementation.- Specified by:
toSet
in interfaceRichIterable<Integer>
- Overrides:
toSet
in classAbstractRichIterable<Integer>
-
toBag
Description copied from interface:RichIterable
Converts the collection to the default MutableBag implementation.- Specified by:
toBag
in interfaceRichIterable<Integer>
- Overrides:
toBag
in classAbstractRichIterable<Integer>
-
add
-
remove
-
addAll
-
addAll
-
removeAll
-
retainAll
-
clear
public void clear() -
set
-
add
-
remove
-
listIterator
- Specified by:
listIterator
in interfaceList<Integer>
-
listIterator
- Specified by:
listIterator
in interfaceList<Integer>
-
subList
-
take
Description copied from interface:LazyIterable
Creates a deferred take iterable for the current iterable using the specified count as the limit.- Specified by:
take
in interfaceLazyIterable<Integer>
- Overrides:
take
in classAbstractLazyIterable<Integer>
-
drop
Description copied from interface:LazyIterable
Creates a deferred drop iterable for the current iterable using the specified count as the limit.- Specified by:
drop
in interfaceLazyIterable<Integer>
- Overrides:
drop
in classAbstractLazyIterable<Integer>
-
distinct
Description copied from interface:LazyIterable
Creates a deferred distinct iterable to get distinct elements from the current iterable.- Specified by:
distinct
in interfaceLazyIterable<Integer>
- Overrides:
distinct
in classAbstractLazyIterable<Integer>
-