hu.icellmobilsoft.coffee.tool.utils.validation.ParamValidatorUtil Maven / Gradle / Ivy
/*-
* #%L
* Coffee
* %%
* Copyright (C) 2020 - 2023 i-Cell Mobilsoft Zrt.
* %%
* 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.
* #L%
*/
package hu.icellmobilsoft.coffee.tool.utils.validation;
import java.text.MessageFormat;
import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import org.apache.commons.lang3.StringUtils;
import hu.icellmobilsoft.coffee.dto.exception.InvalidParameterException;
import hu.icellmobilsoft.coffee.se.api.exception.BaseException;
/**
* Utility for function parameter validation.
*
* @author attila.kiss
* @since 2.3.0
*/
public class ParamValidatorUtil {
private ParamValidatorUtil() {
}
/**
* Ensures that the specified parameter cannot be null
.
*
* @param
* the type of the parameter
* @param object
* the parameter
* @param paramName
* the name of the parameter
* @return the not null
parameter
* @throws BaseException
* if the parameter is null
*/
public static T requireNonNull(T object, String paramName) throws BaseException {
if (Objects.isNull(object)) {
throw newInvalidParameterException("[{0}] object is null!", paramName);
}
return object;
}
/**
* Ensures that the specified parameter cannot be blank {@link String}.
*
* @param object
* the parameter
* @param paramName
* the name of the parameter
* @return the non blank parameter
* @throws BaseException
* if the parameter is blank
*/
public static String requireNonBlank(String object, String paramName) throws BaseException {
if (StringUtils.isBlank(object)) {
throw newInvalidParameterException("[{0}] object is blank!", paramName);
}
return object;
}
/**
* Ensures that the specified parameter cannot be an empty {@link Optional}.
*
* @param
* the type of the parameter
* @param object
* the parameter
* @param paramName
* the name of the parameter
* @return the value of the non-empty {@link Optional}
* @throws BaseException
* if the parameter is null
or empty
*/
public static T requireNonEmpty(Optional object, String paramName) throws BaseException {
requireNonNull(object, paramName);
return object.orElseThrow(() -> newInvalidParameterException("[{0}] object is empty!", paramName));
}
/**
* Ensures that the specified parameter cannot be an empty {@link Collection}.
*
* @param
* the type of the {@link Collection} parameter
* @param object
* the parameter
* @param paramName
* the name of the parameter
* @return the non-empty {@link Collection}
* @throws BaseException
* if the parameter is null
or empty
*/
public static Collection requireNonEmpty(Collection object, String paramName) throws BaseException {
requireNonNull(object, paramName);
if (object.isEmpty()) {
throw newInvalidParameterException("[{0}] object is empty!", paramName);
}
return object;
}
private static BaseException newInvalidParameterException(String messagePattern, Object... messageArguments) {
return new InvalidParameterException(MessageFormat.format(messagePattern, messageArguments));
}
}