How to create your own Warnings framework

Java has built in support for exceptions, but does not provide built in support for warnings. We can create our own Warnings class hirachy (warnings framework) and use.
Here you can raise warnings instances with specific parameterised details and each Warning class will implement its specific warning message using the parameters.

This is a growable hirachy of warnings that you can extend in the future.

 

 

Step 1:

Create your own Warnings class. We will make it an abstract class.

import java.text.MessageFormat;

/**
* Defines a warning
*/

public abstract class Warning {
 
  /**
   * Specific details about the Warning.
   */

  private String detailMessage;

  /**
   * Creates a new <code>Warning</code> with null as it's detail
   * message.
   */

  public Warning() {
      super();
  }
 
  /**
   * Returns the detail message string of the warning.
   * @return  the detail message string of this Warning instance.
   */

  public String getMessage() {
      String message;     
      String messageFormat = getMessageFormat();
      if (null == messageFormat) {
          message = detailMessage;
      }
      else {
          Object[] args = getMessageArgs();
          message = MessageFormat.format(messageFormat, args);
      }     
      return message;
  }
 
  /**
   * Returns the args that are used with the message format to create
   * the message for the exception.
   * @return The arguments for the message format, or null if there are
   * no arguments.
   */

  protected abstract Object[] getMessageArgs();
 
  /**
   * Returns a message format string that is appropriate for use by
   * java.text.MessageFormat. The message format returned by this method
   * is passed, along with the args returned by getMessageArgs, to
   * java.text.MessageFormat.format(). If the exception initialized
   * the standard message variable (by passing a message to the
   * constructor), then this method can return null and the initialized
   * message will be used.
   * 
   * @return the message format string for the exception, or null if
   * the initialized message should be used.
   */

  protected abstract String getMessageFormat();
}

 

 Step 2:

Now lets create our own Warnings classes which must be extended from Warning class or its sub classesa depending on the requirment. We can use attributes in the super class or define our own attributes. Also we can overide getMessageFormat() method to specify the warning message created using the parameters.

 

/**
 * Defines a specific warning.
 */

public class MyWarning extends Warning {
    /** Specific attribute1 */
    private final String attr1_;
    /** Specific attribute2 */
    private final String attr2_;

    /**
     * Creates a new <code>MyWarning</code> with null as it's detail message.
     */

    public MyWarning() {
        super();
        attr1_ = null;
        attr2_ = null;
    }

    /**
     * Creates a new <code>MyWarning </code> with the specified attr1.
     * @param attr1
     *            detail attr1 relevent to this warning.
     */

    public MyWarning(String attr1) {
        super();
        attr1_ = attr1;
        attr2_ = null;
    }

    /**
     * Creates a new <code>MyWarning</code> with the specified attr1and
     * attr2.
     * @param attr1
     *            detail attr1 relevent to this warning.
     * @param attr2
     *            detail attr2 relevent to this warning.
     */
    public MyWarning(String attr1, String attr2) {
        super();
        attr1_ = attr1;
        attr2_ = attr2;
    }

    /*
     * (non-Javadoc)
     *
     * @see Warning#getMessageArgs()
     */

    protected Object[] getMessageArgs() {
        Object[] args = { attr1_, //0
                attr2_ //1
        };
        return args;
    }

    /*
     * (non-Javadoc)
     * @see Warning#getMessageFormat()
     */
    protected String getMessageFormat() {
        String message;
        if ((getMessageArgs()[0] != null) && (getMessageArgs()[1] != null)) {
            message = "A MyWarning occured. attr1: \"{0}\" attr2: \"{1}\".";
        } else if ((getMessageArgs()[0] != null)) {
            message = "A MyWarning occured. attr1: \"{0}\".";
        } else {
            message = "A MyWarning occured.";
        }
        return message;
    }

    /**
     * @return Returns the attr1_.
     */

    public String getAttr1_() {
        return attr1_;
    }

    /**
     * @return Returns the attr2_.
     */

    public String getAttr2_() {
        return attr2_;
    }
}

 

Step 3:

Lets test our Warnings framework...

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TestMyWarnings {

    public static void main(String[] args) {
        TestMyWarnings testMyWarnings = new TestMyWarnings();
        List warnings = testMyWarnings.myProcess();
       
        Iterator it = warnings.iterator();
        while (it.hasNext()) {
            Warning warn = (Warning) it.next();
            // Log or process your warnings here
            System.out.println(warn.getMessage()); 
        }
    }
   
    public List myProcess(){ 
        List warnings = new ArrayList();
        // Do some processing
       
        // Oops we should raise a warning for this condition

        warnings.add(new MyWarning("MyParamValue1","MyParamValue2"));
       
        // More processing
       
        // We should raise a warning for this as well

        warnings.add(new MyWarning("MyParamValue11","MyParamValue22"));
       
        return warnings;
    }
}
 

Out put:

A MyWarning occured. attr1: "MyParamValue1" attr2: "MyParamValue2".
A MyWarning occured. attr1: "MyParamValue11" attr2: "MyParamValue22".

 

Current Rating: Rate This Code Sample: