Skip to content

Collection of utility methods and classes for checking and validating primitives and objects in Java

License

Notifications You must be signed in to change notification settings

kennedykori/jutils

Repository files navigation

🛠️🧰 Java Utils

Check and validate primitives and reference types in Java.

Build status Codecov coverage status Semantic Release: conventionalcommits GitHub License

Maven Central Javadoc


This library contains a utility class comprised of static methods, similar to java.util.Objects, which can check and validate objects/references and primitives. These utility methods fall into two categories:

  • Methods that check if a condition holds - These methods return a boolean value and typically have prefixes like has, in, is, etc. They take a value or values and return true if the value(s) satisfy a specific condition (e.g., checking if a number is negative), and false otherwise.

  • Methods that validate that a condition holds - These methods throw a java.lang.IllegalArgumentException if the condition they check for fails, and all have the prefix require. These methods take a value and return it if it meets a given condition (e.g., validating that a number isn't negative). Otherwise, a java.lang.IllegalArgumentException is thrown.

The utility methods in this class include methods for checking and validating a String's length, methods for checking and validating if a number is negative, methods for checking and validating if a number is less than or greater than a given base value, and methods for checking and validating if a number falls within a given range.

Get Started

There are different ways to consume the library depending on your preferred dependency management tool:

1. Maven

Add the following dependency to your pom file:

<dependency>
    <groupId>io.github.kennedykori</groupId>
    <artifactId>utils</artifactId>
    <version>2.0.0</version>
</dependency>

2. Gradle

Add the following dependency to your build.gradle:

implementation 'io.github.kennedykori:utils:2.0.0'

Or for Kotlin DSL, add the following to your build.gradle.kts:

implementation("io.github.kennedykori:utils:2.0.0")

3. Ivy

<dependency org="io.github.kennedykori" name="utils" rev="2.0.0"/>

Building the library

Alternatively, you can use gradle to compile and build the library. Just follow the steps below:

  1. Clone the project from Github.

  2. From the project root, run the following command:

    ./gradlew build
  3. Add the generated jar (located at build/libs after a successful build) to your classpath.

  4. You're good to go. 👍

Usage

Import the ObjectsUtils class in your code and use its static methods to check and validate your code.

Example 1:

import static io.github.kennedykori.utils.ObjectUtils.*;

public class Person {

	private final String firstName;
	private final String lastName;
	private final int age;
	
	public Person (String firstName, String lastName, int age) {
		this.firstName = requireMoreThanChars(2, firstName.trim(), "firstName should have atleast two characters.");
		this.lastName = requireCharsInRange(2, 31, lastName.trim(), "lastName should have atleast two characters and atmost 30 characters.");
		this.age = requireInRange(18, 36, age, "Only youths are allowed.");
	}
	
	// Other methods and properties
}

Example 2:

import static io.github.kennedykori.utils.ObjectUtils.*;

public class Address {
	
	private final String recipient;
	private final int box;
	private final String city;
	private final int zipCode;
	
	public Address (String recipient, int box, String city, int zipCode) {
		this.recipient = requireCharsInRange(2, 51, recipient.trim(), "recipient should have between 2 to 50 characters."); // equal to: this.recipient = requireLessThanChars(51, requireMoreThanChars(2, recipient.trim()));
		this.box = requireNonNegative(box, "box shouldn't be negative.");
		this.city = requireNonEmptyString(city, "city");
		this.zipCode = requireNonNegative(zipCode);
	}
	
	// Other methods and properties
}

API Reference

You can find a comprehensive list of all the available functions by referring to the online library documentation provided at this link.

Alternatively, you can generate the documentation using gradle by running the following command from the project root:

./gradlew javadoc

Then open build/docs/javadoc/index.html in your browser of choice.

License

MIT License

Copyright (c) 2019 Kennedy Kori

About

Collection of utility methods and classes for checking and validating primitives and objects in Java

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages