📄 validation.java
字号:
package org.redsoft.forum.util;
/**
* This class provides validation methods for Java Objects such as
* Strings and Integers. It also validate primitive int types.
*
* @author Charles Huang
*/
public class Validation
{
/**
* Used to check if the test string is not null, empty or falls
* between the specified minLength and maxLength values.
*
* @param testString String to be tested.
* @param minLength Minimum length a string may be.
* @param maxLength Maximum length a string may be.
* @since EW 1.0
*/
public static void validateString( final String testString,
final int minLength,
final int maxLength )
{
// If object is null, then an exception is thrown
if ( testString == null )
{
throw new IllegalArgumentException(
"String parameter can't be null." );
}
// Temp storage for string length
final int stringLength = testString.length();
// Constaint used to make sure that the user never
// passes a maxLength less than zero.
if ( maxLength < 0 )
{
throw new IllegalArgumentException(
"maxLength [" + maxLength
+"] param. can not be less than zero." );
}
// Constaint used to make sure that the user never
// passes a minLength less than zero.
if ( minLength < 0 )
{
throw new IllegalArgumentException(
"minLength [" + minLength
+"] param. can not be less than zero." );
}
// Checking to see that the user did not pass a min.
// value that is greater than the max.
if ( maxLength < minLength )
{
throw new IllegalArgumentException (
"minValue [" + minLength +
"] can not be greater than maxValue [" +
maxLength + "]" );
}
// If string is less than the specified min. value, then an
// exception is thrown.
if ( stringLength < minLength )
{
throw new IllegalArgumentException(
"String parameter (actual value = "
+ stringLength
+" ) is less than minLength of." + minLength);
}
// Making sure that the testString length does not exceed
// maxLength
if ( stringLength > maxLength )
{
throw new IllegalArgumentException(
"String parameter exceeds maxLength." +
"testString size [" + stringLength +
"], max. limit [" + maxLength + "]" );
}
}
/**
* Used to check if the object passed is not null.
*
* @param testObject Object to be tested.
* @since EW 1.0
*/
public static void validateNotNull( final Object testObject )
{
// If object is null, then an exception is thrown
if ( testObject == null )
{
throw new IllegalArgumentException( "Object can't be null." );
}
}
/**
* Provides the ability to validate the an Integer object
* to ensure that it is not null and that
* it falls between specified min. and max. parameters.
*
* @param testInt Integer to be tested.
* @param minValue Minimum possible value the Integer may have.
* @param maxValue Maximum possible value the Integer may have.
* @since EW 1.0
*/
public static void validateInteger( final Integer testInt,
final int minValue,
final int maxValue )
{
// Temp. storage for int. size
final int intSize = testInt.intValue();
// Checking to see that the user did not pass a min.
// value that is greater than the max.
if ( maxValue < minValue )
{
throw new IllegalArgumentException (
"minValue [" + minValue +
"] can not be greater than maxValue[" +
maxValue + "]" );
}
// If string has zero length, then an exception is thrown.
if ( testInt == null )
{
throw new IllegalArgumentException
( "Integer parameter can not be null." );
}
// Checking that the testInt value is not less than the
// min. allowable value.
if ( intSize < minValue )
{
final String errorMsg =
"Integer size [" + intSize + "] is less than min. "
+ "value possible[" + minValue + "]";
throw new IllegalArgumentException( errorMsg );
}
// Checking that the testInt value is not more than the
// max. allowable value.
if ( intSize > maxValue )
{
final String errorMsg =
"Integer size [" + intSize + "] is greater than max. "
+ "value possible[" + maxValue + "]";
throw new IllegalArgumentException( errorMsg );
}
}
/**
* Provides the ability to validate a primitive int to ensure that
* it falls between the min and max possible parameters.
*
* @param testInt int to be tested.
* @param minValue Minimum possible value the Integer may have.
* @param maxValue Maximum possible value the Integer may have.
* @since EW 1.0
*/
public static void validateInt( final int testInt,
final int minValue,
final int maxValue )
{
// Checking to see that the user did not pass a min.
// value that is greater than the max.
if ( maxValue < minValue )
{
throw new IllegalArgumentException (
"minValue [" + minValue +
"] can not be greater than maxValue[" +
maxValue + "]" );
}
// Checking that the testInt value is not less than the
// min. allowable value.
if ( testInt < minValue )
{
final String errorMsg =
"Int size [" + testInt + "] is less than min. "
+ "value possible[" + minValue + "]";
throw new IllegalArgumentException( errorMsg );
}
// Checking that the testInt value is not more than the
// max. allowable value.
if ( testInt > maxValue )
{
final String errorMsg =
"Int size [" + testInt + "] is greater than max. "
+ "value possible[" + maxValue + "]";
throw new IllegalArgumentException( errorMsg );
}
}
/**
* Provides the ability to validate a primitive int to ensure that
* it does not fall below the min parameter.
*
* @param testInt int to be tested.
* @param minValue Minimum possible value the Integer may have.
* @since EW 1.0
*/
public static void validateInt( final int testInt,
final int minValue )
{
// Checking that the testInt value is not less than the
// min. allowable value.
if ( testInt < minValue )
{
final String errorMsg =
"Int size [" + testInt + "] is less than min. "
+ "value possible[" + minValue + "]";
throw new IllegalArgumentException( errorMsg );
}
}
/**
* Provides the ability to validate a primitive double to ensure that
* it does not fall below the min parameter.
*
* @param testDouble double to be tested.
* @param minValue Minimum possible value the double may have.
* @since EW 1.0
*/
public static void validateDoubleGreaterThanMin( final double testDouble,
final int minValue )
{
// Checking that the testDouble value is not less than the
// min. allowable value.
if ( testDouble < minValue )
{
final String errorMsg =
"Double size [" + testDouble + "] is less than min. "
+ "value possible[" + minValue + "]";
throw new IllegalArgumentException( errorMsg );
}
}
/**
* Provides the ability to validate a primitive double to ensure that
* it does not fall below the min parameter.
*
* @param testDouble double to be tested.
* @param minValue Minimum possible value the double may have.
* @param maxValue Maximum possible value the double may have.
* @since EW 1.0
*/
public static void validateDouble( final double testDouble,
final int minValue,
final int maxValue )
{
// Checking that the testDouble value is not less than the
// min. allowable value.
if ( testDouble < minValue )
{
final String errorMsg =
"Double size [" + testDouble + "] is less than min. "
+ "value possible[" + minValue + "]";
throw new IllegalArgumentException( errorMsg );
}
// Checking that the testDouble value is not greater than the
// max. allowable value.
if ( testDouble > maxValue )
{
final String errorMsg =
"Double size [" + testDouble + "] is greater than max. "
+ "value possible[" + maxValue + "]";
throw new IllegalArgumentException( errorMsg );
}
}
/**
* Provides the ability to validate a primitive double to ensure that
* it does not fall below the min parameter.
*
* @param testDouble double to be tested.
* @param minValue Minimum possible value the double may have.
* @param maxValue Maximum possible value the double may have.
* @since EW 1.0
*/
public static void validateDouble( final double testDouble,
final double minValue,
final double maxValue )
{
// Checking that the testDouble value is not less than the
// min. allowable value.
if ( testDouble < minValue )
{
final String errorMsg =
"Double size [" + testDouble + "] is less than min. "
+ "value possible[" + minValue + "]";
throw new IllegalArgumentException( errorMsg );
}
// Checking that the testDouble value is not greater than the
// max. allowable value.
if ( testDouble > maxValue )
{
final String errorMsg =
"Double size [" + testDouble + "] is greater than max. "
+ "value possible[" + maxValue + "]";
throw new IllegalArgumentException( errorMsg );
}
}
};// EOC
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -