Guava prerequisites, empty list

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);
    // 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<ThingAMajig> fullList =
        Lists.newArrayList(new ThingAMajig(
            "that thingy for the furnace I have been holding off buying"));
    List<ThingAMajig> emptyList = Lists.newLinkedList();
    goByFirstItemOfTheseAMajigs(fullList);
    // goByFirstItemOfTheseAMajigs(emptyList); // This *bombs*
  }
}
+5
source share
5 answers

checkElementIndex().

checkPositionIndex() , (.. add(0, obj) ), .

+14

.

list.get(0) .

, , checkElementIndex checkArgument(!list.isEmpty()).

+3

checkElementIndex

:

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

0

twitter-commons checkNotBlank. , Iterable , . :

/**
* 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 <S, T extends Iterable<S>> T checkNotBlank(T argument, String message,
      Object... args) {
  Preconditions.checkNotNull(argument, message, args);
  Preconditions.checkArgument(!Iterables.isEmpty(argument), message, args);
  return argument;
}

.

0

valid4j hamcrest-matchers ( Maven Central org.valid4j: valid4j)

For preconditions and postconditions (mostly statements -> throwing AssertionError):

import static org.valid4j.Assertive.*;

require(list, hasSize(greaterThan(0)));

Or to test input (resetting a user-resettable exception):

import static org.valid4j.Validation.*;

validate(list, hasSize(greaterThan(0)), otherwiseThrowing(EmptyListException.class));

References:

0
source

All Articles