|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object ch.elca.el4j.util.codingsupport.Reject
public final class Reject
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:
In this example an
public class AccountDao {
public void saveAccount(Account x) {
Reject.ifNull(x, IllegalArgumentException.class, "My String");
...
}
}
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.
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 |
---|
public static void ifNull(Object object)
object
- Is the object to be analyzed.public static void ifNull(Object object, String reason)
object
- Is the object to be analyzed.reason
- Is the message to explain the reason of the exception.public static void ifNull(Object object, Class<? extends RuntimeException> exceptionType, Object... exceptionArguments)
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
.public static void ifFalse(boolean condition)
ifCondition(boolean)
instead
condition
- Is the condition to be analyzed.public static void ifCondition(boolean condition)
condition
- Is the condition to be analyzed.public static void ifFalse(boolean condition, String reason)
ifCondition(boolean,String)
instead
condition
- Is the condition to be analyzed.reason
- Is the message to explain the reason of the exception.public static void ifCondition(boolean condition, String reason)
condition
- Is the condition to be analyzed.reason
- Is the message to explain the reason of the exception.public static void ifFalse(boolean condition, Class<? extends RuntimeException> exceptionType, Object... exceptionArguments)
ifCondition(boolean, Class, Object...)
instead
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
.public static void ifCondition(boolean condition, Class<? extends RuntimeException> exceptionType, Object... exceptionArguments)
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
.public static void ifEmpty(String s)
s
- Is the string to be analyzed.public static void ifEmpty(String s, String reason)
s
- Is the string to be analyzed.reason
- Is the message to explain the reason of the exception.public static void ifEmpty(String s, Class<? extends RuntimeException> exceptionType, Object... exceptionArguments)
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
.public static void ifEmpty(Collection<?> c)
c
- The collection.public static void ifEmpty(Collection<?> c, String reason)
c
- The collection.reason
- Message that explains the reason of the thrown exception.public static void ifEmpty(Collection<?> c, Class<? extends RuntimeException> exceptionType, Object... exceptionArguments)
c
- The collection.exceptionType
- the kind of RuntimeException
to be thrown in
case the precondition is violated.exceptionArguments
- constructor arguments for exceptionType
.public static void ifEmpty(Object[] objects)
objects
- The object array.public static void ifEmpty(Object[] objects, String reason)
objects
- The object array.reason
- Message that explains the reason of the thrown exception.public static void ifEmpty(Object[] objects, Class<? extends RuntimeException> exceptionType, Object... exceptionArguments)
objects
- The object array.exceptionType
- the kind of RuntimeException
to be thrown in
case the precondition is violated.exceptionArguments
- constructor arguments for exceptionType
.public static void ifNotAssignableTo(Object o, Class<?> assignableTo)
o
- Is the object that must be assignable to.assignableTo
- Is the target class.public static void ifNotAssignableTo(Object o, Class<?> assignableTo, String reason)
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.public static void ifNotAssignableTo(Object o, Class<?> assignableTo, Class<? extends RuntimeException> exceptionType, Object... exceptionArguments)
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
.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |