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), andfalse
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, ajava.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.
There are different ways to consume the library depending on your preferred dependency management tool:
Add the following dependency to your pom
file:
<dependency>
<groupId>io.github.kennedykori</groupId>
<artifactId>utils</artifactId>
<version>2.0.0</version>
</dependency>
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")
<dependency org="io.github.kennedykori" name="utils" rev="2.0.0"/>
Alternatively, you can use gradle to compile and build the library. Just follow the steps below:
-
Clone the project from Github.
-
From the project root, run the following command:
./gradlew build
-
Add the generated jar (located at
build/libs
after a successful build) to your classpath. -
You're good to go. 👍
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
}
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.
Copyright (c) 2019 Kennedy Kori