KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > tax > AbstractUtil


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.tax;
20
21 import java.util.ResourceBundle JavaDoc;
22 import java.text.MessageFormat JavaDoc;
23
24 /**
25  *
26  * @author Libor Kramolis
27  * @version 0.2
28  */

29 public abstract class AbstractUtil {
30     /** Cached isLoggable value. */
31     private boolean loggable;
32     /** Has loggable already been initialized. */
33     private boolean loggableInit = false;
34     /** Package resource bundle. */
35     private ResourceBundle JavaDoc bundle;
36
37
38     /**
39      * Kind of equals that treat null-ed object as equvivalent.
40      * Suitable while testing for property equalence before firing.
41      * return true if these are same
42      */

43     public static boolean equals (Object JavaDoc a, Object JavaDoc b) {
44         if ( a != null ) {
45             return (a.equals (b));
46         } else {
47             return (a == b);
48         }
49     }
50
51     /**
52      * Just for debugging purposes.
53      */

54     public final void debug (String JavaDoc message, Throwable JavaDoc ex) {
55         if ( isLoggable() ) {
56             System.err.println("[org.netbeans.tax] " + message);
57             if ( ex != null ) {
58                 ex.printStackTrace (System.err);
59             }
60         }
61     }
62     
63     
64     /**
65      * Just for debugging purposes.
66      */

67     public final void debug (String JavaDoc message) {
68         debug (message, null);
69     }
70
71
72     /**
73      * Just for debugging purposes.
74      */

75     public final void debug (Throwable JavaDoc ex) {
76         debug (ex.getMessage(), ex);
77     }
78
79
80     /** Test if <code>debug (...)</code> will log something.
81      */

82     public final synchronized boolean isLoggable () {
83         if ( loggableInit == false ) {
84             loggable = Boolean.getBoolean("org.netbeans.tax"); // NOI18N
85
loggableInit = true;
86         }
87         return loggable;
88     }
89
90     /**
91      * @return bundle for this instance package
92      */

93     protected final synchronized ResourceBundle JavaDoc getBundle () {
94         return ResourceBundle.getBundle(getClass().getName().replaceFirst("\\.[^.]+$", ".Bundle")); // NOI18N
95
}
96     
97     /** Get localized string.
98      * @param key key of localized value.
99      * @return localized value.
100      */

101     public final String JavaDoc getString (String JavaDoc key) {
102         return getBundle ().getString (key);
103     }
104     
105     /** Get localized string by passing parameter.
106      * @param key key of localized value.
107      * @param param argument to use when formating the message
108      * @return localized value.
109      */

110     public final String JavaDoc getString (String JavaDoc key, Object JavaDoc param) {
111         return MessageFormat.format (getBundle().getString (key), new Object JavaDoc[] {param});
112     }
113     
114     /** Get localized character. Usually used on mnemonic.
115      * @param key key of localized value.
116      * @return localized value.
117      */

118     public final char getChar (String JavaDoc key) {
119         return getString (key).charAt (0);
120     }
121
122 }
123
Popular Tags