-
Enhancement
-
Resolution: Fixed
-
P4
-
6
-
b53
-
generic, x86, sparc
-
generic, solaris_7, windows_xp
Name: krT82822 Date: 10/29/99
java version "1.2.2"
HotSpot VM (1.0.1, mixed mode, build g)
The java.util.ResourceBundle class needs to provide a way to determine if
it contains a resource for a given key, without necesarily fetching the
resource. Currently, if getObject(String) is called and there is no resource
associated with the given key, then ResourceBundle throws a
MissingResourceException. The cost of throwing and catching the exception is too
high if all you want to know is whether or not the key is valid.
I suggest adding two new methods to the ResourceBundle API. The first is a
public, final method which correctly handles inherited resources from the
bundle's parent. The other is a protected method which the user may override for
the specific type of resource bundle in question.
// Added to java.util.ResourceBundle
public final boolean hasKey(String key) {
return handleHasKey(key) || parent.hasKey(key);
}
protected boolean handleHasKey(String key) {
// The default implementation relies on getKeys()
// Sub-classes are strongly encouraged to provide a better implementation
Enumeration enum = getKeys();
while (enum.hasMoreElements()) {
if (key.equals(enum.nextElement())) {
return true;
}
}
return false;
}
NOTE: The above implementation of handleHasKey(String) has a flaw in it. It uses
the getKeys() method which will cause it to search ALL the keys, not just those
in this resource bundle. However, if bug #4095319 is fixed according to my
suggestion (see my comment attached to the bug) then you can call
handleGetKeys() instead and get the correct semantics.
(Review ID: 97177)
======================================================================
java version "1.2.2"
HotSpot VM (1.0.1, mixed mode, build g)
The java.util.ResourceBundle class needs to provide a way to determine if
it contains a resource for a given key, without necesarily fetching the
resource. Currently, if getObject(String) is called and there is no resource
associated with the given key, then ResourceBundle throws a
MissingResourceException. The cost of throwing and catching the exception is too
high if all you want to know is whether or not the key is valid.
I suggest adding two new methods to the ResourceBundle API. The first is a
public, final method which correctly handles inherited resources from the
bundle's parent. The other is a protected method which the user may override for
the specific type of resource bundle in question.
// Added to java.util.ResourceBundle
public final boolean hasKey(String key) {
return handleHasKey(key) || parent.hasKey(key);
}
protected boolean handleHasKey(String key) {
// The default implementation relies on getKeys()
// Sub-classes are strongly encouraged to provide a better implementation
Enumeration enum = getKeys();
while (enum.hasMoreElements()) {
if (key.equals(enum.nextElement())) {
return true;
}
}
return false;
}
NOTE: The above implementation of handleHasKey(String) has a flaw in it. It uses
the getKeys() method which will cause it to search ALL the keys, not just those
in this resource bundle. However, if bug #4095319 is fixed according to my
suggestion (see my comment attached to the bug) then you can call
handleGetKeys() instead and get the correct semantics.
(Review ID: 97177)
======================================================================
- duplicates
-
JDK-4494785 RFE: ResourceBundles needs a hasKey method
-
- Closed
-
- relates to
-
JDK-6323465 Explore using ResourceBundle.hasKey() in UIDefaults
-
- Open
-