Skip to content

Commit

Permalink
[#42] Move precondition checks into common package/class.
Browse files Browse the repository at this point in the history
  • Loading branch information
korydraughn committed Feb 3, 2025
1 parent c4d7cc4 commit 89f5cd2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.irods.irods4j.input_validation;

public class PreconditionViolationException extends RuntimeException {

private static final long serialVersionUID = 1L;

public PreconditionViolationException(String message) {
super(message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.irods.irods4j.input_validation;

public class Preconditions {

/**
* Checks if the object is null.
*
* @param o The object to check for null.
* @param message The precondition violation message.
*
* @throws PreconditionViolationException If the object is null.
*
* @since 0.1.0
*/
public static void notNull(Object o, String message) {
if (null == o) {
throw new PreconditionViolationException(message);
}
}

/**
* Checks if a string is null or empty.
*
* @param s The string to check.
* @param message The precondition violation message.
*
* @throws PreconditionViolationException If the object is null.
*
* @since 0.1.0
*/
public static void notNullOrEmpty(String s, String message) {
if (null == s || s.isEmpty()) {
throw new PreconditionViolationException(message);
}
}

/**
* Checks if an integer is greater than or equal to a threshold.
*
* @param value The integer to test.
* @param threshold The integer to test against.
* @param message The precondition violation message.
*
* @throws PreconditionViolationException If the object is null.
*
* @since 0.1.0
*/
public static void greaterThanOrEqualToValue(long value, long threshold, String message) {
if (value < threshold) {
throw new PreconditionViolationException(message);
}
}

}

0 comments on commit 89f5cd2

Please sign in to comment.