src/share/classes/java/util/logging/Level.java
Print this page
*** 25,34 ****
--- 25,35 ----
package java.util.logging;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
+ import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
/**
* The Level class defines a set of standard logging levels that
*** 61,71 ****
*
* @since 1.4
*/
public class Level implements java.io.Serializable {
! private static String defaultBundle = "sun.util.logging.resources.logging";
/**
* @serial The non-localized name of the level.
*/
private final String name;
--- 62,72 ----
*
* @since 1.4
*/
public class Level implements java.io.Serializable {
! private static final String defaultBundle = "sun.util.logging.resources.logging";
/**
* @serial The non-localized name of the level.
*/
private final String name;
*** 79,89 ****
* @serial The resource bundle name to be used in localizing the level name.
*/
private final String resourceBundleName;
// localized level name
! private String localizedLevelName;
/**
* OFF is a special level that can be used to turn off logging.
* This level is initialized to <CODE>Integer.MAX_VALUE</CODE>.
*/
--- 80,91 ----
* @serial The resource bundle name to be used in localizing the level name.
*/
private final String resourceBundleName;
// localized level name
! private transient String localizedLevelName;
! private transient Locale cachedLocale;
/**
* OFF is a special level that can be used to turn off logging.
* This level is initialized to <CODE>Integer.MAX_VALUE</CODE>.
*/
*** 207,216 ****
--- 209,219 ----
}
this.name = name;
this.value = value;
this.resourceBundleName = resourceBundleName;
this.localizedLevelName = resourceBundleName == null ? name : null;
+ this.cachedLocale = null;
KnownLevel.add(this);
}
/**
* Return the level's localization resource bundle name, or
*** 248,268 ****
// instead of getName() to avoid calling the subclass's version
final String getLevelName() {
return this.name;
}
! final synchronized String getLocalizedLevelName() {
if (localizedLevelName != null) {
return localizedLevelName;
}
try {
! ResourceBundle rb = ResourceBundle.getBundle(resourceBundleName);
! localizedLevelName = rb.getString(name);
} catch (Exception ex) {
localizedLevelName = name;
}
return localizedLevelName;
}
// Returns a mirrored Level object that matches the given name as
// specified in the Level.parse method. Returns null if not found.
--- 251,325 ----
// instead of getName() to avoid calling the subclass's version
final String getLevelName() {
return this.name;
}
! private String computeLocalizedLevelName(Locale newLocale) {
! ResourceBundle rb = ResourceBundle.getBundle(resourceBundleName, newLocale);
! final String localizedName = rb.getString(name);
!
! final boolean isDefaultBundle = defaultBundle.equals(resourceBundleName);
! if (!isDefaultBundle) return localizedName;
!
! // This is a trick to determine whether the name has been translated
! // or not. If it has not been translated, we need to use Locale.ROOT
! // when calling toUpperCase().
! final Locale rbLocale = rb.getLocale();
! final Locale locale =
! Locale.ROOT.equals(rbLocale)
! || name.equals(localizedName.toUpperCase(Locale.ROOT))
! ? Locale.ROOT : rbLocale;
!
! // ALL CAPS in a resource bundle's message indicates no translation
! // needed per Oracle translation guideline. To workaround this
! // in Oracle JDK implementation, convert the localized level name
! // to uppercase for compatibility reason.
! return Locale.ROOT.equals(locale) ? name : localizedName.toUpperCase(locale);
! }
!
! // Avoid looking up the localizedLevelName twice if we already
! // have it.
! final String getCachedLocalizedLevelName() {
!
if (localizedLevelName != null) {
+ if (cachedLocale != null) {
+ if (cachedLocale.equals(Locale.getDefault())) {
+ // OK: our cached value was looked up with the same
+ // locale. We can use it.
return localizedLevelName;
}
+ }
+ }
+
+ if (resourceBundleName == null) {
+ // No resource bundle: just use the name.
+ return name;
+ }
+
+ // We need to compute the localized name.
+ // Either because it's the first time, or because our cached
+ // value is for a different locale. Just return null.
+ return null;
+ }
+
+ final synchronized String getLocalizedLevelName() {
+
+ // See if we have a cached localized name
+ final String cachedLocalizedName = getCachedLocalizedLevelName();
+ if (cachedLocalizedName != null) {
+ return cachedLocalizedName;
+ }
+ // No cached localized name or cache invalid.
+ // Need to compute the localized name.
+ final Locale newLocale = Locale.getDefault();
try {
! localizedLevelName = computeLocalizedLevelName(newLocale);
} catch (Exception ex) {
localizedLevelName = name;
}
+ cachedLocale = newLocale;
return localizedLevelName;
}
// Returns a mirrored Level object that matches the given name as
// specified in the Level.parse method. Returns null if not found.
*** 316,325 ****
--- 373,383 ----
/**
* Returns a string representation of this Level.
*
* @return the non-localized name of the Level, for example "INFO".
*/
+ @Override
public final String toString() {
return name;
}
/**
*** 418,427 ****
--- 476,486 ----
/**
* Compare two objects for value equality.
* @return true if and only if the two objects have the same level value.
*/
+ @Override
public boolean equals(Object ox) {
try {
Level lx = (Level)ox;
return (lx.value == this.value);
} catch (Exception ex) {
*** 431,440 ****
--- 490,500 ----
/**
* Generate a hashcode.
* @return a hashcode based on the level value
*/
+ @Override
public int hashCode() {
return this.value;
}
// KnownLevel class maintains the global list of all known levels.