Class ExclusiveRange

java.lang.Object
org.eclipse.xtext.xbase.lib.ExclusiveRange
All Implemented Interfaces:
Iterable<Integer>

@GwtCompatible public class ExclusiveRange extends Object implements Iterable<Integer>
A sequence of integers starting from start to end counting up or down. It excludes the end value when counting up and the start value when counting down. Examples:
new ExclusiveRange(1, 5, true)(1,2,3,4)
new ExclusiveRange(0, 0, true)()
new ExclusiveRange(0, -1, true)()
new ExclusiveRange(-1, 0, true)(-1)
new ExclusiveRange(5, 1, false)(4,3,2,1)
new ExclusiveRange(0, 0, false)()
new ExclusiveRange(-1, 0, false)()
new ExclusiveRange(0, -1, false)(-1)
As opposed to IntegerRange this class meets the requirements to iterate arrays or lists without the need for further guards, e.g.
 for(i: new ExclusiveRange(0, list.size, true)) 
   list.get(i)...
 
 for(i: new ExclusiveRange(array.length, 0, false)) 
   array.get(i)...
 
 for(i: new ExclusiveRange(0, string.indexOf('x'), true)
   string.charAt(i)...
 
Since:
2.4
Author:
Jan Koehnlein - Initial contribution and API
  • Constructor Details

    • ExclusiveRange

      @Pure public ExclusiveRange(int start, int end, boolean increment)
      Constructs a new ExclusiveRange object.
      Parameters:
      start - the start value
      end - the end value
      increment - if true, the range goes from start up to end (exclusive) if false, the range goes from end down to start (exclusive)
  • Method Details

    • iterator

      @Pure public ListIterator<Integer> iterator()
      Specified by:
      iterator in interface Iterable<Integer>
      Returns:
      a read-only ListIterator for this.
    • size

      @Pure public int size()
      Returns the number of elements in this ExclusiveRange.
      Returns:
      the number of elements in this ExclusiveRange.
    • isEmpty

      @Pure public boolean isEmpty()
      Returns whether this is range is empty.
      Returns:
      true if this range is empty.
    • contains

      @Pure public boolean contains(int number)
      Checks whether this contains the given number, i.e. whether the iterator will yield the number. This is different from interval containment: 0..2.by(2) will not contain 1.
      Parameters:
      number - the number to be checked for containment.
      Returns:
      whether this sequence contains the given number or not.