A lightweight, zero-dependency Java library for clean and semantically correct argument validation.
When validating method and constructor arguments, Args provides a better alternative to Objects.requireNonNull() with three key advantages:
Throws IllegalArgumentException (indicating invalid input) rather than NullPointerException (indicating a programming bug). This distinction is crucial for proper exception handling and debugging.
Returns the validated value, enabling clean inline validation that's especially elegant in Java record compact constructors:
public record User(UUID id, String name) {
public User {
Args.notNull(id, "ID cannot be null");
Args.notBlank(name, "Name cannot be blank");
}
}Provides a uniform validation API for common scenarios:
- Null checks:
notNull() - Empty checks:
notEmpty()for arrays and strings - Blank checks:
notBlank()for strings - Boolean conditions:
requireTrue()andrequireFalse()
dependencies {
implementation("io.github.appcomponents:args:VERSION")
}dependencies {
implementation 'io.github.appcomponents:args:VERSION'
}<dependency>
<groupId>io.github.appcomponents</groupId>
<artifactId>args</artifactId>
<version>VERSION</version>
</dependency>public void processUser(User user) {
Args.notNull(user, "User cannot be null");
// ... process user
}
// With default message
String value = Args.notNull(parameter);// Ensure string is not null or empty
String username = Args.notEmpty(input, "Username cannot be empty");
// Ensure string is not null or blank (all whitespace)
String password = Args.notBlank(input, "Password cannot be blank");// Works with all primitive and object arrays
byte[] data = Args.notEmpty(bytes, "Data cannot be empty");
int[] numbers = Args.notEmpty(values, "Values cannot be empty");
String[] names = Args.notEmpty(items, "Items cannot be empty");// Validate arbitrary conditions
Args.requireTrue(age >= 18, "User must be 18 or older");
Args.requireFalse(isBlocked, "User account is blocked");The return-value design makes Args particularly elegant for Java records:
public record Product(
UUID id,
String name,
BigDecimal price,
String[] tags
) {
public Product {
id = Args.notNull(id, "Product ID is required");
name = Args.notBlank(name, "Product name is required");
price = Args.notNull(price, "Price is required");
tags = Args.notEmpty(tags, "At least one tag is required");
Args.requireTrue(price.compareTo(BigDecimal.ZERO) > 0,
"Price must be positive");
}
}<T> T notNull(T object)- Validates object is not null<T> T notNull(T object, String message)- Validates with custom message
String notEmpty(String string)- Validates string is not null or emptyString notEmpty(String string, String message)- Validates with custom messageString notBlank(String string)- Validates string is not null or blankString notBlank(String string, String message)- Validates with custom message
All primitive types and Object[] are supported:
byte[] notEmpty(byte[] array)char[] notEmpty(char[] array)short[] notEmpty(short[] array)int[] notEmpty(int[] array)long[] notEmpty(long[] array)float[] notEmpty(float[] array)double[] notEmpty(double[] array)boolean[] notEmpty(boolean[] array)Object[] notEmpty(Object[] array)
Each array method has a variant accepting a custom message.
void requireTrue(boolean condition, String message)- Throws if condition is falsevoid requireFalse(boolean condition, String message)- Throws if condition is true
Use Args for validating method and constructor parameters where invalid input is a client error.
Use Objects.requireNonNull() for internal null-checks where NullPointerException is semantically appropriate (indicating a programming bug).
All Args methods throw IllegalArgumentException to indicate that the caller provided invalid arguments.
- Java 17 or higher
- Zero dependencies (except test dependencies)
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
If you encounter any issues or have questions, please file an issue on the GitHub issue tracker.