/** * Return a list that repeats a single object multiple times. * * @param obj The object. * @param n The size of the list. * @param <T> The type of list elements. * @return A list containing <var>obj</var> <var>n</var> times. */ public static <T> List<T> repeat(T obj, int n) { return new RepeatedList<>(obj, n); }
@Test public void testSingle() { RepeatedList<Integer> rl = new RepeatedList<>(7, 1); assertThat(rl.size(), equalTo(1)); assertThat(rl.get(0).intValue(), equalTo(7)); try { rl.get(1); fail("Should throw an exception!"); } catch(IndexOutOfBoundsException e) { /* expected */ } }
@Test public void testEmpty() { RepeatedList<Integer> rl = new RepeatedList<>(7, 0); assertThat(rl.size(), equalTo(0)); try { rl.get(-1); fail("Should throw an exception!"); } catch(IndexOutOfBoundsException e) { /* expected */ } try { rl.get(0); fail("Should throw an exception!"); } catch(IndexOutOfBoundsException e) { /* expected */ } try { rl.get(1); fail("Should throw an exception!"); } catch(IndexOutOfBoundsException e) { /* expected */ } }
/** * Return a list that repeats a single object multiple times. * * @param obj The object. * @param n The size of the list. * @param <T> The type of list elements. * @return A list containing <var>obj</var> <var>n</var> times. */ public static <T> List<T> repeat(T obj, int n) { return new RepeatedList<>(obj, n); }