001: /*
002: * Copyright (C) The Apache Software Foundation. All rights reserved.
003: *
004: * This software is published under the terms of the Apache Software License
005: * version 1.1, a copy of which has been included with this distribution in
006: * the LICENSE file.
007: */
008: package org.jivesoftware.util.log;
009:
010: import java.io.ObjectStreamException;
011: import java.io.Serializable;
012:
013: /**
014: * Class representing and holding constants for priority.
015: *
016: * @author <a href="mailto:peter@apache.org">Peter Donald</a>
017: */
018: public final class Priority implements Serializable {
019:
020: /**
021: * Developer orientated messages, usually used during development of product.
022: */
023: public final static Priority DEBUG = new Priority("DEBUG", 5);
024:
025: /**
026: * Useful information messages such as state changes, client connection, user login etc.
027: */
028: public final static Priority INFO = new Priority("INFO", 10);
029:
030: /**
031: * A problem or conflict has occurred but it may be recoverable, then
032: * again it could be the start of the system failing.
033: */
034: public final static Priority WARN = new Priority("WARN", 15);
035:
036: /**
037: * A problem has occurred but it is not fatal. The system will still function.
038: */
039: public final static Priority ERROR = new Priority("ERROR", 20);
040:
041: /**
042: * Something caused whole system to fail. This indicates that an administrator
043: * should restart the system and try to fix the problem that caused the failure.
044: */
045: public final static Priority FATAL_ERROR = new Priority(
046: "FATAL_ERROR", 25);
047:
048: private final String m_name;
049: private final int m_priority;
050:
051: /**
052: * Retrieve a Priority object for the name parameter.
053: *
054: * @param priority the priority name
055: * @return the Priority for name
056: */
057: public static Priority getPriorityForName(final String priority) {
058: if (Priority.DEBUG.getName().equals(priority))
059: return Priority.DEBUG;
060: else if (Priority.INFO.getName().equals(priority))
061: return Priority.INFO;
062: else if (Priority.WARN.getName().equals(priority))
063: return Priority.WARN;
064: else if (Priority.ERROR.getName().equals(priority))
065: return Priority.ERROR;
066: else if (Priority.FATAL_ERROR.getName().equals(priority))
067: return Priority.FATAL_ERROR;
068: else
069: return Priority.DEBUG;
070: }
071:
072: /**
073: * Private Constructor to block instantiation outside class.
074: *
075: * @param name the string name of priority
076: * @param priority the numerical code of priority
077: */
078: private Priority(final String name, final int priority) {
079: m_name = name;
080: m_priority = priority;
081: }
082:
083: /**
084: * Overidden string to display Priority in human readable form.
085: *
086: * @return the string describing priority
087: */
088: public String toString() {
089: return "Priority[" + getName() + "/" + getValue() + "]";
090: }
091:
092: /**
093: * Get numerical value associated with priority.
094: *
095: * @return the numerical value
096: */
097: public int getValue() {
098: return m_priority;
099: }
100:
101: /**
102: * Get name of priority.
103: *
104: * @return the priorities name
105: */
106: public String getName() {
107: return m_name;
108: }
109:
110: /**
111: * Test whether this priority is greater than other priority.
112: *
113: * @param other the other Priority
114: */
115: public boolean isGreater(final Priority other) {
116: return m_priority > other.getValue();
117: }
118:
119: /**
120: * Test whether this priority is lower than other priority.
121: *
122: * @param other the other Priority
123: */
124: public boolean isLower(final Priority other) {
125: return m_priority < other.getValue();
126: }
127:
128: /**
129: * Test whether this priority is lower or equal to other priority.
130: *
131: * @param other the other Priority
132: */
133: public boolean isLowerOrEqual(final Priority other) {
134: return m_priority <= other.getValue();
135: }
136:
137: /**
138: * Helper method that replaces deserialized object with correct singleton.
139: *
140: * @return the singleton version of object
141: * @throws ObjectStreamException if an error occurs
142: */
143: private Object readResolve() throws ObjectStreamException {
144: return getPriorityForName(m_name);
145: }
146: }
|