jdk/src/share/classes/java/util/logging/Level.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7803 56bc97d69d93
child 16098 9001e536ab4e
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7803
diff changeset
     2
 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util.logging;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.ResourceBundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * The Level class defines a set of standard logging levels that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * can be used to control logging output.  The logging Level objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * are ordered and are specified by ordered integers.  Enabling logging
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * at a given level also enables logging at all higher levels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Clients should normally use the predefined Level constants such
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * as Level.SEVERE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * The levels in descending order are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <li>SEVERE (highest value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <li>WARNING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <li>INFO
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <li>CONFIG
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <li>FINE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <li>FINER
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <li>FINEST  (lowest value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * In addition there is a level OFF that can be used to turn
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * off logging, and a level ALL that can be used to enable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * logging of all messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * It is possible for third parties to define additional logging
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * levels by subclassing Level.  In such cases subclasses should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * take care to chose unique integer level values and to ensure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * they maintain the Object uniqueness property across serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * by defining a suitable readResolve method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public class Level implements java.io.Serializable {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
    62
    private static java.util.ArrayList<Level> known = new java.util.ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static String defaultBundle = "sun.util.logging.resources.logging";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @serial  The non-localized name of the level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private final String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @serial  The integer value of the level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private final int value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @serial The resource bundle name to be used in localizing the level name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private final String resourceBundleName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * OFF is a special level that can be used to turn off logging.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * This level is initialized to <CODE>Integer.MAX_VALUE</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public static final Level OFF = new Level("OFF",Integer.MAX_VALUE, defaultBundle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * SEVERE is a message level indicating a serious failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * In general SEVERE messages should describe events that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * of considerable importance and which will prevent normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * program execution.   They should be reasonably intelligible
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * to end users and to system administrators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * This level is initialized to <CODE>1000</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public static final Level SEVERE = new Level("SEVERE",1000, defaultBundle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * WARNING is a message level indicating a potential problem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * In general WARNING messages should describe events that will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * be of interest to end users or system managers, or which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * indicate potential problems.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * This level is initialized to <CODE>900</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public static final Level WARNING = new Level("WARNING", 900, defaultBundle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * INFO is a message level for informational messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Typically INFO messages will be written to the console
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * or its equivalent.  So the INFO level should only be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * used for reasonably significant messages that will
3853
9d2382b74894 6882363: 4/4 typos in java.util.logging javadocs
dcubed
parents: 2
diff changeset
   113
     * make sense to end users and system administrators.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * This level is initialized to <CODE>800</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public static final Level INFO = new Level("INFO", 800, defaultBundle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * CONFIG is a message level for static configuration messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * CONFIG messages are intended to provide a variety of static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * configuration information, to assist in debugging problems
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * that may be associated with particular configurations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * For example, CONFIG message might include the CPU type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * the graphics depth, the GUI look-and-feel, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * This level is initialized to <CODE>700</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public static final Level CONFIG = new Level("CONFIG", 700, defaultBundle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * FINE is a message level providing tracing information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * All of FINE, FINER, and FINEST are intended for relatively
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * detailed tracing.  The exact meaning of the three levels will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * vary between subsystems, but in general, FINEST should be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * for the most voluminous detailed output, FINER for somewhat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * less detailed output, and FINE for the  lowest volume (and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * most important) messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * In general the FINE level should be used for information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * that will be broadly interesting to developers who do not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * a specialized interest in the specific subsystem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * FINE messages might include things like minor (recoverable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * failures.  Issues indicating potential performance problems
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * are also worth logging as FINE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * This level is initialized to <CODE>500</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public static final Level FINE = new Level("FINE", 500, defaultBundle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * FINER indicates a fairly detailed tracing message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * By default logging calls for entering, returning, or throwing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * an exception are traced at this level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * This level is initialized to <CODE>400</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public static final Level FINER = new Level("FINER", 400, defaultBundle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * FINEST indicates a highly detailed tracing message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * This level is initialized to <CODE>300</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public static final Level FINEST = new Level("FINEST", 300, defaultBundle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * ALL indicates that all messages should be logged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * This level is initialized to <CODE>Integer.MIN_VALUE</CODE>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public static final Level ALL = new Level("ALL", Integer.MIN_VALUE, defaultBundle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Create a named Level with a given integer value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Note that this constructor is "protected" to allow subclassing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * In general clients of logging should use one of the constant Level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * objects such as SEVERE or FINEST.  However, if clients need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * add new logging levels, they may subclass Level and define new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * constants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @param name  the name of the Level, for example "SEVERE".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param value an integer value for the level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @throws NullPointerException if the name is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    protected Level(String name, int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        this(name, value, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Create a named Level with a given integer value and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * given localization resource name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @param name  the name of the Level, for example "SEVERE".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param value an integer value for the level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @param resourceBundleName name of a resource bundle to use in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *    localizing the given name. If the resourceBundleName is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *    or an empty string, it is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @throws NullPointerException if the name is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    protected Level(String name, int value, String resourceBundleName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        this.resourceBundleName = resourceBundleName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        synchronized (Level.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            known.add(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Return the level's localization resource bundle name, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * null if no localization bundle is defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @return localization resource bundle name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public String getResourceBundleName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return resourceBundleName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Return the non-localized string name of the Level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @return non-localized name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Return the localized string name of the Level, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * the current default locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * If no localization information is available, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * non-localized name is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @return localized name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public String getLocalizedName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            ResourceBundle rb = ResourceBundle.getBundle(resourceBundleName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            return rb.getString(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /**
3853
9d2382b74894 6882363: 4/4 typos in java.util.logging javadocs
dcubed
parents: 2
diff changeset
   248
     * Returns a string representation of this Level.
9d2382b74894 6882363: 4/4 typos in java.util.logging javadocs
dcubed
parents: 2
diff changeset
   249
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @return the non-localized name of the Level, for example "INFO".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public final String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Get the integer value for this level.  This integer value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * can be used for efficient ordering comparisons between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Level objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @return the integer value for this level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    public final int intValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    private static final long serialVersionUID = -8176160795706313070L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    // Serialization magic to prevent "doppelgangers".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    // This is a performance optimization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    private Object readResolve() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        synchronized (Level.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            for (int i = 0; i < known.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                Level other = known.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                if (this.name.equals(other.name) && this.value == other.value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                        && (this.resourceBundleName == other.resourceBundleName ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                            (this.resourceBundleName != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                            this.resourceBundleName.equals(other.resourceBundleName)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    return other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            // Woops.  Whoever sent us this object knows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            // about a new log level.  Add it to our list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            known.add(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Parse a level name string into a Level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * The argument string may consist of either a level name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * or an integer value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * <li>     "SEVERE"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * <li>     "1000"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @param  name   string to be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @throws NullPointerException if the name is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @throws IllegalArgumentException if the value is not valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * Valid values are integers between <CODE>Integer.MIN_VALUE</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * and <CODE>Integer.MAX_VALUE</CODE>, and all known level names.
3853
9d2382b74894 6882363: 4/4 typos in java.util.logging javadocs
dcubed
parents: 2
diff changeset
   304
     * Known names are the levels defined by this class (e.g., <CODE>FINE</CODE>,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * <CODE>FINER</CODE>, <CODE>FINEST</CODE>), or created by this class with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * appropriate package access, or new levels defined or created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * by subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @return The parsed value. Passing an integer that corresponds to a known name
3853
9d2382b74894 6882363: 4/4 typos in java.util.logging javadocs
dcubed
parents: 2
diff changeset
   310
     * (e.g., 700) will return the associated name (e.g., <CODE>CONFIG</CODE>).
9d2382b74894 6882363: 4/4 typos in java.util.logging javadocs
dcubed
parents: 2
diff changeset
   311
     * Passing an integer that does not (e.g., 1) will return a new level name
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * initialized to that value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    public static synchronized Level parse(String name) throws IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        // Check that name is not null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        name.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        // Look for a known Level with the given non-localized name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        for (int i = 0; i < known.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            Level l = known.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            if (name.equals(l.name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                return l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        // Now, check if the given name is an integer.  If so,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        // first look for a Level with the given value and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        // if necessary create one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            int x = Integer.parseInt(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            for (int i = 0; i < known.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                Level l = known.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                if (l.value == x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    return l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            // Create a new Level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            return new Level(name, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        } catch (NumberFormatException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            // Not an integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            // Drop through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        // Finally, look for a known level with the given localized name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        // in the current default locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        // This is relatively expensive, but not excessively so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        for (int i = 0; i < known.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            Level l =  known.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            if (name.equals(l.getLocalizedName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                return l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        // OK, we've tried everything and failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        throw new IllegalArgumentException("Bad level \"" + name + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Compare two objects for value equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @return true if and only if the two objects have the same level value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    public boolean equals(Object ox) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            Level lx = (Level)ox;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            return (lx.value == this.value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Generate a hashcode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @return a hashcode based on the level value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        return this.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
}