Check parameters for validity

Effective java Item 38: Check parameters for validity is the general principle of detecting errors as soon as possible. Also known as the Fail fast it is good practice to notify consumers up front which will help to determine and detect source of errors. This example will show common ways to check parameters for validitiy with guava's preconditions and apache commons Validate.

Something to be aware of is there a difference in the exceptions that these two approachs throw. For instance, check parameter is not null in Guava throws a NullPointerException vs Apache commons Validator throws IllegalArgumentException. My recommendation is pick one within your applications and stick with it.

Google Guava

Check argument

@Test(expected=IllegalArgumentException.class)
public void check_argument_guava () {

    Integer stockPrice = 50;

    checkArgument(stockPrice > 50, "Stock price must be > 50");

    // ...
}

Check parameter is not null

@Test(expected=NullPointerException.class)
public void check_not_null_guava () {

    String val = null;

    checkNotNull(val, "val cannot be null");

    // ...
}

Check state of object

An example of where you may call a methods initialize method before any other method.

class Stock {

    private boolean initialized = false;

    Stock () {

    }

    void initialize () {
        this.initialized = true;
    }
}

@Test(expected=IllegalStateException.class)
public void check_state_of_object () {

    Stock stock = new Stock();

    checkState(stock.initialized);

    // ...
}

Check element index

@Test(expected=IndexOutOfBoundsException.class)
public void check_element_index_guava () {

    List<String> stockSold = Lists.newArrayList();

    checkElementIndex(10, stockSold.size(), "must have 10 transactions to qualify for...");

    // ...
}

Check position index

@Test(expected=IndexOutOfBoundsException.class)
public void check_position_index_guava () {

    List<String> availableStocksForSale = Lists.newArrayList();

    checkPositionIndex(1, availableStocksForSale.size(), "there must be 1 availble stock for sale");

    // ...
}

Apache Commons

Check argument

@Test
public void check_argument_apache_commons () {

    Integer stockPrice = 50;

    Validate.isTrue(stockPrice > 50, "Stock price must be > 50");

    // ...
}

Check parameter is not null

@Test(expected=IllegalArgumentException.class)
public void check_not_null_apache_commons () {

    String val = null;

    Validate.notNull(val, "val cannot be null");

    // ...     
}

Check position index

@Test(expected=IllegalArgumentException.class)
public void check_position_index_apache_commons () {

    List<String> availableStocksForSale = Lists.newArrayList();

    Validate.isTrue(availableStocksForSale.size() >=1, "there must be 1 availble stock for sale");

    // ... 
}

Check not empty

@Test(expected=IllegalArgumentException.class)
public void check_not_empty_index_apache_commons () {

    List<String> availableStocksForSale = Lists.newArrayList();

    Validate.notEmpty(availableStocksForSale, "there must be 1 availble stock for sale");

    // ... 
}