Skip to content

LANG-1402: Null/index safe get methods for ArrayUtils#336

Merged
asfgit merged 8 commits into
apache:masterfrom
MarkDacek:LANG-1402
Aug 9, 2018
Merged

LANG-1402: Null/index safe get methods for ArrayUtils#336
asfgit merged 8 commits into
apache:masterfrom
MarkDacek:LANG-1402

Conversation

@MarkDacek
Copy link
Copy Markdown
Contributor

@chtompki
If this is deemed worthy or needs improvement - let me know.

@coveralls
Copy link
Copy Markdown

coveralls commented Jul 7, 2018

Coverage Status

Coverage decreased (-0.0003%) to 95.243% when pulling 3dbc944 on MarkDacek:LANG-1402 into c241b09 on apache:master.

@sebbASF
Copy link
Copy Markdown
Contributor

sebbASF commented Jul 7, 2018

User code has to cast the return to make use of it; that is not ideal.

@MarkDacek
Copy link
Copy Markdown
Contributor Author

I added your suggested method and refactored the other to use generics.

}

if(index < 0 ){
index = 0;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely a negative index should return the default?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

}

/**
* Gets an element from the array if the array is non-null and appropriately long, otherwise returns the specified value
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not agree with code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, mistaken copy-paste there. Feel free to use this as a tutorial for how NOT to contribute.

/**
* 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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be 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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does ill-formatted mean?
Can also return null if the array element is present and null.

I don't think this method is sufficiently useful to be worth adding

* @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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ill-formatted?

Again, I don't see the point of this method.
It's a bit more flexible than the original method.
But it's still only useful for the case where you can choose a default value that does not appear in the array, and you still have to check whether the return value is the default or not.

Copy link
Copy Markdown
Contributor Author

@MarkDacek MarkDacek Jul 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thought process was something similar to this https://github.com/apache/commons-lang/blob/master/src/main/java/org/apache/commons/lang3/StringUtils.java#L7456-L7458

I would understand if Strings are understood well-enough to merit a true default value and generic arrays aren't, so as to justify removing the two-argument edition.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@MarkDacek
Copy link
Copy Markdown
Contributor Author

@sebbASF Thanks for your patience and efforts to make this a worthy contribution. I have removed the other methods per your suggestion.

Copy link
Copy Markdown
Member

@chtompki chtompki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally this seems reasonable to me. @sebbASF - do you have any more points you want to make?

@asfgit asfgit merged commit 3dbc944 into apache:master Aug 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants