ArrayUtils { public static <T> T getElementAtIndex(T[] array, int index, T defaultValue) { try { return array[index]; } catch (ArrayIndexOutOfBoundsException ignore) { return defaultValue; } }  static T getElementAtIndex(T[] array, int index, T defaultValue); static T getFirst(final T... array); static String toString(final Object... array); static String toString(final String... array); static Object[] insert(Object[] originalArray, int pos, Object element); static Object[] remove(Object[] originalArray, int pos); static String objectRefString(Object obj); static void objectRefString(Object obj, StringBuilder sb); static String objectString(Object obj); static void objectString(Object obj, StringBuilder sb); static String objectStringNonRecursive(@Unretained Object obj); static boolean areByteArrayArrayEquals(byte[][] v1, byte[][] v2); static void objectStringNonRecursive(@Unretained Object obj, StringBuilder sb); static boolean objectEquals(Object o1, Object o2); static Integer[] toIntegerArray(final int[] array); static Byte[][] toByteArray(final byte[][] array); static byte[][] toBytes(final Byte[][] byteArray); static List<T> asList(T... array);  }
@Test public void testGetElementAtIndex() { Object[] arrayOfThree = new Object[] {"test", "testing", "tested"}; assertThat(getElementAtIndex(arrayOfThree, 0, null)).isEqualTo("test"); assertThat(getElementAtIndex(arrayOfThree, 1, null)).isEqualTo("testing"); assertThat(getElementAtIndex(arrayOfThree, 2, null)).isEqualTo("tested"); }
@Test public void getElementAtIndex_emptyArray_returnsDefaultValue() { Object[] emptyArray = new Object[] {}; String defaultValue = this.testName.getMethodName(); assertThat(getElementAtIndex(emptyArray, 0, defaultValue)).isEqualTo(defaultValue); }
@Test public void getElementAtIndex_emptyArray_returnsNullDefaultValue() { Object[] emptyArray = new Object[] {}; assertThat(getElementAtIndex(emptyArray, 0, null)).isNull(); }
@Test public void getElementAtIndex_indexOutOfBounds_returnsDefaultValue() { Object[] arrayOfOne = new Object[] {"test"}; String defaultValue = this.testName.getMethodName(); assertThat(getElementAtIndex(arrayOfOne, 2, defaultValue)).isEqualTo(defaultValue); }
@Test public void getElementAtIndex_empty_indexOutOfBounds_returnsDefaultValue() { Object[] emptyArray = new Object[] {}; String defaultValue = this.testName.getMethodName(); assertThat(getElementAtIndex(emptyArray, 2, defaultValue)).isEqualTo(defaultValue); }