io.greptime.common.util.Ensures Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2023 Greptime Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.greptime.common.util;
/**
* Simple static methods to be called at the start of your own methods to verify
* correct arguments and state.
*/
public final class Ensures {
/**
* Checks that the specified object reference is not {@code null}.
*
* @param obj the object reference to check for nullity
* @param the type of the reference
* @return {@code obj} if not {@code null}
* @throws NullPointerException if {@code obj} is {@code null}
*/
public static T ensureNonNull(T obj) {
if (obj == null) {
throw new NullPointerException();
}
return obj;
}
/**
* Checks that the specified object reference is not {@code null} and
* throws a customized {@link NullPointerException} if it is.
*
* @param obj the object reference to check for nullity
* @param message detail message to be used in the event that a {@code
* NullPointerException} is thrown
* @param the type of the reference
* @return {@code obj} if not {@code null}
* @throws NullPointerException if {@code obj} is {@code null}
*/
public static T ensureNonNull(T obj, String message) {
if (obj == null) {
throw new NullPointerException(message);
}
return obj;
}
/**
* Checks that the specified object reference is not {@code null} and
* throws a customized {@link NullPointerException} if it is.
*
* @param obj the object reference to check for nullity
* @param fmt the exception message with format string
* @param args arguments referenced by the format specifiers in the format
* string
* @param the type of the reference
* @return {@code obj} if not {@code null}
* @throws NullPointerException if {@code obj} is {@code null}
*/
public static T ensureNonNull(T obj, String fmt, Object... args) {
if (obj == null) {
throw new NullPointerException(String.format(fmt, args));
}
return obj;
}
/**
* Ensures the truth of an expression involving one or more parameters
* to the calling method.
*
* @param expression a boolean expression
* @throws IllegalArgumentException if {@code expression} is false
*/
public static void ensure(boolean expression) {
if (!expression) {
throw new IllegalArgumentException();
}
}
/**
* Ensures the truth of an expression involving one or more parameters
* to the calling method.
*
* @param expression a boolean expression
* @param message the exception message to use if the check fails;
* will be converted to a string using
* {@link String#valueOf(Object)}
* @throws IllegalArgumentException if {@code expression} is false
*/
public static void ensure(boolean expression, Object message) {
if (!expression) {
throw new IllegalArgumentException(String.valueOf(message));
}
}
/**
* Ensures the truth of an expression involving one or more parameters
* to the calling method.
*
* @param expression a boolean expression
* @param fmt the exception message with format string
* @param args arguments referenced by the format specifiers in the format
* string
* @throws IllegalArgumentException if {@code expression} is false
*/
public static void ensure(boolean expression, String fmt, Object... args) {
if (!expression) {
throw new IllegalArgumentException(String.format(fmt, args));
}
}
private Ensures() {}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy