ch.elca.el4j.util.codingsupport
Class Reject

java.lang.Object
  extended by ch.elca.el4j.util.codingsupport.Reject

public final class Reject
extends Object

This class provides support for assertions and for design by contract. Methods from this class are used at the beginning of a method, before anyone is working with the given parameters. Parameters which are checked at the end of a method or inside a method should be checked by using the assert keyword of JDK 1.4. More about this you can find here .
Example:

     public class AccountDao {
         public void saveAccount(Account x) {
             Reject.ifNull(x, IllegalArgumentException.class, "My String");
             ...
         }
     }
 
In this example an IllegalArgumentException is thrown if the given account is null. This may prevent an ugly NullPointerException. Example 2:
     public class AccountDao {
         public void saveAccount(Account x) {
             Reject.ifNull(x);
             ...
         }
     }
 


In this example, a ch.elca.el4j.util.codingsupport.PreconditionRTException is thrown if the given account is null. This prevents that an ugly NullPointerException.

It is also possible to add a reason to the reject method, if it is not absolutely clear what is checked. Alternatively, you can customize the exception to be thrown in case the condition is violated by providing the exception class and the arguments to its constructor (the rationale of this is that it does not require the (potential expensive) creation of an exception class when its not needed. The existence and uniqueness of a constructor capable of taking the provided arguments is not verified statically and must therefore be ensured by the user.

Author:
Martin Zeltner (MZE), Adrian Moos(AMS)
File-location:
Reject
Last check-in date:
2009-08-04 15:35:01 +0200 (Di, 04. Aug 2009) by swismer for revision 3883

Method Summary
static void ifCondition(boolean condition)
          Method to ensure that a condition is false.
static void ifCondition(boolean condition, Class<? extends RuntimeException> exceptionType, Object... exceptionArguments)
          Method to ensure that a condition is false.
static void ifCondition(boolean condition, String reason)
          Method to ensure that a condition is false.
static void ifEmpty(Collection<?> c)
          Method to ensure that a collection is not empty.
static void ifEmpty(Collection<?> c, Class<? extends RuntimeException> exceptionType, Object... exceptionArguments)
          Method to ensure that a collection is not empty.
static void ifEmpty(Collection<?> c, String reason)
          Method to ensure that a collection is not empty.
static void ifEmpty(Object[] objects)
          Method to ensure that an object array is not empty.
static void ifEmpty(Object[] objects, Class<? extends RuntimeException> exceptionType, Object... exceptionArguments)
          Method to ensure that an object array is not empty.
static void ifEmpty(Object[] objects, String reason)
          Method to ensure that an object array is not empty.
static void ifEmpty(String s)
          Method to ensure that a string is not empty.
static void ifEmpty(String s, Class<? extends RuntimeException> exceptionType, Object... exceptionArguments)
          Method to ensure that a string is not empty.
static void ifEmpty(String s, String reason)
          Method to ensure that a string is not empty.
static void ifFalse(boolean condition)
          Deprecated. Use ifCondition(boolean) instead
static void ifFalse(boolean condition, Class<? extends RuntimeException> exceptionType, Object... exceptionArguments)
          Deprecated. Use ifCondition(boolean, Class, Object...) instead
static void ifFalse(boolean condition, String reason)
          Deprecated. Use ifCondition(boolean,String) instead
static void ifNotAssignableTo(Object o, Class<?> assignableTo)
          Ensures that the given object is assignable to the given class.
static void ifNotAssignableTo(Object o, Class<?> assignableTo, Class<? extends RuntimeException> exceptionType, Object... exceptionArguments)
          Ensures that the given object is assignable to the given class.
static void ifNotAssignableTo(Object o, Class<?> assignableTo, String reason)
          Ensures that the given object is assignable to the given class.
static void ifNull(Object object)
          Method to ensure that an object is not null.
static void ifNull(Object object, Class<? extends RuntimeException> exceptionType, Object... exceptionArguments)
          Method to ensure that an object is not null.
static void ifNull(Object object, String reason)
          Method to ensure that an object is not null.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

ifNull

public static void ifNull(Object object)
Method to ensure that an object is not null. Used at the beginning of a method before working with parameters.

Parameters:
object - Is the object to be analyzed.

ifNull

public static void ifNull(Object object,
                          String reason)
Method to ensure that an object is not null. Used at the beginning of a method before working with parameters.

Parameters:
object - Is the object to be analyzed.
reason - Is the message to explain the reason of the exception.

ifNull

public static void ifNull(Object object,
                          Class<? extends RuntimeException> exceptionType,
                          Object... exceptionArguments)
Method to ensure that an object is not null. Used at the beginning of a method before working with parameters.

Parameters:
object - Is the object to be analyzed.
exceptionType - the kind of RuntimeException to be thrown in case the precondition is violated.
exceptionArguments - constructor arguments for exceptionType.

ifFalse

public static void ifFalse(boolean condition)
Deprecated. Use ifCondition(boolean) instead

Method to ensure that a condition is true. Used at the beginning of a method before working with parameters.

Parameters:
condition - Is the condition to be analyzed.

ifCondition

public static void ifCondition(boolean condition)
Method to ensure that a condition is false. Used at the beginning of a method before working with parameters.

Parameters:
condition - Is the condition to be analyzed.

ifFalse

public static void ifFalse(boolean condition,
                           String reason)
Deprecated. Use ifCondition(boolean,String) instead

Method to ensure that a condition is true. Used at the beginning of a method before working with parameters.

Parameters:
condition - Is the condition to be analyzed.
reason - Is the message to explain the reason of the exception.

ifCondition

public static void ifCondition(boolean condition,
                               String reason)
Method to ensure that a condition is false. Used at the beginning of a method before working with parameters.

Parameters:
condition - Is the condition to be analyzed.
reason - Is the message to explain the reason of the exception.

ifFalse

public static void ifFalse(boolean condition,
                           Class<? extends RuntimeException> exceptionType,
                           Object... exceptionArguments)
Deprecated. Use ifCondition(boolean, Class, Object...) instead

Method to ensure that a condition is true. Used at the beginning of a method before working with parameters.

Parameters:
condition - Is the condition to be analyzed.
exceptionType - the kind of RuntimeException to be thrown in case the precondition is violated.
exceptionArguments - constructor arguments for exceptionType.

ifCondition

public static void ifCondition(boolean condition,
                               Class<? extends RuntimeException> exceptionType,
                               Object... exceptionArguments)
Method to ensure that a condition is false. Used at the beginning of a method before working with parameters.

Parameters:
condition - Is the condition to be analyzed.
exceptionType - the kind of RuntimeException to be thrown in case the precondition is violated.
exceptionArguments - constructor arguments for exceptionType.

ifEmpty

public static void ifEmpty(String s)
Method to ensure that a string is not empty. Used at the beginning of a method before working with parameters.

Parameters:
s - Is the string to be analyzed.

ifEmpty

public static void ifEmpty(String s,
                           String reason)
Method to ensure that a string is not empty. Used at the beginning of a method before working with parameters.

Parameters:
s - Is the string to be analyzed.
reason - Is the message to explain the reason of the exception.

ifEmpty

public static void ifEmpty(String s,
                           Class<? extends RuntimeException> exceptionType,
                           Object... exceptionArguments)
Method to ensure that a string is not empty. Used at the beginning of a method before working with parameters.

Parameters:
s - Is the string to be analyzed.
exceptionType - the kind of RuntimeException to be thrown in case the precondition is violated.
exceptionArguments - constructor arguments for exceptionType.

ifEmpty

public static void ifEmpty(Collection<?> c)
Method to ensure that a collection is not empty.

Parameters:
c - The collection.

ifEmpty

public static void ifEmpty(Collection<?> c,
                           String reason)
Method to ensure that a collection is not empty.

Parameters:
c - The collection.
reason - Message that explains the reason of the thrown exception.

ifEmpty

public static void ifEmpty(Collection<?> c,
                           Class<? extends RuntimeException> exceptionType,
                           Object... exceptionArguments)
Method to ensure that a collection is not empty.

Parameters:
c - The collection.
exceptionType - the kind of RuntimeException to be thrown in case the precondition is violated.
exceptionArguments - constructor arguments for exceptionType.

ifEmpty

public static void ifEmpty(Object[] objects)
Method to ensure that an object array is not empty.

Parameters:
objects - The object array.

ifEmpty

public static void ifEmpty(Object[] objects,
                           String reason)
Method to ensure that an object array is not empty.

Parameters:
objects - The object array.
reason - Message that explains the reason of the thrown exception.

ifEmpty

public static void ifEmpty(Object[] objects,
                           Class<? extends RuntimeException> exceptionType,
                           Object... exceptionArguments)
Method to ensure that an object array is not empty.

Parameters:
objects - The object array.
exceptionType - the kind of RuntimeException to be thrown in case the precondition is violated.
exceptionArguments - constructor arguments for exceptionType.

ifNotAssignableTo

public static void ifNotAssignableTo(Object o,
                                     Class<?> assignableTo)
Ensures that the given object is assignable to the given class.

Parameters:
o - Is the object that must be assignable to.
assignableTo - Is the target class.

ifNotAssignableTo

public static void ifNotAssignableTo(Object o,
                                     Class<?> assignableTo,
                                     String reason)
Ensures that the given object is assignable to the given class.

Parameters:
o - Is the object that must be assignable to.
assignableTo - Is the target class.
reason - Is the message to explain the reason of the exception.

ifNotAssignableTo

public static void ifNotAssignableTo(Object o,
                                     Class<?> assignableTo,
                                     Class<? extends RuntimeException> exceptionType,
                                     Object... exceptionArguments)
Ensures that the given object is assignable to the given class.

Parameters:
o - Is the object that must be assignable to.
assignableTo - Is the target class.
exceptionType - Is the type of exception to throw.
exceptionArguments - constructor arguments for exceptionType.


Copyright © 2005-2011 ELCA. All Rights Reserved.