public abstract class AbstractValidator extends Object
Here's a pattern we want to avoid:
A typical pattern for concrete subclasses is:
public class MyValidator extends AbstractValidator {
// no instantiation outside the class
private MyValidator() { }
private void validate(SomeInterface someImplementation) {
if ( ... some problem ... ) {
addError(" detail message ");
} else if ( ... some other problem ... ) {
addError(" other detail message ");
}
throwExceptionIfMessages();
}
/** Validates an implementation of ...
* If all is well, this method silently returns. If issues are found, a
* ValidationException is thrown, with details of the discovered issues.
*
* @throws ValidationException if issues were found
* @throws NullPointerException if parameter is null
*/
public static void validateImplementation(SomeInterface someImplementation) {
new MyValidator().validate(someImplementation);
}
}
Production code would then use the public API:
SomeInterface myImplementation = ... MyValidator.validateImplementation(myImplementation);
| Constructor and Description |
|---|
AbstractValidator() |
| Modifier and Type | Method and Description |
|---|---|
protected void |
addError(String message)
Add a message prefixed with "Error: "
|
protected void |
addInfo(String message)
Add a message prefixed with "Info: "
|
protected void |
addVerbatim(String message)
Add a message verbatim.
|
protected void |
addWarning(String message)
Add a message prefixed with "Warning: "
|
List<String> |
getMessages()
Returns the list of messages.
|
String |
getMessagesAsString()
Returns the messages as a string (newlines after each individual message).
|
protected boolean |
nullOrEmpty(String s)
This predicate returns true if the supplied string is null, or if (once trimmed of whitespace)
its length is zero.
|
protected int |
numberOfMessages()
Returns the number of messages stored.
|
protected void |
throwException()
This method throws an exception to terminate the validation.
|
protected void |
throwException(String message)
This method can be used when the validator finds an unrecoverable error and wants
to terminate the validation.
|
protected void |
throwException(String message,
Throwable cause)
This method can be used when the validator finds an unrecoverable error and wants
to terminate the validation.
|
protected void |
throwExceptionIfMessages()
This method will throw a default exception if the message buffer is not empty.
|
public List<String> getMessages()
public String getMessagesAsString()
protected boolean nullOrEmpty(String s)
s - the stringprotected int numberOfMessages()
protected void addInfo(String message)
message - the messageprotected void addWarning(String message)
message - the messageprotected void addError(String message)
message - the messageprotected void addVerbatim(String message)
message - the messageprotected void throwExceptionIfMessages()
ValidationException - thrown if any messages are pendingprotected void throwException()
ValidationException - thrown using the default messageprotected void throwException(String message)
message - the exception messageValidationException - thrown using the specified messageprotected void throwException(String message, Throwable cause)
message - the exception messagecause - an underlying causeValidationException - thrown using the specified message and causeCopyright © 2014. All Rights Reserved.