I would like to know what is the best prerequisite checklist in the list from which I will need to select the first item.
In words, I believe that the list should not be null, and its size should be> 1.
I believe Guava checkPositionIndex does not help in this regard. On the contrary, I find this counter-intuitive, see the Example below, which is bombarded with an empty list, because I use checkPositionIndex and not checkArgument, as indicated after the defender does not start.
It seems that checking position 0 is not enough to check the argument, even if Iget (0) is 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<ThingAMajig> things) {
checkNotNull(things);
checkPositionIndex(0, things.size());
System.out.println(things.get(0));
checkArgument(things.size() > 0);
}
public static void main(String[] args) {
List<ThingAMajig> fullList =
Lists.newArrayList(new ThingAMajig(
"that thingy for the furnace I have been holding off buying"));
List<ThingAMajig> emptyList = Lists.newLinkedList();
goByFirstItemOfTheseAMajigs(fullList);
}
}
source
share