KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > ListResourceBundle


1 /*
2  * @(#)ListResourceBundle.java 1.27 05/09/01
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 /*
9  * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
10  * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
11  *
12  * The original version of this source code and documentation
13  * is copyrighted and owned by Taligent, Inc., a wholly-owned
14  * subsidiary of IBM. These materials are provided under terms
15  * of a License Agreement between Taligent and Sun. This technology
16  * is protected by multiple US and International patents.
17  *
18  * This notice and attribution to Taligent may not be removed.
19  * Taligent is a registered trademark of Taligent, Inc.
20  *
21  */

22
23 package java.util;
24
25 /**
26  * <code>ListResourceBundle</code> is an abstract subclass of
27  * <code>ResourceBundle</code> that manages resources for a locale
28  * in a convenient and easy to use list. See <code>ResourceBundle</code> for
29  * more information about resource bundles in general.
30  *
31  * <P>
32  * Subclasses must override <code>getContents</code> and provide an array,
33  * where each item in the array is a pair of objects.
34  * The first element of each pair is the key, which must be a
35  * <code>String</code>, and the second element is the value associated with
36  * that key.
37  *
38  * <p>
39  * The following <a name="sample">example</a> shows two members of a resource
40  * bundle family with the base name "MyResources".
41  * "MyResources" is the default member of the bundle family, and
42  * "MyResources_fr" is the French member.
43  * These members are based on <code>ListResourceBundle</code>
44  * (a related <a HREF="PropertyResourceBundle.html#sample">example</a> shows
45  * how you can add a bundle to this family that's based on a properties file).
46  * The keys in this example are of the form "s1" etc. The actual
47  * keys are entirely up to your choice, so long as they are the same as
48  * the keys you use in your program to retrieve the objects from the bundle.
49  * Keys are case-sensitive.
50  * <blockquote>
51  * <pre>
52  *
53  * public class MyResources extends ListResourceBundle {
54  * protected Object[][] getContents() {
55  * return new Object[][] = {
56  * // LOCALIZE THIS
57  * {"s1", "The disk \"{1}\" contains {0}."}, // MessageFormat pattern
58  * {"s2", "1"}, // location of {0} in pattern
59  * {"s3", "My Disk"}, // sample disk name
60  * {"s4", "no files"}, // first ChoiceFormat choice
61  * {"s5", "one file"}, // second ChoiceFormat choice
62  * {"s6", "{0,number} files"}, // third ChoiceFormat choice
63  * {"s7", "3 Mar 96"}, // sample date
64  * {"s8", new Dimension(1,5)} // real object, not just string
65  * // END OF MATERIAL TO LOCALIZE
66  * };
67  * }
68  * }
69  *
70  * public class MyResources_fr extends ListResourceBundle {
71  * protected Object[][] getContents() {
72  * return new Object[][] = {
73  * // LOCALIZE THIS
74  * {"s1", "Le disque \"{1}\" {0}."}, // MessageFormat pattern
75  * {"s2", "1"}, // location of {0} in pattern
76  * {"s3", "Mon disque"}, // sample disk name
77  * {"s4", "ne contient pas de fichiers"}, // first ChoiceFormat choice
78  * {"s5", "contient un fichier"}, // second ChoiceFormat choice
79  * {"s6", "contient {0,number} fichiers"}, // third ChoiceFormat choice
80  * {"s7", "3 mars 1996"}, // sample date
81  * {"s8", new Dimension(1,3)} // real object, not just string
82  * // END OF MATERIAL TO LOCALIZE
83  * };
84  * }
85  * }
86  * </pre>
87  * </blockquote>
88  * @see ResourceBundle
89  * @see PropertyResourceBundle
90  * @since JDK1.1
91  */

92 public abstract class ListResourceBundle extends ResourceBundle JavaDoc {
93     /**
94      * Sole constructor. (For invocation by subclass constructors, typically
95      * implicit.)
96      */

97     public ListResourceBundle() {
98     }
99
100     // Implements java.util.ResourceBundle.handleGetObject; inherits javadoc specification.
101
public final Object JavaDoc handleGetObject(String JavaDoc key) {
102         // lazily load the lookup hashtable.
103
if (lookup == null) {
104             loadLookup();
105         }
106         if (key == null) {
107             throw new NullPointerException JavaDoc();
108         }
109         return lookup.get(key); // this class ignores locales
110
}
111
112     /**
113      * Implementation of ResourceBundle.getKeys.
114      */

115     public Enumeration JavaDoc<String JavaDoc> getKeys() {
116         // lazily load the lookup hashtable.
117
if (lookup == null) {
118             loadLookup();
119         }
120         
121         ResourceBundle JavaDoc parent = this.parent;
122         return new ResourceBundleEnumeration JavaDoc(lookup.keySet(),
123                 (parent != null) ? parent.getKeys() : null);
124     }
125
126     /**
127      * See class description.
128      */

129     abstract protected Object JavaDoc[][] getContents();
130
131     // ==================privates====================
132

133     /**
134      * We lazily load the lookup hashtable. This function does the
135      * loading.
136      */

137     private synchronized void loadLookup() {
138         if (lookup != null)
139             return;
140
141         Object JavaDoc[][] contents = getContents();
142         HashMap JavaDoc temp = new HashMap JavaDoc(contents.length);
143         for (int i = 0; i < contents.length; ++i) {
144             // key must be non-null String, value must be non-null
145
String JavaDoc key = (String JavaDoc) contents[i][0];
146             Object JavaDoc value = contents[i][1];
147             if (key == null || value == null) {
148                 throw new NullPointerException JavaDoc();
149             }
150             temp.put(key, value);
151         }
152         lookup = temp;
153     }
154
155     private Map JavaDoc lookup = null;
156 }
157
Popular Tags