Skip to content
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

Closed
gissuebot opened this issue Oct 31, 2014 · 8 comments
Closed

Add Iterables.isOnlyElement #957

gissuebot opened this issue Oct 31, 2014 · 8 comments

Comments

@gissuebot
Copy link

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;
    }

static <T> T getSingleValue(Collection<T> values) {
    Assert.isTrue(isSingleValue(values));
    return values.iterator().next();
}

}

and do special case in case if only single element present in collection:
class Demo {

static demo() {
    final List list = Lists.newArrayList(new Object());

    if (Collections.isSingleValue(list)) {
        final Object object = Collections.getSingleValue(list);
        //... do something with object
    }

}

}

Iterables.getOnlyElement matches Collections.getSingleValue, but I can't find match for Collections.isSingleValue.

Let me know your thoughts on this.

@gissuebot
Copy link
Author

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?

@gissuebot
Copy link
Author

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: Acknowledged
Labels: Type-Enhancement, Package-Collect

@gissuebot
Copy link
Author

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 :-).

@gissuebot
Copy link
Author

Original comment posted by Petro.Semeniuk on 2012-04-04 at 03:40 AM


@kevin,
In my case I need it when creating web page on server side(we use freemarker). If there are multiple options for some field we're displaying radios/combos for user, in case of single value in collection of options we're displaying simple label with auto-selected attribute.
Btw, I did "grep -r '== 1' ." on guava codebase and there are roughly ten occurrences where collections length compared to 1. It might be used inside guava as well.

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.

@gissuebot
Copy link
Author

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.

@gissuebot
Copy link
Author

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.

@gissuebot
Copy link
Author

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: WontFix

@gissuebot
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant