Guava precondition checks, empty List
Guava precondition checks, empty List
I would like to know what is the best pattern for precondition checks on a list from which I will need to pick the first item.
In words, I suppose the list should not be null
and its size should be > 1.
I find Guava's checkPositionIndex not helpful in this respect. On the contrary, I find it counter-intuitive, see the example below which bombs on an empty list because I use checkPositionIndex rather than checkArgument, as outlined after the guard which does not trigger.
It seems like checking position 0 is not sufficient to validate the argument even if I .get(0) from it?
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkPositionIndex; import java.util.List; import com.google.common.collect.Lists; public class HowShouldIUseCheckPositionIndex { private static class ThingAMajig { private String description; private ThingAMajig(String description) { this.description = description; } @Override public String toString() { return description; } } private static void goByFirstItemOfTheseAMajigs(List things) { checkNotNull(things); // Check whether getting the first item is fine checkPositionIndex(0, things.size()); // Looks intuitive but... System.out.println(things.get(0)); // Finally, help the economy! checkArgument(things.size() > 0); // This would have worked :( } public static void main(String[] args) { List fullList = Lists.newArrayList(new ThingAMajig( "that thingy for the furnace I have been holding off buying")); List emptyList = Lists.newLinkedList(); goByFirstItemOfTheseAMajigs(fullList); // goByFirstItemOfTheseAMajigs(emptyList); // This *bombs* } }
Answer by axtavt for Guava precondition checks, empty List
You should use checkElementIndex()
instead.
checkPositionIndex()
ensures that the given position is a valid position to insert new element to (i.e. you can do add(0, obj)
on empty list), not a valid index to get an element from.
Answer by John B for Guava precondition checks, empty List
It states:
Ensures that index specifies a valid element in an array, list or string of size size. An element index may range from zero, inclusive, to size, exclusive
Answer by Asaf for Guava precondition checks, empty List
You can use the twitter-commons implementation of the checkNotBlank method. it validates that an Iterable is not null and not empty. here is the implementation:
/** * Checks that an Iterable is both non-null and non-empty. This method does not check individual * elements in the Iterable, it just checks that the Iterable has at least one element. * * @param argument the argument to validate * @param message the message template for validation exception messages where %s serves as the * sole argument placeholder * @param args any arguments needed by the message template * @return the argument if it is valid * @throws NullPointerException if the argument is null * @throws IllegalArgumentException if the argument has no iterable elements */ public static > T checkNotBlank(T argument, String message, Object... args) { Preconditions.checkNotNull(argument, message, args); Preconditions.checkArgument(!Iterables.isEmpty(argument), message, args); return argument; }
Very simple and works on all iterables.
Answer by Louis Wasserman for Guava precondition checks, empty List
You don't need to do the check at all, in point of fact.
The list.get(0)
call itself will already throw basically exactly the same error message.
But if you did want to do the check explicitly, yes, use checkElementIndex
or checkArgument(!list.isEmpty())
.
Answer by keyoxy for Guava precondition checks, empty List
You can use valid4j with hamcrest-matchers instead (found on Maven Central as org.valid4j:valid4j)
For preconditions and postconditions (basically assertions -> throwing AssertionError):
import static org.valid4j.Assertive.*; require(list, hasSize(greaterThan(0)));
Or for input validation (throwing your custom recoverable exception):
import static org.valid4j.Validation.*; validate(list, hasSize(greaterThan(0)), otherwiseThrowing(EmptyListException.class));
Links:
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment