Skip to content

Instantly share code, notes, and snippets.

@digulla
Created October 2, 2012 08:33
Show Gist options
  • Save digulla/3817358 to your computer and use it in GitHub Desktop.
Save digulla/3817358 to your computer and use it in GitHub Desktop.
IntRange for Xtend which works as Java developers expect
/*
Copyright (c) 2012, Aaron Digulla
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import java.util.Iterator;
public class IntRange implements Iterable<Integer> {
private int start;
private int end;
public IntRange( int end ) {
this( 0, end );
}
public IntRange( int start, int end ) {
this.start = start;
this.end = end;
}
@Override
public Iterator<Integer> iterator() {
return new IntIterator( start, end );
}
public static class IntIterator implements Iterator<Integer> {
private int current;
private final int end;
private final int step;
public IntIterator( int start, int end ) {
this.current = start;
this.end = end;
this.step = ( start < end ) ? 1 : ( start == end ) ? 0 : -1;
}
@Override
public boolean hasNext() {
return current != end;
}
@Override
public Integer next() {
int result = current;
current += step;
return result;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
public boolean contains( int value ) {
if( start <= end ) {
return start <= value && value < end;
} else {
return end < value && value <= start;
}
}
public static IntRange range( final int end ) {
return new IntRange( end );
}
public static IntRange range( final Integer end ) {
if (end == null) {
throw new NullPointerException("a");
}
return new IntRange( end );
}
public static IntRange range( final int start, final int end ) {
return new IntRange( start, end );
}
public static IntRange range( final Integer start, final Integer end ) {
if (start == null) {
throw new NullPointerException("a");
}
if (end == null) {
throw new NullPointerException("a");
}
return new IntRange( start, end );
}
public static Iterable<Integer> operator_upTo(final Integer a, Number b) {
if (a == null)
throw new NullPointerException("a");
return new IntRange( a, b.intValue() );
}
}
package com.avanon.blu.gen.xtext;
/*
Copyright (c) 2012, Aaron Digulla
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import static org.junit.Assert.*;
import org.junit.Test;
import com.google.common.collect.Iterators;
public class IntRangeTest {
@Test
public void test0to10() {
IntRange range = new IntRange( 10 );
assertFalse( range.contains( -1 ) );
for( int i=0; i<10; i++ ) {
assertTrue( "Testing " + i, range.contains( i ) );
}
assertFalse( range.contains( 10 ) );
assertFalse( range.contains( 11 ) );
assertEquals( "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]", Iterators.toString( range.iterator() ) );
}
@Test
public void test0to0() {
IntRange range = new IntRange( 0 );
assertFalse( range.contains( -1 ) );
assertFalse( range.contains( 0 ) );
assertFalse( range.contains( 1 ) );
assertEquals( "[]", Iterators.toString( range.iterator() ) );
}
@Test
public void test5to5() {
IntRange range = IntRange.range( 5, 5 );
assertFalse( range.contains( 4 ) );
assertFalse( range.contains( 5 ) );
assertFalse( range.contains( 6 ) );
assertEquals( "[]", Iterators.toString( range.iterator() ) );
}
@Test
public void test0to1() {
IntRange range = IntRange.range( 1 );
assertFalse( range.contains( -1 ) );
assertTrue( range.contains( 0 ) );
assertFalse( range.contains( 1 ) );
assertEquals( "[0]", Iterators.toString( range.iterator() ) );
}
@Test
public void test1to0() {
IntRange range = new IntRange( 1, 0 );
assertFalse( range.contains( -1 ) );
assertFalse( range.contains( 0 ) );
assertTrue( range.contains( 1 ) );
assertEquals( "[1]", Iterators.toString( range.iterator() ) );
}
@Test
public void test_1to0() {
IntRange range = new IntRange( -1, 0 );
assertTrue( range.contains( -1 ) );
assertFalse( range.contains( 0 ) );
assertFalse( range.contains( 1 ) );
assertEquals( "[-1]", Iterators.toString( range.iterator() ) );
}
@Test
public void testMAX_VALUE() {
int start = Integer.MAX_VALUE - 1;
IntRange range = new IntRange( start, Integer.MAX_VALUE );
assertFalse( range.contains( Integer.MAX_VALUE - 2 ) );
assertTrue( range.contains( start ) );
assertFalse( range.contains( Integer.MAX_VALUE ) );
assertEquals( "[" + start + "]", Iterators.toString( range.iterator() ) );
}
@Test
public void testMAX_VALUE_2() {
int start = Integer.MAX_VALUE;
IntRange range = new IntRange( start, Integer.MAX_VALUE );
assertFalse( range.contains( Integer.MAX_VALUE - 2 ) );
assertFalse( range.contains( start ) );
assertFalse( range.contains( Integer.MAX_VALUE ) );
assertEquals( "[]", Iterators.toString( range.iterator() ) );
}
@Test
public void testMIN_VALUE() {
int start = Integer.MIN_VALUE + 1;
IntRange range = new IntRange( start, Integer.MIN_VALUE );
assertFalse( range.contains( Integer.MIN_VALUE - 2 ) );
assertTrue( range.contains( start ) );
assertFalse( range.contains( Integer.MIN_VALUE ) );
assertEquals( "[" + start + "]", Iterators.toString( range.iterator() ) );
}
@Test
public void testMIN_VALUE_2() {
int start = Integer.MIN_VALUE;
IntRange range = new IntRange( start, Integer.MIN_VALUE );
assertFalse( range.contains( Integer.MIN_VALUE - 2 ) );
assertFalse( range.contains( start ) );
assertFalse( range.contains( Integer.MIN_VALUE ) );
assertEquals( "[]", Iterators.toString( range.iterator() ) );
}
}
import static IntRange.*;
for( i : range(list.size) ) { ... }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment