public void setValue(int value) { // Ensure that the value is positive Preconditions.checkArgument(value > 0, "Value must be positive: %s", value); this.value = value; }
public void login(String username, String password) { // Ensure that both username and password are not empty Preconditions.checkArgument( !Strings.isNullOrEmpty(username) && !Strings.isNullOrEmpty(password), "Username and password cannot be empty"); // Login logic }In this example, the checkArgument method is used to ensure that both the username and password passed to the login method are not empty. If either is empty, an IllegalArgumentException will be thrown with the message "Username and password cannot be empty". Overall, the Preconditions package is part of the Google Guava library, which provides a wide range of useful utility classes and methods for Java developers.