de.smartics.maven.plugin.jboss.modules.util.Arg Maven / Gradle / Ivy
/*
* Copyright 2013-2017 smartics, Kronseder & Reiner GmbH
*
* 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 de.smartics.maven.plugin.jboss.modules.util;
/**
* Utility class to check an argument.
*/
public final class Arg
{
// ********************************* Fields *********************************
// --- constants ------------------------------------------------------------
// --- members --------------------------------------------------------------
// ****************************** Initializer *******************************
// ****************************** Constructors ******************************
/**
* Utility class pattern.
*/
private Arg()
{
}
// ****************************** Inner Classes *****************************
// ********************************* Methods ********************************
// --- init -----------------------------------------------------------------
// --- get&set --------------------------------------------------------------
// --- business -------------------------------------------------------------
/**
* Checks if {@code value} is null.
*
* @param name the name of the argument of error reporting. Not used if no
* exception is thrown. May be null, although not
* recommended.
* @param value the value of the argument to check to be not {@code null}.
* @return the passed in {@code value}.
* @throws NullPointerException if {@code value} is null.
* @param the type of the argument to check.
*/
public static T checkNotNull(final String name, final T value)
throws NullPointerException
{
checkNotNull(name, value, null);
return value;
}
/**
* Checks if {@code value} is null providing an additional
* message.
*
* @param name the name of the argument of error reporting. Not used if no
* exception is thrown. May be null, although not
* recommended.
* @param value the value of the argument to check to be not {@code null}.
* @param message the message to pass to the exception as additional
* information to the standard message being generated.
* @return the passed in {@code value}.
* @throws NullPointerException if {@code value} is null.
* @param the type of the argument to check.
*/
public static T checkNotNull(final String name, final T value,
final String message) throws NullPointerException
{
if (value == null)
{
throw new NullArgumentException(name, message);
}
return value;
}
/**
* Checks if {@code value} is blank.
*
* @param name the name of the argument of error reporting. Not used if no
* exception is thrown. May be null, although not
* recommended.
* @param value the value of the argument to check to be not blank.
* @return the passed in {@code value}.
* @throws NullArgumentException if {@code value} is null.
* @throws BlankArgumentException if {@code value} is blank.
* @param a character sequence like a {@link String}.
*/
public static T checkNotBlank(final String name,
final T value) throws NullArgumentException, BlankArgumentException
{
checkNotBlank(name, value, null);
return value;
}
/**
* Checks if {@code value} is blank providing an additional message.
*
* @param name the name of the argument of error reporting. Not used if no
* exception is thrown. May be null, although not
* recommended.
* @param value the value of the argument to check to be not blank.
* @param message the message to pass to the exception as additional
* information to the standard message being generated.
* @return the passed in {@code value}.
* @throws NullArgumentException if {@code value} is null.
* @throws BlankArgumentException if {@code value} is blank.
* @param a character sequence like a {@link String}.
*/
public static T checkNotBlank(final String name,
final T value, final String message) throws NullArgumentException,
BlankArgumentException
{
if (value == null)
{
throw new NullArgumentException(name, message);
}
if (isBlank(value))
{
throw new BlankArgumentException(name, message);
}
return value;
}
/**
* Checks if {@code value} is blank except null.
*
* @param name the name of the argument of error reporting. Not used if no
* exception is thrown. May be null, although not
* recommended.
* @param value the value of the argument to check to be not blank but may be
* null.
* @return the passed in {@code value}.
* @throws BlankExceptNullArgumentException if {@code value} is blank and not
* null.
* @param a character sequence like a {@link String}.
*/
public static T checkNotBlankExceptNull(
final String name, final T value) throws BlankExceptNullArgumentException
{
checkNotBlankExceptNull(name, value, null);
return value;
}
/**
* Checks if {@code value} is blank except null providing an
* additional message.
*
* @param name the name of the argument of error reporting. Not used if no
* exception is thrown. May be null, although not
* recommended.
* @param value the value of the argument to check to be not blank but may be
* null.
* @param message the message to pass to the exception as additional
* information to the standard message being generated.
* @return the passed in {@code value}.
* @throws BlankExceptNullArgumentException if {@code value} is blank and not
* null.
* @param a character sequence like a {@link String}.
*/
public static T checkNotBlankExceptNull(
final String name, final T value, final String message)
throws BlankExceptNullArgumentException
{
if (value != null && isBlank(value))
{
throw new BlankArgumentException(name, message);
}
return value;
}
// Copied from Apaches StringUtils and modified to cope with CharSequences.
// http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/StringUtils.html#isBlank%28java.lang.String%29
private static boolean isBlank(final T str)
{
// CHECKSTYLE:OFF
int strLen;
if (str == null || (strLen = str.length()) == 0) // NOPMD
{
return true;
}
for (int i = 0; i < strLen; i++)
{
if ((Character.isWhitespace(str.charAt(i)) == false)) // NOPMD
{
return false;
}
}
return true;
// CHECKSTYLE:ON
}
// --- object basics --------------------------------------------------------
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy