001 /*
002 * Copyright 1996-2005 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 /*
027 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
028 * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
029 *
030 * The original version of this source code and documentation
031 * is copyrighted and owned by Taligent, Inc., a wholly-owned
032 * subsidiary of IBM. These materials are provided under terms
033 * of a License Agreement between Taligent and Sun. This technology
034 * is protected by multiple US and International patents.
035 *
036 * This notice and attribution to Taligent may not be removed.
037 * Taligent is a registered trademark of Taligent, Inc.
038 *
039 */
040
041 package java.util;
042
043 import sun.util.ResourceBundleEnumeration;
044
045 /**
046 * <code>ListResourceBundle</code> is an abstract subclass of
047 * <code>ResourceBundle</code> that manages resources for a locale
048 * in a convenient and easy to use list. See <code>ResourceBundle</code> for
049 * more information about resource bundles in general.
050 *
051 * <P>
052 * Subclasses must override <code>getContents</code> and provide an array,
053 * where each item in the array is a pair of objects.
054 * The first element of each pair is the key, which must be a
055 * <code>String</code>, and the second element is the value associated with
056 * that key.
057 *
058 * <p>
059 * The following <a name="sample">example</a> shows two members of a resource
060 * bundle family with the base name "MyResources".
061 * "MyResources" is the default member of the bundle family, and
062 * "MyResources_fr" is the French member.
063 * These members are based on <code>ListResourceBundle</code>
064 * (a related <a href="PropertyResourceBundle.html#sample">example</a> shows
065 * how you can add a bundle to this family that's based on a properties file).
066 * The keys in this example are of the form "s1" etc. The actual
067 * keys are entirely up to your choice, so long as they are the same as
068 * the keys you use in your program to retrieve the objects from the bundle.
069 * Keys are case-sensitive.
070 * <blockquote>
071 * <pre>
072 *
073 * public class MyResources extends ListResourceBundle {
074 * protected Object[][] getContents() {
075 * return new Object[][] = {
076 * // LOCALIZE THIS
077 * {"s1", "The disk \"{1}\" contains {0}."}, // MessageFormat pattern
078 * {"s2", "1"}, // location of {0} in pattern
079 * {"s3", "My Disk"}, // sample disk name
080 * {"s4", "no files"}, // first ChoiceFormat choice
081 * {"s5", "one file"}, // second ChoiceFormat choice
082 * {"s6", "{0,number} files"}, // third ChoiceFormat choice
083 * {"s7", "3 Mar 96"}, // sample date
084 * {"s8", new Dimension(1,5)} // real object, not just string
085 * // END OF MATERIAL TO LOCALIZE
086 * };
087 * }
088 * }
089 *
090 * public class MyResources_fr extends ListResourceBundle {
091 * protected Object[][] getContents() {
092 * return new Object[][] = {
093 * // LOCALIZE THIS
094 * {"s1", "Le disque \"{1}\" {0}."}, // MessageFormat pattern
095 * {"s2", "1"}, // location of {0} in pattern
096 * {"s3", "Mon disque"}, // sample disk name
097 * {"s4", "ne contient pas de fichiers"}, // first ChoiceFormat choice
098 * {"s5", "contient un fichier"}, // second ChoiceFormat choice
099 * {"s6", "contient {0,number} fichiers"}, // third ChoiceFormat choice
100 * {"s7", "3 mars 1996"}, // sample date
101 * {"s8", new Dimension(1,3)} // real object, not just string
102 * // END OF MATERIAL TO LOCALIZE
103 * };
104 * }
105 * }
106 * </pre>
107 * </blockquote>
108 * @see ResourceBundle
109 * @see PropertyResourceBundle
110 * @since JDK1.1
111 */
112 public abstract class ListResourceBundle extends ResourceBundle {
113 /**
114 * Sole constructor. (For invocation by subclass constructors, typically
115 * implicit.)
116 */
117 public ListResourceBundle() {
118 }
119
120 // Implements java.util.ResourceBundle.handleGetObject; inherits javadoc specification.
121 public final Object handleGetObject(String key) {
122 // lazily load the lookup hashtable.
123 if (lookup == null) {
124 loadLookup();
125 }
126 if (key == null) {
127 throw new NullPointerException();
128 }
129 return lookup.get(key); // this class ignores locales
130 }
131
132 /**
133 * Returns an <code>Enumeration</code> of the keys contained in
134 * this <code>ResourceBundle</code> and its parent bundles.
135 *
136 * @return an <code>Enumeration</code> of the keys contained in
137 * this <code>ResourceBundle</code> and its parent bundles.
138 * @see #keySet()
139 */
140 public Enumeration<String> getKeys() {
141 // lazily load the lookup hashtable.
142 if (lookup == null) {
143 loadLookup();
144 }
145
146 ResourceBundle parent = this .parent;
147 return new ResourceBundleEnumeration(lookup.keySet(),
148 (parent != null) ? parent.getKeys() : null);
149 }
150
151 /**
152 * Returns a <code>Set</code> of the keys contained
153 * <em>only</em> in this <code>ResourceBundle</code>.
154 *
155 * @return a <code>Set</code> of the keys contained only in this
156 * <code>ResourceBundle</code>
157 * @since 1.6
158 * @see #keySet()
159 */
160 protected Set<String> handleKeySet() {
161 if (lookup == null) {
162 loadLookup();
163 }
164 return lookup.keySet();
165 }
166
167 /**
168 * Returns an array in which each item is a pair of objects in an
169 * <code>Object</code> array. The first element of each pair is
170 * the key, which must be a <code>String</code>, and the second
171 * element is the value associated with that key. See the class
172 * description for details.
173 *
174 * @return an array of an <code>Object</code> array representing a
175 * key-value pair.
176 */
177 abstract protected Object[][] getContents();
178
179 // ==================privates====================
180
181 /**
182 * We lazily load the lookup hashtable. This function does the
183 * loading.
184 */
185 private synchronized void loadLookup() {
186 if (lookup != null)
187 return;
188
189 Object[][] contents = getContents();
190 HashMap<String, Object> temp = new HashMap<String, Object>(
191 contents.length);
192 for (int i = 0; i < contents.length; ++i) {
193 // key must be non-null String, value must be non-null
194 String key = (String) contents[i][0];
195 Object value = contents[i][1];
196 if (key == null || value == null) {
197 throw new NullPointerException();
198 }
199 temp.put(key, value);
200 }
201 lookup = temp;
202 }
203
204 private Map<String, Object> lookup = null;
205 }
|