Skip to content

Easy and Consistent Argument Cheking Helper for Java Applications

License

app-components/args

Repository files navigation

Args

A lightweight, zero-dependency Java library for clean and semantically correct argument validation.

Why Args?

When validating method and constructor arguments, Args provides a better alternative to Objects.requireNonNull() with three key advantages:

1. Semantic Correctness

Throws IllegalArgumentException (indicating invalid input) rather than NullPointerException (indicating a programming bug). This distinction is crucial for proper exception handling and debugging.

2. Ergonomic Design

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");
  }
}

3. Consistent API

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() and requireFalse()

Installation

Gradle (Kotlin DSL)

dependencies {
    implementation("io.github.appcomponents:args:VERSION")
}

Gradle (Groovy DSL)

dependencies {
    implementation 'io.github.appcomponents:args:VERSION'
}

Maven

<dependency>
    <groupId>io.github.appcomponents</groupId>
    <artifactId>args</artifactId>
    <version>VERSION</version>
</dependency>

Usage

Null Validation

public void processUser(User user) {
    Args.notNull(user, "User cannot be null");
    // ... process user
}

// With default message
String value = Args.notNull(parameter);

String Validation

// 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");

Array Validation

// 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");

Boolean Conditions

// Validate arbitrary conditions
Args.requireTrue(age >= 18, "User must be 18 or older");
Args.requireFalse(isBlocked, "User account is blocked");

Record Compact Constructors

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");
  }
}

API Reference

Null Checks

  • <T> T notNull(T object) - Validates object is not null
  • <T> T notNull(T object, String message) - Validates with custom message

String Validation

  • String notEmpty(String string) - Validates string is not null or empty
  • String notEmpty(String string, String message) - Validates with custom message
  • String notBlank(String string) - Validates string is not null or blank
  • String notBlank(String string, String message) - Validates with custom message

Array Validation

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.

Boolean Conditions

  • void requireTrue(boolean condition, String message) - Throws if condition is false
  • void requireFalse(boolean condition, String message) - Throws if condition is true

Design Principles

When to Use Args

Use Args for validating method and constructor parameters where invalid input is a client error.

When to Use Objects.requireNonNull()

Use Objects.requireNonNull() for internal null-checks where NullPointerException is semantically appropriate (indicating a programming bug).

Exception Type

All Args methods throw IllegalArgumentException to indicate that the caller provided invalid arguments.

Requirements

  • Java 17 or higher
  • Zero dependencies (except test dependencies)

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Support

If you encounter any issues or have questions, please file an issue on the GitHub issue tracker.

About

Easy and Consistent Argument Cheking Helper for Java Applications

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages