/** * Create an {@link IntList} that contains all numbers in a specified interval. * * @param from The first number (inclusive) * @param to the last number (exclusive). * @return A list containing the integers in the interval {@code [from,to)}. */ public static IntList interval(int from, int to) { Preconditions.checkArgument(to >= from, "last integer less than first"); return new IntIntervalList(from, to); } }
@Override public int getInt(int index) { checkIndex(index, startIndex, endIndex); return startIndex + index; }
/** * Create an {@link IntList} that contains all numbers in a specified interval. * * @param from The first number (inclusive) * @param to the last number (exclusive). * @return A list containing the integers in the interval {@code [from,to)}. */ public static IntList interval(int from, int to) { Preconditions.checkArgument(to >= from, "last integer less than first"); return new IntIntervalList(from, to); } }
@Override public IntListIterator listIterator(int idx) { checkIndex(idx, startIndex, endIndex + 1); // this index can be one past the end return IntIterators.fromTo(startIndex + idx, endIndex); } }
@Test public void testEmptyRange() { IntList list = new IntIntervalList(5, 5); assertTrue(list.isEmpty()); assertEquals(0, list.size()); assertFalse(list.iterator().hasNext()); }
@Override public int getInt(int index) { checkIndex(index, startIndex, endIndex); return startIndex + index; }
@Test public void testEmptyList() { IntList list = new IntIntervalList(0); assertTrue(list.isEmpty()); assertEquals(0, list.size()); assertFalse(list.iterator().hasNext()); }
@Override public IntListIterator listIterator(int idx) { checkIndex(idx, startIndex, endIndex + 1); // this index can be one past the end return IntIterators.fromTo(startIndex + idx, endIndex); } }
@Test public void testBroaderInterval() { IntList list = new IntIntervalList(5); assertFalse(list.isEmpty()); assertEquals(5, list.size()); for (int i = 0; i < 5; i++) { assertEquals(i, list.getInt(i)); } try { list.getInt(5); fail("getInt(5) should throw"); } catch (IndexOutOfBoundsException e) { /* no-op */ } } }
@Test public void testSimpleListAccess() { IntList list = new IntIntervalList(1); assertFalse(list.isEmpty()); assertEquals(1, list.size()); assertEquals(0, list.getInt(0)); try { list.getInt(1); fail("getInt(1) should throw"); } catch (IndexOutOfBoundsException e) { /* no-op */ } IntListIterator iter = list.iterator(); assertTrue(iter.hasNext()); assertFalse(iter.hasPrevious()); assertEquals(0, iter.nextInt()); assertFalse(iter.hasNext()); assertTrue(iter.hasPrevious()); assertEquals(0, iter.previousInt()); }
@Test public void testSimpleIntervalAccess() { IntList list = new IntIntervalList(42, 43); assertFalse(list.isEmpty()); assertEquals(1, list.size()); assertEquals(42, list.getInt(0)); try { list.getInt(1); fail("getInt(1) should throw"); } catch (IndexOutOfBoundsException e) { /* no-op */ } IntListIterator iter = list.iterator(); assertTrue(iter.hasNext()); assertFalse(iter.hasPrevious()); assertEquals(42, iter.nextInt()); assertFalse(iter.hasNext()); assertTrue(iter.hasPrevious()); assertEquals(42, iter.previousInt()); }