com.h3c.util

## Class AbstractValidator

- java.lang.Object
- - com.h3c.util.AbstractValidator

- Direct Known Subclasses:
    AbstractResourceValidator

      public abstract class AbstractValidator
      extends Object

  This class forms the basis for validators that want to report multiple warnings and errors.

  Here's a pattern we want to avoid:

  - There are multiple issues with an implementation
  - The first issue detected (#1) is reported
  - The developer fixes that issue
  - The first issue detected (#2) is reported
  - The developer fixes that issue
  - ...
  We want to discover and report all the issues at one time, allowing the developer to fix them all in one go.

  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 Summary

    Constructors
    Constructor and Description
    `AbstractValidator()`

  - ### Method Summary

    Methods
    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.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### AbstractValidator

          public AbstractValidator()

  - ### Method Detail

    - #### getMessages

          public List<String> getMessages()

      Returns the list of messages.
      Returns:
        the list of messages

    - #### getMessagesAsString

          public String getMessagesAsString()

      Returns the messages as a string (newlines after each individual message).
      Returns:
        the messages as a string

    - #### nullOrEmpty

          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.
      Parameters:
        `s` - the string
      Returns:
        true if the string is null or just whitespace

    - #### numberOfMessages

          protected int numberOfMessages()

      Returns the number of messages stored.
      Returns:
        the number of messages

    - #### addInfo

          protected void addInfo(String message)

      Add a message prefixed with "Info: "
      Parameters:
        `message` - the message

    - #### addWarning

          protected void addWarning(String message)

      Add a message prefixed with "Warning: "
      Parameters:
        `message` - the message

    - #### addError

          protected void addError(String message)

      Add a message prefixed with "Error: "
      Parameters:
        `message` - the message

    - #### addVerbatim

          protected void addVerbatim(String message)

      Add a message verbatim.
      Parameters:
        `message` - the message

    - #### throwExceptionIfMessages

          protected void throwExceptionIfMessages()

      This method will throw a default exception if the message buffer is not empty.
      Throws:
        `ValidationException` - thrown if any messages are pending

    - #### throwException

          protected void throwException()

      This method throws an exception to terminate the validation. It uses a default exception message of "Validation Failed".
      Throws:
        `ValidationException` - thrown using the default message

    - #### throwException

          protected void throwException(String message)

      This method can be used when the validator finds an unrecoverable error and wants to terminate the validation.
      Parameters:
        `message` - the exception message
      Throws:
        `ValidationException` - thrown using the specified message

    - #### throwException

          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.
      Parameters:
        `message` - the exception message
        `cause` - an underlying cause
      Throws:
        `ValidationException` - thrown using the specified message and cause

Copyright © 2014. All Rights Reserved.