[
https://issues.apache.org/jira/browse/LANG-1402?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16536453#comment-16536453
]
ASF GitHub Bot commented on LANG-1402:
--------------------------------------
Github user MarkDacek commented on a diff in the pull request:
https://github.com/apache/commons-lang/pull/336#discussion_r200858239
--- Diff: src/main/java/org/apache/commons/lang3/ArrayUtils.java ---
@@ -8672,4 +8672,53 @@ public static void shuffle(final double[] array,
final Random random) {
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 <T> the component type of the array
+ * @param array the array holding the desired element
+ * @param index the index of the element in the array
+ * @return The element in the array at the index, or null if it is
ill-formatted
+ * @since 3.8
+ */
+ public static <T> T get(T[] 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 <T> the component type of the array
+ * @param array the array holding the desired element
+ * @param index the index of the element in the array
+ * @param defaultReturn the object to be returned if the array is null
or shorter than the index
+ * @return The element in the array at the specified index, or the
given argument if it is ill-formatted
--- End diff --
The usefulness of these would be to reduce some clutter in everyday
development. Checking for both null and then length can be a tedious situation
for developers striving to get high unit-test coverage.
isArrayIndexValid would reduce the number of branches in a typical use
case, whereas get is probably a bit simpler. I'm can't say this with any degree
of certainty, but a correctly-placed null element probably requires yet another
check after the fact. Something like:
if(isArrayIndexValid(array, 0) && array[0] != null)
> ArrayUtils should have null and index-safe get methods.
> -------------------------------------------------------
>
> Key: LANG-1402
> URL: https://issues.apache.org/jira/browse/LANG-1402
> Project: Commons Lang
> Issue Type: New Feature
> Components: General
> Reporter: Mark Dacek
> Priority: Minor
> Original Estimate: 24h
> Remaining Estimate: 24h
>
> There should be a safe way to retrieve a value from array without having to
> check for null and length. It would be a very simple implementation but could
> save developers a great deal of time in writing and testing.
>
> Something like
>
> {code:java}
> String[] a = null;
> ArrayUtils.get(a, 5); //returns null
> a = new String[5];
> ArrayUtils.get(a, 10); // returns null
>
> a[0] = "Hello World";
> ArrayUtils.get(a, 0); // returns "Hello World"
> {code}
>
> We could handle a few other cases - a default return value. The
> tricky/annoying thing is the need to cast everything in order to make this
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)