Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

Chapter 18: java.util Part 2: More Utility Classes 551


its own. The second is the abstract classListResourceBundle, which manages resources in
an array of key/value pairs.ListResourceBundleadds the methodgetContents( ), which
all subclasses must implement. It is shown here:


protected abstract Object[ ][ ] getContents( )

It returns a two-dimensional array that contains key/value pairs that represent resources.
The keys must be strings. The values are typically strings, but can be other types of objects.
Here is an example that demonstrates using a resource bundle. The resource bundle
has the family nameSampleRB. Two resource bundle classes of this family are created by
extendingListResourceBundle. The first is calledSampleRB, and it is the default bundle
(which uses English). It is shown here:


import java.util.*;


Method Description
static ResourceBundle
getBundle(StringfamilyName,
Localeloc,
ClassLoaderldr,
ResourceBundle.Controlcntl)

Loads the resource bundle with a family name of
familyNameusing the specified locale and the specified
class loader. The loading process is under the control of
cntl. ThrowsMissingResourceExceptionif no resource
bundle matching the family name specified byfamilyName
is available. (Added by Java SE 6.)
abstract Enumeration<String> getKeys( ) Returns the resource bundle keys as an enumeration of
strings. Any parent’s keys are also obtained.
Locale getLocale( ) Returns the locale suppor ted by the resource bundle.
final Object getObject(Stringk) Returns the object associated with the key passed viak.
ThrowsMissingResourceExceptionifkis not in the
resource bundle.
final String getString(Stringk) Returns the string associated with the key passed viak.
ThrowsMissingResourceExceptionifkis not in the
resource bundle. ThrowsClassCastExceptionif the
object associated withkis not a string.
final String[ ] getStringArray(Stringk) Returns the string array associated with the key passed
viak. ThrowsMissingResourceExceptionifkis not in the
resource bundle. ThrowsClassCastExceptionif the
object associated withkis not a string array.
protected abstract Object
handleGetObject(Stringk)

Returns the object associated with the key passed viak.
Returnsnullifkis not in the resource bundle.
protected Set<String> handleKeySet( ) Returns the resource bundle keys as a set of strings. No
parent’s keys are obtained. Also, keys withnullvalues
are not obtained. (Added by Java SE 6.)
Set<String> keySet( ) Returns the resource bundle keys as a set of strings. Any
parent keys are also obtained. (Added by Java SE 6.)
protected void
setParent(ResourceBundleparent)

Setsparentas the parent bundle for the resource bundle.
When a key is looked up, the parent will be searched if
the key is not found in the invoking resource object.

TABLE 18-17 The Methods Defined byResourceBundle(continued)

Free download pdf