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
An Interval is a range of integers that may be iterated over using a step value. Interval is an OO implementation of a for-loop.
See Also:
Serialized Form
  • Method Details

    • from

      public static Interval from​(int newFrom)
      This static from method allows Interval to act as a fluent builder for itself. It works in conjunction with the instance methods to(int) and by(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

      public Interval to​(int newTo)
      This instance to method allows Interval to act as a fluent builder for itself. It works in conjunction with the static method from(int) and instance method by(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

      public Interval by​(int newStep)
      This instance by method allows Interval to act as a fluent builder for itself. It works in conjunction with the static method from(int) and instance method to(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

      public static Interval 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

      public static Interval oneTo​(int count)
      Returns an Interval starting from 1 to the specified count value with a step value of 1.
    • oneToBy

      public static Interval oneToBy​(int count, int step)
      Returns an Interval starting from 1 to the specified count value with a step value of step.
    • zeroTo

      public static Interval zeroTo​(int count)
      Returns an Interval starting from 0 to the specified count value with a step value of 1.
    • zeroToBy

      public static Interval zeroToBy​(int count, int step)
      Returns an Interval starting from 0 to the specified count value with a step value of step.
    • fromTo

      public 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.
    • fromToExclusive

      public 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 1
    • evensFromTo

      public static Interval evensFromTo​(int from, int to)
      Returns an Interval representing the even values from the value from to the value to.
    • oddsFromTo

      public static Interval oddsFromTo​(int from, int to)
      Returns an Interval representing the odd values from the value from to the value to.
    • toSet

      public static MutableSet<Integer> toSet​(int from, int to)
      Returns an Set representing the Integer values from the value from to the value to.
    • toReverseList

      public 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.
    • toArray

      public static Integer[] toArray​(int from, int to)
      Returns an Integer array with the values inclusively between from and to.
    • toReverseArray

      public static Integer[] toReverseArray​(int from, int to)
    • fromToBy

      public 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.
    • 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

      public boolean contains​(Object object)
      Description copied from interface: RichIterable
      Returns true if the iterable has an element which responds true to element.equals(object).
      Specified by:
      contains in interface Collection<Integer>
      Specified by:
      contains in interface List<Integer>
      Specified by:
      contains in interface RichIterable<Integer>
      Overrides:
      contains in class AbstractRichIterable<Integer>
    • contains

      public boolean contains​(int value)
      Returns true if the Interval contains the specified int value.
    • factorial

      public Number factorial()
      Returns the Number result of calculating factorial for the range.
    • product

      public Number product()
      Returns the Number result of calculating product for the range.
    • forEachWithIndex

      public void forEachWithIndex​(IntIntProcedure procedure)
    • forEachWithIndex

      public void forEachWithIndex​(ObjectIntProcedure<? super Integer> objectIntProcedure)
      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 interface InternalIterable<Integer>
      Overrides:
      forEachWithIndex in class AbstractRichIterable<Integer>
    • forEachWith

      public <P> void forEachWith​(IntObjectProcedure<? super P> procedure, P parameter)
    • goForward

      public boolean goForward()
    • forEachWith

      public <P> void forEachWith​(Procedure2<? super Integer,​? super P> procedure, P parameter)
      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 interface InternalIterable<Integer>
      Overrides:
      forEachWith in class AbstractRichIterable<Integer>
    • forEach

      public void forEach​(IntProcedure procedure)
    • each

      public void each​(Procedure<? super Integer> procedure)
      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 of InternalIterable.forEach(Procedure) that has a signature conflict with Iterable.forEach(java.util.function.Consumer).
      Specified by:
      each in interface RichIterable<Integer>
      See Also:
      InternalIterable.forEach(Procedure), Iterable.forEach(java.util.function.Consumer)
    • forEach

      public void forEach​(Procedure<? super Integer> procedure, Executor executor)
      This method executes a void procedure against an executor, passing the current index of the interval.
    • run

      public void run​(Runnable runnable)
      This method runs a runnable a specified number of times against on the current thread.
    • run

      public void run​(Runnable runnable, Executor executor)
      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 interface RichIterable<Integer>
      Overrides:
      injectInto in class AbstractRichIterable<Integer>
    • injectInto

      public int injectInto​(int injectedValue, IntObjectToIntFunction<? super Integer> function)
      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 interface RichIterable<Integer>
      Overrides:
      injectInto in class AbstractRichIterable<Integer>
    • injectInto

      public long injectInto​(long injectedValue, LongObjectToLongFunction<? super Integer> function)
      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 interface RichIterable<Integer>
      Overrides:
      injectInto in class AbstractRichIterable<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 interface RichIterable<Integer>
      Overrides:
      injectInto in class AbstractRichIterable<Integer>
    • reverseForEach

      public void reverseForEach​(Procedure<? super Integer> procedure)
    • reverseInjectInto

      public <R> R reverseInjectInto​(R injectValue, Function2<? super R,​Integer,​? extends R> function)
    • addAllTo

      public <R extends Collection<Integer>> R addAllTo​(R targetCollection)
    • collect

      public <T,​ R extends Collection<T>> R collect​(Function<? super Integer,​? extends T> function, R target)
      Description copied from interface: RichIterable
      Same as RichIterable.collect(Function), except that the results are gathered into the specified target 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 interface RichIterable<Integer>
      Overrides:
      collect in class AbstractRichIterable<Integer>
      Parameters:
      function - a Function to use as the collect transformation function
      target - the Collection to append to for all elements in this RichIterable that meet select criteria function
      Returns:
      target, which contains appended elements as a result of the collect transformation
      See Also:
      RichIterable.collect(Function)
    • select

      public <R extends Collection<Integer>> R select​(Predicate<? super Integer> predicate, R target)
      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 interface RichIterable<Integer>
      Overrides:
      select in class AbstractRichIterable<Integer>
      Parameters:
      predicate - a Predicate to use as the select criteria
      target - the Collection to append to for all elements in this RichIterable that meet select criteria predicate
      Returns:
      target, which contains appended elements as a result of the select criteria
      See Also:
      RichIterable.select(Predicate)
    • reject

      public <R extends Collection<Integer>> R reject​(Predicate<? super Integer> predicate, R target)
      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 interface RichIterable<Integer>
      Overrides:
      reject in class AbstractRichIterable<Integer>
      Parameters:
      predicate - a Predicate to use as the reject criteria
      target - the Collection to append to for all elements in this RichIterable that cause Predicate#accept(Object) method to evaluate to false
      Returns:
      target, which contains appended elements as a result of the reject criteria
    • equals

      public boolean equals​(Object otherList)
      Specified by:
      equals in interface Collection<Integer>
      Specified by:
      equals in interface List<Integer>
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Collection<Integer>
      Specified by:
      hashCode in interface List<Integer>
      Overrides:
      hashCode in class Object
    • reverseThis

      public Interval 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 interface Collection<Integer>
      Specified by:
      size in interface List<Integer>
      Specified by:
      size in interface RichIterable<Integer>
      Overrides:
      size in class AbstractLazyIterable<Integer>
    • toArray

      public Integer[] toArray()
      Description copied from interface: RichIterable
      Converts this iterable to an array.
      Specified by:
      toArray in interface Collection<Integer>
      Specified by:
      toArray in interface List<Integer>
      Specified by:
      toArray in interface RichIterable<Integer>
      Overrides:
      toArray in class AbstractRichIterable<Integer>
      See Also:
      Collection.toArray()
    • toIntArray

      public int[] toIntArray()
      Converts the interval to an Integer array.
    • toString

      public String 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 interface RichIterable<Integer>
      Overrides:
      toString in class AbstractRichIterable<Integer>
      Returns:
      a string representation of this collection.
      See Also:
      AbstractCollection.toString()
    • iterator

      public Iterator<Integer> iterator()
      Specified by:
      iterator in interface Collection<Integer>
      Specified by:
      iterator in interface Iterable<Integer>
      Specified by:
      iterator in interface List<Integer>
    • getFirst

      public Integer 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 interface LazyIterable<Integer>
      Specified by:
      getFirst in interface RichIterable<Integer>
      Overrides:
      getFirst in class AbstractLazyIterable<Integer>
    • getLast

      public 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 interface RichIterable<Integer>
      Overrides:
      getLast in class AbstractLazyIterable<Integer>
    • forEach

      public void forEach​(Procedure<? super Integer> procedure, int startIndex, int endIndex)
    • forEachWithIndex

      public void forEachWithIndex​(ObjectIntProcedure<? super Integer> objectIntProcedure, int startIndex, int endIndex)
    • get

      public Integer get​(int index)
      Specified by:
      get in interface List<Integer>
    • indexOf

      public int indexOf​(Object object)
      Specified by:
      indexOf in interface List<Integer>
    • lastIndexOf

      public int lastIndexOf​(Object object)
      Specified by:
      lastIndexOf in interface List<Integer>
    • toList

      public MutableList<Integer> toList()
      Description copied from interface: RichIterable
      Converts the collection to a MutableList implementation.
      Specified by:
      toList in interface RichIterable<Integer>
      Overrides:
      toList in class AbstractRichIterable<Integer>
    • toSet

      public MutableSet<Integer> toSet()
      Description copied from interface: RichIterable
      Converts the collection to a MutableSet implementation.
      Specified by:
      toSet in interface RichIterable<Integer>
      Overrides:
      toSet in class AbstractRichIterable<Integer>
    • toBag

      public MutableBag<Integer> toBag()
      Description copied from interface: RichIterable
      Converts the collection to the default MutableBag implementation.
      Specified by:
      toBag in interface RichIterable<Integer>
      Overrides:
      toBag in class AbstractRichIterable<Integer>
    • add

      public boolean add​(Integer integer)
      Specified by:
      add in interface Collection<Integer>
      Specified by:
      add in interface List<Integer>
    • remove

      public boolean remove​(Object o)
      Specified by:
      remove in interface Collection<Integer>
      Specified by:
      remove in interface List<Integer>
    • addAll

      public boolean addAll​(Collection<? extends Integer> collection)
      Specified by:
      addAll in interface Collection<Integer>
      Specified by:
      addAll in interface List<Integer>
    • addAll

      public boolean addAll​(int index, Collection<? extends Integer> collection)
      Specified by:
      addAll in interface List<Integer>
    • removeAll

      public boolean removeAll​(Collection<?> collection)
      Specified by:
      removeAll in interface Collection<Integer>
      Specified by:
      removeAll in interface List<Integer>
    • retainAll

      public boolean retainAll​(Collection<?> collection)
      Specified by:
      retainAll in interface Collection<Integer>
      Specified by:
      retainAll in interface List<Integer>
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<Integer>
      Specified by:
      clear in interface List<Integer>
    • set

      public Integer set​(int index, Integer element)
      Specified by:
      set in interface List<Integer>
    • add

      public void add​(int index, Integer element)
      Specified by:
      add in interface List<Integer>
    • remove

      public Integer remove​(int index)
      Specified by:
      remove in interface List<Integer>
    • listIterator

      public ListIterator<Integer> listIterator()
      Specified by:
      listIterator in interface List<Integer>
    • listIterator

      public ListIterator<Integer> listIterator​(int index)
      Specified by:
      listIterator in interface List<Integer>
    • subList

      public Interval subList​(int fromIndex, int toIndex)
      Specified by:
      subList in interface List<Integer>
    • take

      public LazyIterable<Integer> take​(int count)
      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 interface LazyIterable<Integer>
      Overrides:
      take in class AbstractLazyIterable<Integer>
    • drop

      public LazyIterable<Integer> drop​(int count)
      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 interface LazyIterable<Integer>
      Overrides:
      drop in class AbstractLazyIterable<Integer>
    • distinct

      public LazyIterable<Integer> distinct()
      Description copied from interface: LazyIterable
      Creates a deferred distinct iterable to get distinct elements from the current iterable.
      Specified by:
      distinct in interface LazyIterable<Integer>
      Overrides:
      distinct in class AbstractLazyIterable<Integer>