New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Iterables.isOnlyElement #957
Comments
Original comment posted by wasserman.louis on 2012-04-03 at 03:07 PM I'm not sure I follow. Are you asking for a boolean check to ask if an Iterable has exactly one element? |
Original comment posted by kevinb@google.com on 2012-04-03 at 05:49 PM I'm not currently understanding why a special library method to tell you that an iterable is of size one would be that broadly useful. Can you give us examples of where this method has been useful to you? Status: |
Original comment posted by Petro.Semeniuk on 2012-04-04 at 03:35 AM @louis, yes, that's what I after. Sorry about confusing description - I'm not a native english speaker and not a native english writer neither :-). |
Original comment posted by Petro.Semeniuk on 2012-04-04 at 03:40 AM @kevin, I understand that 'no elements' case in Guava is covered by Iterable.getOnlyElement(Iterable<? extends T> iterable, T defaultValue) and it's possible to do "soft" check - having default value as Optional.absent() and use if/else afterwards. For me having "soft"(if/else, without having to deal with IllegalArgumentException) check for case where there are more than one element would be useful as well. |
Original comment posted by wasserman.louis on 2012-04-04 at 04:31 AM I'm simply not convinced that represents a win over Iterables.size(elements) == 1, in terms of readability, correctness, or speed. |
Original comment posted by Petro.Semeniuk on 2012-04-04 at 06:13 AM @louis, it's just a suggestion. Its ok if having this method doesn't fit into Guava's direction. Library is great anyway and what I love about it is that it has a rich API and at the same time is not overwhelmed by classes/methods. At stackoverflow top rated answer(http://stackoverflow.com/questions/9988179/check-if-only-one-element-exist-using-guava) is to compare to 1 (as you suggested as well) Regarding speed, correctness - there is no difference. As for readability - well... I prefer have specific method instead of "== 1", but it's a matter of personal taste. |
Original comment posted by cpovirk@google.com on 2012-04-04 at 03:32 PM Thanks for the clarifications and for the pointer to "grep -r '== 1' ." and its surprisingly many results. I see at least 5 possible users there. Nevertheless, we'd want even more for a method that exists just for readability. Status: |
Original comment posted by cgdecker on 2012-04-04 at 03:48 PM There is an advantage over size() == 1 (which, incidentally, I think is as good as you can get in terms of readability): a method to check if the size is 1 wouldn't have to scan the whole Iterable to determine the size, it would just need to check that there's a first element and not a second. That's not to say anything about how common a need for such a method there might be. |
Original issue created by Petro.Semeniuk on 2012-04-03 at 07:44 AM
Would it be possible to add Iterables.isOnlyElement to guava library? I'm asking because currently the way how we handle that is to use home-brew collections class with next methods:
class Collections {
static <T> boolean isSingleValue(Collection<T> values) {
return values.size() == 1;
}
}
and do special case in case if only single element present in collection:
class Demo {
}
Iterables.getOnlyElement matches Collections.getSingleValue, but I can't find match for Collections.isSingleValue.
Let me know your thoughts on this.
The text was updated successfully, but these errors were encountered: