001/*
002 * Copyright 2001-2004 The Apache Software Foundation.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017// Contributors:  Kitching Simon <Simon.Kitching@orange.ch>
018
019package org.apache.log4j;
020
021// Contributors:  Kitching Simon <Simon.Kitching@OOOrange.ch>
022
023/**
024   <b>Refrain from using this class directly, use
025   the {@link Level} class instead</b>.
026
027   @author Ceki G&uuml;lc&uuml; */
028public class Priority {
029
030    transient int level;
031    transient String levelStr;
032    transient int syslogEquivalent;
033
034    public final static int OFF_INT = Integer.MAX_VALUE;
035    public final static int FATAL_INT = 50000;
036    public final static int ERROR_INT = 40000;
037    public final static int WARN_INT = 30000;
038    public final static int INFO_INT = 20000;
039    public final static int DEBUG_INT = 10000;
040    // public final static int FINE_INT = DEBUG_INT;
041    public final static int ALL_INT = Integer.MIN_VALUE;
042
043    /**
044     * @deprecated Use {@link Level#FATAL} instead.
045     */
046    final static public Priority FATAL = new Level(FATAL_INT, "FATAL", 0);
047
048    /**
049     * @deprecated Use {@link Level#ERROR} instead.
050     */
051    @Deprecated
052    final static public Priority ERROR = new Level(ERROR_INT, "ERROR", 3);
053
054    /**
055     * @deprecated Use {@link Level#WARN} instead.
056     */
057    @Deprecated
058    final static public Priority WARN = new Level(WARN_INT, "WARN", 4);
059
060    /**
061     * @deprecated Use {@link Level#INFO} instead.
062     */
063    @Deprecated
064    final static public Priority INFO = new Level(INFO_INT, "INFO", 6);
065
066    /**
067     * @deprecated Use {@link Level#DEBUG} instead.
068     */
069    @Deprecated
070    final static public Priority DEBUG = new Level(DEBUG_INT, "DEBUG", 7);
071
072    /**
073      * Default constructor for deserialization.
074      */
075    protected Priority() {
076        level = DEBUG_INT;
077        levelStr = "DEBUG";
078        syslogEquivalent = 7;
079    }
080
081    /**
082       Instantiate a level object.
083       @param
084     */
085
086    /**
087     * Instantiate a level object. 
088     * 
089     * @param level a level as in int
090     * @param levelStr  a levelStr
091     * @param syslogEquivalent the syslog equivalent level integer
092     */
093    protected Priority(int level, String levelStr, int syslogEquivalent) {
094        this.level = level;
095        this.levelStr = levelStr;
096        this.syslogEquivalent = syslogEquivalent;
097    }
098
099    /**
100       Two priorities are equal if their level fields are equal.
101       @since 1.2
102     */
103    public boolean equals(Object o) {
104        if (o instanceof Priority) {
105            Priority r = (Priority) o;
106            return (this.level == r.level);
107        } else {
108            return false;
109        }
110    }
111
112    /**
113       Return the syslog equivalent of this priority as an integer.
114       @return the Syslog Equivalent of this Priority
115     */
116    public final int getSyslogEquivalent() {
117        return syslogEquivalent;
118    }
119
120    /**
121     * Returns <code>true</code> if this level has a higher or equal
122     *  level than the level passed as argument, <code>false</code>
123     *  otherwise.  
124     *  
125     *  <p>You should think twice before overriding the default
126     *  implementation of <code>isGreaterOrEqual</code> method.
127     *
128     * @param r a priority
129     * @return a boolean
130     */
131    public boolean isGreaterOrEqual(Priority r) {
132        return level >= r.level;
133    }
134
135    /**
136       Return all possible priorities as an array of Level objects in
137       descending order.
138    
139       @deprecated This method will be removed with no replacement.
140       @return array of all possible priorities
141    */
142    @Deprecated
143    public static Priority[] getAllPossiblePriorities() {
144        return new Priority[] { Priority.FATAL, Priority.ERROR, Level.WARN, Priority.INFO, Priority.DEBUG };
145    }
146
147    /**
148       Returns the string representation of this priority.
149     */
150    final public String toString() {
151        return levelStr;
152    }
153
154    /**
155     * Returns the integer representation of this level.
156     *
157     * @return integer representation of this level
158     */
159    public final int toInt() {
160        return level;
161    }
162
163    /**
164     * @deprecated Please use the {@link Level#toLevel(String)} method instead.
165     * 
166     * @param sArg a string to convert to a Priority 
167     * @return the corresponding Priority 
168    */
169    @Deprecated
170    public static Priority toPriority(String sArg) {
171        return Level.toLevel(sArg);
172    }
173
174    /**
175     * @deprecated Please use the {@link Level#toLevel(int)} method instead.   
176     * 
177     * @param val an integer to convert to a Priority
178     * @return the corresponding Priority
179     */
180    @Deprecated
181    public static Priority toPriority(int val) {
182        return toPriority(val, Priority.DEBUG);
183    }
184
185    /**
186     * @deprecated Please use the {@link Level#toLevel(int, Level)} method instead.   
187     * 
188     * @param val an integer value
189     * @param defaultPriority a default priority value
190     * @return corresponding Priority value
191     */
192    @Deprecated
193    public static Priority toPriority(int val, Priority defaultPriority) {
194        return Level.toLevel(val, (Level) defaultPriority);
195    }
196
197    /**
198     * @deprecated Please use the {@link Level#toLevel(String, Level)} method instead.   
199     * @param sArg string value
200     * @param defaultPriority a default Priority
201     * @return a Priority
202     */
203    @Deprecated
204    public static Priority toPriority(String sArg, Priority defaultPriority) {
205        return Level.toLevel(sArg, (Level) defaultPriority);
206    }
207}