[LANG-1569] Add ArrayUtils.get(T[], index, T) to provide an

out-of-bounds default value
This commit is contained in:
Gary Gregory 2020-06-12 16:40:49 -04:00
parent 495167a392
commit 5b699d0765
3 changed files with 77 additions and 30 deletions

View File

@ -61,6 +61,7 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="LANG-1545" type="update" dev="ggregory" due-to="XenoAmess, Gary Gregory">CharSequenceUtils.regionMatches is wrong dealing with Georgian.</action>
<action type="add" dev="jochen">Added the Locks class as a convenient possibility to deal with locked objects.</action>
<action issue="LANG-1568" type="add" dev="ggregory">Add to Functions: FailableBooleanSupplier, FailableIntSupplier, FailableLongSupplier, FailableDoubleSupplier, and so on.</action>
<action issue="LANG-1569" type="add" dev="ggregory">Add ArrayUtils.get(T[], index, T) to provide an out-of-bounds default value.</action>
</release>
<release version="3.10" date="2020-03-22" description="New features and bug fixes. Requires Java 8, supports Java 9, 10, 11.">

View File

@ -1655,6 +1655,39 @@ private static Object copyArrayGrow1(final Object array, final Class<?> newArray
return Array.newInstance(newArrayComponentType, 1);
}
/**
* Gets the nTh element of an array or null if the index is out of bounds or the array is null.
*
* @param <T> The type of array elements.
* @param array The array to index.
* @param index The index
* @return the nTh element of an array or null if the index is out of bounds or the array is null.
* @since 3.11
*/
public static <T> T get(final T[] array, final int index) {
return get(array, index, null);
}
/**
* Gets the nTh element of an array or a default value if the index is out of bounds.
*
* @param <T> The type of array elements.
* @param array The array to index.
* @param index The index
* @param defaultValue The return value of the given index is out of bounds.
* @return the nTh element of an array or a default value if the index is out of bounds.
* @since 3.11
*/
public static <T> T get(final T[] array, final int index, T defaultValue) {
if (array == null) {
return defaultValue;
}
if (index >= 0 && index < array.length) {
return array[index];
}
return defaultValue;
}
//-----------------------------------------------------------------------
/**
* <p>Returns the length of the specified array.

View File

@ -47,16 +47,15 @@ public class ArrayUtilsTest {
private class TestClass {
}
/** A predefined seed used to initialize {@link Random} in order to get predictable results */
private static final long SEED = 16111981L;
@SafeVarargs
private static <T> T[] toArrayPropagatingType(final T... items) {
return ArrayUtils.toArray(items);
}
//-----------------------------------------------------------------------
private void assertIsEquals(final Object array1, final Object array2, final Object array3) {
assertTrue(ArrayUtils.isEquals(array1, array1));
assertTrue(ArrayUtils.isEquals(array2, array2));
@ -69,7 +68,6 @@ private void assertIsEquals(final Object array1, final Object array2, final Obje
assertFalse(ArrayUtils.isEquals(array2, array1));
}
//-----------------------------------------------------------------------
/**
* Tests generic array creation with parameters of same type.
*/
@ -101,7 +99,6 @@ public void testArrayCreationWithGeneralReturnType() {
assertTrue(obj instanceof String[]);
}
//-----------------------------------------------------------------------
@Test
public void testClone() {
assertArrayEquals(null, ArrayUtils.clone((Object[]) null));
@ -192,7 +189,6 @@ public void testCloneShort() {
assertNotSame(original, cloned);
}
//-----------------------------------------------------------------------
@Test
public void testConstructor() {
assertNotNull(new ArrayUtils());
@ -281,8 +277,6 @@ public void testContainsDouble() {
assertFalse(ArrayUtils.contains(array, (double) 99));
}
//-----------------------------------------------------------------------
@SuppressWarnings("cast")
@Test
public void testContainsDoubleTolerance() {
@ -363,7 +357,47 @@ public void testEmptyArrayCreation() {
assertEquals(0, array.length);
}
// ------------------------------------------------------------------------
@Test
public void testGet() {
assertNull(ArrayUtils.get(null, -1));
assertNull(ArrayUtils.get(null, 0));
assertNull(ArrayUtils.get(null, 1));
final String[] array0 = {};
assertNull(ArrayUtils.get(array0, -1));
assertNull(ArrayUtils.get(array0, 0));
assertNull(ArrayUtils.get(array0, 1));
final String[] array1 = { StringUtils.EMPTY };
assertEquals(null, ArrayUtils.get(array1, -1));
assertEquals(StringUtils.EMPTY, ArrayUtils.get(array1, 0));
assertEquals(null, ArrayUtils.get(array1, 1));
}
@Test
public void testGetDefault() {
// null default
{
assertNull(ArrayUtils.get(null, -1, null));
assertNull(ArrayUtils.get(null, 0, null));
assertNull(ArrayUtils.get(null, 1, null));
final String[] array0 = {};
assertNull(ArrayUtils.get(array0, -1, null));
assertNull(ArrayUtils.get(array0, 0, null));
assertNull(ArrayUtils.get(array0, 1, null));
final String[] array1 = { StringUtils.EMPTY };
assertEquals(null, ArrayUtils.get(array1, -1, null));
assertEquals(StringUtils.EMPTY, ArrayUtils.get(array1, 0, null));
assertEquals(null, ArrayUtils.get(array1, 1, null));
}
// non-null default
{
final String defaultValue = "defaultValue";
final String[] array1 = { StringUtils.EMPTY };
assertEquals(defaultValue, ArrayUtils.get(array1, -1, defaultValue));
assertEquals(StringUtils.EMPTY, ArrayUtils.get(array1, 0, defaultValue));
assertEquals(defaultValue, ArrayUtils.get(array1, 1, defaultValue));
}
}
@Test
public void testGetLength() {
assertEquals(0, ArrayUtils.getLength(null));
@ -419,7 +453,6 @@ public void testGetLength() {
assertThrows(IllegalArgumentException.class, () -> ArrayUtils.getLength("notAnArray"));
}
//-----------------------------------------------------------------------
@Test
public void testHashCode() {
final long[][] array1 = new long[][]{{2, 5}, {4, 5}};
@ -885,7 +918,6 @@ public void testIndexesOfWithStartIndex() {
}
//-----------------------------------------------------------------------
@Test
public void testIndexOf() {
final Object[] array = new Object[]{"0", "1", "2", "3", null, "0"};
@ -900,7 +932,6 @@ public void testIndexOf() {
assertEquals(-1, ArrayUtils.indexOf(array, "notInArray"));
}
//-----------------------------------------------------------------------
@Test
public void testIndexOfBoolean() {
boolean[] array = null;
@ -930,7 +961,6 @@ public void testIndexOfBooleanWithStartIndex() {
assertEquals(-1, ArrayUtils.indexOf(array, false, -1));
}
//-----------------------------------------------------------------------
@Test
public void testIndexOfByte() {
byte[] array = null;
@ -957,7 +987,6 @@ public void testIndexOfByteWithStartIndex() {
assertEquals(-1, ArrayUtils.indexOf(array, (byte) 0, 6));
}
//-----------------------------------------------------------------------
@Test
public void testIndexOfChar() {
char[] array = null;
@ -984,7 +1013,6 @@ public void testIndexOfCharWithStartIndex() {
assertEquals(-1, ArrayUtils.indexOf(array, 'a', 6));
}
//-----------------------------------------------------------------------
@SuppressWarnings("cast")
@Test
public void testIndexOfDouble() {
@ -1049,7 +1077,6 @@ public void testIndexOfDoubleWithStartIndexTolerance() {
assertEquals(1, ArrayUtils.indexOf(array, 1.00001324, -300, 0.0001));
}
//-----------------------------------------------------------------------
@SuppressWarnings("cast")
@Test
public void testIndexOfFloat() {
@ -1082,7 +1109,6 @@ public void testIndexOfFloatWithStartIndex() {
assertEquals(-1, ArrayUtils.indexOf(array, (float) 0, 6));
}
//-----------------------------------------------------------------------
@Test
public void testIndexOfInt() {
int[] array = null;
@ -1109,7 +1135,6 @@ public void testIndexOfIntWithStartIndex() {
assertEquals(-1, ArrayUtils.indexOf(array, 0, 6));
}
//-----------------------------------------------------------------------
@Test
public void testIndexOfLong() {
long[] array = null;
@ -1136,7 +1161,6 @@ public void testIndexOfLongWithStartIndex() {
assertEquals(-1, ArrayUtils.indexOf(array, 0, 6));
}
//-----------------------------------------------------------------------
@Test
public void testIndexOfShort() {
short[] array = null;
@ -1505,8 +1529,6 @@ public void testIsSortedDouble() {
assertFalse(ArrayUtils.isSorted(array));
}
//-----------------------------------------------------------------------
@Test
public void testIsSortedFloat() {
float[] array = null;
@ -2249,7 +2271,6 @@ public void testNullToEmptyStringNull() {
assertArrayEquals(ArrayUtils.EMPTY_STRING_ARRAY, ArrayUtils.nullToEmpty((String[]) null));
}
//-----------------------------------------------------------------------
@Test
public void testReverse() {
final StringBuffer str1 = new StringBuffer("pick");
@ -2667,7 +2688,6 @@ public void testReverseShortRange() {
assertNull(array);
}
//-----------------------------------------------------------------------
@Test
public void testSameLength() {
final Object[] nullArray = null;
@ -4256,7 +4276,6 @@ public void testSameLengthShort() {
assertTrue(ArrayUtils.isSameLength(twoArray, twoArray));
}
//-----------------------------------------------------------------------
@Test
public void testSameType() {
assertThrows(IllegalArgumentException.class, () -> ArrayUtils.isSameType(null, null));
@ -4469,7 +4488,6 @@ public void testShiftChar() {
assertEquals(2, array[3]);
}
//-----------------------------------------------------------------------
@Test
public void testShiftDouble() {
final double[] array = new double[]{1, 2, 3, 4};
@ -5545,7 +5563,6 @@ public void testSwapByteRange() {
assertEquals(3, array[2]);
}
//-----------------------------------------------------------------------
@Test
public void testSwapChar() {
char[] array = new char[]{1, 2, 3};
@ -6034,7 +6051,6 @@ public void testSwapShortRange() {
assertEquals(3, array[2]);
}
//-----------------------------------------------------------------------
@Test
public void testToMap() {
Map<?, ?> map = ArrayUtils.toMap(new String[][]{{"foo", "bar"}, {"hello", "world"}});
@ -6090,8 +6106,6 @@ public Object setValue(final Object value) {
assertEquals(Collections.singletonMap("key", "value1"), map);
}
//-----------------------------------------------------------------------
@Test
public void testToObject_boolean() {
final boolean[] b = null;
@ -6447,7 +6461,6 @@ public void testToPrimitive_short_short() {
Short.valueOf((short) 9999999)}, Short.MAX_VALUE));
}
//-----------------------------------------------------------------------
@Test
public void testToString() {
assertEquals("{}", ArrayUtils.toString(null));