KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log > Priority


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.log;
18
19 import java.io.Serializable JavaDoc;
20
21 /**
22  * Class representing and holding constants for priority.
23  *
24  * @author Peter Donald
25  */

26 public final class Priority
27     implements Serializable JavaDoc
28 {
29     /**
30      * Developer orientated messages, usually used during development of product.
31      */

32     public static final Priority DEBUG = new Priority( "DEBUG", 5 );
33
34     /**
35      * Useful information messages such as state changes, client connection, user login etc.
36      */

37     public static final Priority INFO = new Priority( "INFO", 10 );
38
39     /**
40      * A problem or conflict has occurred but it may be recoverable, then
41      * again it could be the start of the system failing.
42      */

43     public static final Priority WARN = new Priority( "WARN", 15 );
44
45     /**
46      * A problem has occurred but it is not fatal. The system will still function.
47      */

48     public static final Priority ERROR = new Priority( "ERROR", 20 );
49
50     /**
51      * Something caused whole system to fail. This indicates that an administrator
52      * should restart the system and try to fix the problem that caused the failure.
53      */

54     public static final Priority FATAL_ERROR = new Priority( "FATAL_ERROR", 25 );
55
56     /**
57      * Do not log anything.
58      */

59     public static final Priority NONE = new Priority( "NONE", Integer.MAX_VALUE );
60
61     private final String JavaDoc m_name;
62     private final int m_priority;
63
64     /**
65      * Retrieve a Priority object for the name parameter.
66      *
67      * @param priority the priority name
68      * @return the Priority for name
69      */

70     public static Priority getPriorityForName( final String JavaDoc priority )
71     {
72         if( Priority.DEBUG.getName().equals( priority ) )
73         {
74             return Priority.DEBUG;
75         }
76         else if( Priority.INFO.getName().equals( priority ) )
77         {
78             return Priority.INFO;
79         }
80         else if( Priority.WARN.getName().equals( priority ) )
81         {
82             return Priority.WARN;
83         }
84         else if( Priority.ERROR.getName().equals( priority ) )
85         {
86             return Priority.ERROR;
87         }
88         else if( Priority.FATAL_ERROR.getName().equals( priority ) )
89         {
90             return Priority.FATAL_ERROR;
91         }
92         else if( Priority.NONE.getName().equals( priority ) )
93         {
94             return Priority.NONE;
95         }
96         else
97         {
98             return Priority.DEBUG;
99         }
100     }
101
102     /**
103      * Private Constructor to block instantiation outside class.
104      *
105      * @param name the string name of priority
106      * @param priority the numerical code of priority
107      */

108     private Priority( final String JavaDoc name, final int priority )
109     {
110         if( null == name )
111         {
112             throw new NullPointerException JavaDoc( "name" );
113         }
114
115         m_name = name;
116         m_priority = priority;
117     }
118
119     /**
120      * Overidden string to display Priority in human readable form.
121      *
122      * @return the string describing priority
123      */

124     public String JavaDoc toString()
125     {
126         return "Priority[" + getName() + "/" + getValue() + "]";
127     }
128
129     /**
130      * Get numerical value associated with priority.
131      *
132      * @return the numerical value
133      */

134     public int getValue()
135     {
136         return m_priority;
137     }
138
139     /**
140      * Get name of priority.
141      *
142      * @return the priorities name
143      */

144     public String JavaDoc getName()
145     {
146         return m_name;
147     }
148
149     /**
150      * Test whether this priority is greater than other priority.
151      *
152      * @param other the other Priority
153      * @return TRUE if the priority is greater else FALSE
154      */

155     public boolean isGreater( final Priority other )
156     {
157         return m_priority > other.getValue();
158     }
159
160     /**
161      * Test whether this priority is lower than other priority.
162      *
163      * @param other the other Priority
164      * @return TRUE if the priority is lower else FALSE
165      */

166     public boolean isLower( final Priority other )
167     {
168         return m_priority < other.getValue();
169     }
170
171     /**
172      * Test whether this priority is lower or equal to other priority.
173      *
174      * @param other the other Priority
175      * @return TRUE if the priority is lower or equal else FALSE
176      */

177     public boolean isLowerOrEqual( final Priority other )
178     {
179         return m_priority <= other.getValue();
180     }
181
182     /**
183      * Helper method that replaces deserialized object with correct singleton.
184      *
185      * @return the singleton version of object
186      */

187     private Object JavaDoc readResolve()
188     {
189         return getPriorityForName( m_name );
190     }
191 }
192
Popular Tags