qflib 0.98.1

de.qfs.lib.util
Class MessageCollector

java.lang.Object
  |
  +--de.qfs.lib.util.MessageCollector

public class MessageCollector
extends java.lang.Object

Very simple mechanism for collecting warning and error messages.

This class is intended for use in a parser or similar application where many warnings and errors may arise during one run. Each message passed to the MessageCollector is assigned a level of severity, WARNING for warnings, ERROR for errors and FATAL_ERROR for fatal errors. After processing is finished, the number of warnings, errors and fatal errors can be queried and the collected messages are available via getMessages.

Things get even more interesting when nested calls need to be handled, e.g. when a parser needs to include files. To that end an instance mechanism is implemented that associates a MessageCollector with the current thread. For JDK 1.2 and above, ThreadLocal is used for this, but for JDK 1.1 be sure to release MessageCollector instances before a thread terminates, or they will become uncollectable garbage.

Version:
$Revision: 1.6 $
Author:
Gregor Schmid

Field Summary
static int ERROR
          Level for errors.
static int FATAL_ERROR
          Level for fatal errors.
static int NO_ERROR
          Level for neither warnings nor errors.
static int WARNING
          Level for warnings.
 
Constructor Summary
MessageCollector()
          Create a new MessageCollector.
 
Method Summary
 void addMessage(int level, java.lang.String message)
          Add a message to the MessageCollector.
 int getErrorCount()
          Get the number of errors that occured.
 int getFatalErrorCount()
          Get the number of fatal errors that occured.
 java.lang.String[] getMessages()
          Get the collected messages.
 int getWarningCount()
          Get the number of warnings that occured.
 int getWorstCase()
          Get the highest level of the collected messages.
static MessageCollector instance()
          Get the MessageCollector for the current thread, creating a new one if necessary.
static void release()
          Destroy the MessageCollector for the current thread.
 void reset()
          Reset the MessageCollector by clearing the collected messages and resetting the counts.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NO_ERROR

public static final int NO_ERROR
Level for neither warnings nor errors.

WARNING

public static final int WARNING
Level for warnings.

ERROR

public static final int ERROR
Level for errors.

FATAL_ERROR

public static final int FATAL_ERROR
Level for fatal errors.
Constructor Detail

MessageCollector

public MessageCollector()
Create a new MessageCollector.
Method Detail

instance

public static MessageCollector instance()
Get the MessageCollector for the current thread, creating a new one if necessary.
Returns:
The MessageCollector for the current thread.
Since:
0.98.0

release

public static void release()
Destroy the MessageCollector for the current thread. With JDK 1.2 and above, MessageCollectors will be garbage collected after a thread terminates, but with JDK 1.1 it is necessary to call this method to avoid memory leaks.
Since:
0.98.0

addMessage

public void addMessage(int level,
                       java.lang.String message)
Add a message to the MessageCollector.
Parameters:
level - The level of the message.
message - The message.

getMessages

public java.lang.String[] getMessages()
Get the collected messages.
Returns:
The collected messages.

getWarningCount

public int getWarningCount()
Get the number of warnings that occured.
Returns:
The number of warnings.

getErrorCount

public int getErrorCount()
Get the number of errors that occured.
Returns:
The number of errors.

getFatalErrorCount

public int getFatalErrorCount()
Get the number of fatal errors that occured.
Returns:
The number of fatal errors.

getWorstCase

public int getWorstCase()
Get the highest level of the collected messages.
Returns:
The highest level so far.

reset

public void reset()
Reset the MessageCollector by clearing the collected messages and resetting the counts.

qflib 0.98.1