LANG-1402: added get methods to ArrayUtils
This commit is contained in:
parent
c241b096d3
commit
9379d0d36a
|
@ -8672,4 +8672,37 @@ public static void shuffle(final double[] array, final Random random) {
|
||||||
swap(array, i - 1, random.nextInt(i), 1);
|
swap(array, i - 1, random.nextInt(i), 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets an element from the array if the array is non-null and appropriately long, otherwise returns null
|
||||||
|
*
|
||||||
|
* @param array the array holding the desired object
|
||||||
|
* @param index the index of the object in the array
|
||||||
|
* @return The Object in the array at the index, or null if it is ill-formatted
|
||||||
|
* @since 3.8
|
||||||
|
*/
|
||||||
|
public static Object get(Object[] array, int index){
|
||||||
|
return get(array, index, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets an element from the array if the array is non-null and appropriately long, otherwise returns the specified value
|
||||||
|
*
|
||||||
|
* @param array the array holding the desired object
|
||||||
|
* @param index the index of the object in the array
|
||||||
|
* @param defaultReturn the object to be returned if the array is null or shorter than the index
|
||||||
|
* @return The object in the array at the specified index, or the given Object if it is ill-formatted
|
||||||
|
* @since 3.8
|
||||||
|
*/
|
||||||
|
public static Object get(Object[] array, int index, Object defaultReturn){
|
||||||
|
if(getLength(array) == 0 || array.length <= index){
|
||||||
|
return defaultReturn;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(index < 0 ){
|
||||||
|
index = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return array[index];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5111,4 +5111,20 @@ public void testShuffleDouble() {
|
||||||
assertTrue("Element " + element + " not found", ArrayUtils.contains(array1, element));
|
assertTrue("Element " + element + " not found", ArrayUtils.contains(array1, element));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGet(){
|
||||||
|
assertNull(ArrayUtils.get(null, 0));
|
||||||
|
String[] array = new String[1];
|
||||||
|
assertNull(ArrayUtils.get(array, 1));
|
||||||
|
array[0] = "Hello World";
|
||||||
|
//test with happy path
|
||||||
|
assertNotNull(ArrayUtils.get(array, 0));
|
||||||
|
|
||||||
|
//test with default getter
|
||||||
|
assertEquals("Test", ArrayUtils.get(array, 10, "Test"));
|
||||||
|
|
||||||
|
//negative index
|
||||||
|
assertEquals("Hello World", ArrayUtils.get(array, -1));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue