jdk/src/share/classes/java/util/ListResourceBundle.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 7506 bc1ab1b31622
child 7816 55a18147b4bf
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 7506
diff changeset
     2
 * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * The original version of this source code and documentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * is copyrighted and owned by Taligent, Inc., a wholly-owned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * subsidiary of IBM. These materials are provided under terms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * of a License Agreement between Taligent and Sun. This technology
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * is protected by multiple US and International patents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This notice and attribution to Taligent may not be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Taligent is a registered trademark of Taligent, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.util.ResourceBundleEnumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <code>ListResourceBundle</code> is an abstract subclass of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <code>ResourceBundle</code> that manages resources for a locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * in a convenient and easy to use list. See <code>ResourceBundle</code> for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * more information about resource bundles in general.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Subclasses must override <code>getContents</code> and provide an array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * where each item in the array is a pair of objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * The first element of each pair is the key, which must be a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <code>String</code>, and the second element is the value associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * that key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * The following <a name="sample">example</a> shows two members of a resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * bundle family with the base name "MyResources".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * "MyResources" is the default member of the bundle family, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * "MyResources_fr" is the French member.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * These members are based on <code>ListResourceBundle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * (a related <a href="PropertyResourceBundle.html#sample">example</a> shows
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * how you can add a bundle to this family that's based on a properties file).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * The keys in this example are of the form "s1" etc. The actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * keys are entirely up to your choice, so long as they are the same as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * the keys you use in your program to retrieve the objects from the bundle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * Keys are case-sensitive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * public class MyResources extends ListResourceBundle {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *     protected Object[][] getContents() {
7506
bc1ab1b31622 6647615: Sample code in ListResourceBundle is not correct and causes a compile error.
naoto
parents: 5506
diff changeset
    75
 *         return new Object[][] {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *         // LOCALIZE THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *             {"s1", "The disk \"{1}\" contains {0}."},  // MessageFormat pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *             {"s2", "1"},                               // location of {0} in pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *             {"s3", "My Disk"},                         // sample disk name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *             {"s4", "no files"},                        // first ChoiceFormat choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *             {"s5", "one file"},                        // second ChoiceFormat choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *             {"s6", "{0,number} files"},                // third ChoiceFormat choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *             {"s7", "3 Mar 96"},                        // sample date
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *             {"s8", new Dimension(1,5)}                 // real object, not just string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *         // END OF MATERIAL TO LOCALIZE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *         };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * public class MyResources_fr extends ListResourceBundle {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *     protected Object[][] getContents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *         return new Object[][] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *         // LOCALIZE THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *             {"s1", "Le disque \"{1}\" {0}."},          // MessageFormat pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *             {"s2", "1"},                               // location of {0} in pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *             {"s3", "Mon disque"},                      // sample disk name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *             {"s4", "ne contient pas de fichiers"},     // first ChoiceFormat choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *             {"s5", "contient un fichier"},             // second ChoiceFormat choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *             {"s6", "contient {0,number} fichiers"},    // third ChoiceFormat choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *             {"s7", "3 mars 1996"},                     // sample date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *             {"s8", new Dimension(1,3)}                 // real object, not just string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *         // END OF MATERIAL TO LOCALIZE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *         };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * @see ResourceBundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * @see PropertyResourceBundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
public abstract class ListResourceBundle extends ResourceBundle {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Sole constructor.  (For invocation by subclass constructors, typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * implicit.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public ListResourceBundle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    // Implements java.util.ResourceBundle.handleGetObject; inherits javadoc specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public final Object handleGetObject(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        // lazily load the lookup hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (lookup == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            loadLookup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return lookup.get(key); // this class ignores locales
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Returns an <code>Enumeration</code> of the keys contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * this <code>ResourceBundle</code> and its parent bundles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @return an <code>Enumeration</code> of the keys contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *         this <code>ResourceBundle</code> and its parent bundles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @see #keySet()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public Enumeration<String> getKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        // lazily load the lookup hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (lookup == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            loadLookup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        ResourceBundle parent = this.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return new ResourceBundleEnumeration(lookup.keySet(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                (parent != null) ? parent.getKeys() : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Returns a <code>Set</code> of the keys contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * <em>only</em> in this <code>ResourceBundle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @return a <code>Set</code> of the keys contained only in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *         <code>ResourceBundle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @see #keySet()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    protected Set<String> handleKeySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (lookup == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            loadLookup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return lookup.keySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Returns an array in which each item is a pair of objects in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * <code>Object</code> array. The first element of each pair is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * the key, which must be a <code>String</code>, and the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * element is the value associated with that key.  See the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * description for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @return an array of an <code>Object</code> array representing a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * key-value pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    abstract protected Object[][] getContents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    // ==================privates====================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * We lazily load the lookup hashtable.  This function does the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * loading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    private synchronized void loadLookup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (lookup != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        Object[][] contents = getContents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        HashMap<String,Object> temp = new HashMap<String,Object>(contents.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        for (int i = 0; i < contents.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            // key must be non-null String, value must be non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            String key = (String) contents[i][0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            Object value = contents[i][1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            if (key == null || value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            temp.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        lookup = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    private Map<String,Object> lookup = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
}