- Return empty list iterator
- Straight up Java
- Google Guava
- Apache Commons
- Exception to the rule
- Just make the null and empty check
- Use Guava Objects.firstNonNull
- Apache commons ColletionUtils
- Return empty iterator
- Straight up Java
- Google Guava
- Apache Commons
- Exception to the rule
- Just make the null and empty check
- Use Guava Objects.firstNonNull
- Apache commons ColletionUtils
Return empty list iterator
Effective java Item 43 states return empty arrays or collections, not nulls. In practice you should return the same immutable empty collection every time you return a collection. There are a few ways to handle the exception to the rule when you encounter methods that should return a collection but instead return null. Check out return empty list, return empty map, return empty set, return empty enumeration, return empty sorted set, return empty sorted map and return empty iterator when having to deal with other collection types.
Straight up Java
@Test public void return_empty_list_iterator_java () ListIteratorString> strings = Collections.emptyListIterator(); assertFalse(strings.hasNext()); >
Google Guava
@Test public void return_empty_list_iterator_guava () ListIteratorString> strings = ImmutableList.String>of().listIterator(); assertFalse(strings.hasNext()); >
Apache Commons
@Test public void return_empty_list_iterator_apache () ListIteratorString> strings = IteratorUtils.emptyListIterator(); assertFalse(strings.hasNext()); >
Exception to the rule
The goal is to handle the null to empty collection early in the chain. If you are coding the method that returns a list iterator, there is no reason it should return null. When dealing with legacy code you have to deal with a null so here are a few options when that occurs:
Just make the null and empty check
private void return_empty_list_iterator_java_exception () DomainObject domain = null; // dao populate domain ListIteratorString> strings; if (domain != null && domain.getStrings() != null && domain.getStrings().hasNext()) strings = domain.getStrings(); > else strings = Collections.emptyListIterator(); > //. >
Use Guava Objects.firstNonNull
private void return_empty_list_iterator_guava_exception () DomainObject domain = null; // dao populate domain ListIteratorString> strings = Objects.firstNonNull( domain != null ? domain.getStrings() : null, ImmutableList.String>of().listIterator()); //. >
Apache commons ColletionUtils
private void return_empty_list_iterator_apache_commons_exception () DomainObject domain = null; // dao populate domain ListIteratorString> strings; if (domain != null && !CollectionUtils.sizeIsEmpty(domain.getStrings())) strings = domain.getStrings(); > else strings = EmptyListIterator.INSTANCE; > //. >
Return empty list iterator posted by Justin Musgrove on 20 January 2014
Tagged: java and java-collections
Return empty iterator
Effective java Item 43 states return empty arrays or collections, not nulls. In practice you should return the same immutable empty collection every time you return a collection. There are a few ways to handle the exception to the rule when you encounter methods that should return a collection but instead return null. Check out return empty list, return empty map, return empty set, return empty enumeration,return empty sorted set, return empty sorted map and return empty list iterator when having to deal with other collection types.
Straight up Java
@Test public void return_empty_list_iterator_java () IteratorString> strings = Collections.emptyIterator(); assertFalse(strings.hasNext()); >
Google Guava
@Test public void return_empty_list_iterator_guava () IteratorString> strings = Iterators.emptyIterator(); assertFalse(strings.hasNext()); >
Apache Commons
@Test public void return_empty_iterator_java () @SuppressWarnings("unchecked") IteratorString> strings = IteratorUtils.emptyIterator(); assertFalse(strings.hasNext()); >
Exception to the rule
The goal is to handle the null to empty collection early in the chain. If you are coding the method that returns an iterator, there is no reason it should return null. When dealing with legacy code you have to deal with a null so here are a few options when that occurs:
Just make the null and empty check
private void return_empty_iterator_java_exception () DomainObject domain = null; // dao populate domain IteratorString> strings; if (domain != null && domain.getStrings() != null && domain.getStrings().hasNext()) strings = domain.getStrings(); > else strings = Collections.emptyIterator(); > //. >
Use Guava Objects.firstNonNull
private void return_empty_iterator_guava_exception () DomainObject domain = null; // dao populate domain IteratorString> strings = Objects.firstNonNull( domain != null ? domain.getStrings() : null, Iterators.String>emptyIterator()); //. >
Apache commons ColletionUtils
private void return_empty_iterator_apache_commons_exception () DomainObject domain = null; // dao populate domain IteratorString> strings; if (domain != null && !CollectionUtils.sizeIsEmpty(domain.getStrings())) strings = domain.getStrings(); > else strings = EmptyIterator.INSTANCE; > //. >
Return empty iterator posted by Justin Musgrove on 24 January 2014
Tagged: java and java-collections