jdk/src/java.base/share/classes/java/util/ListResourceBundle.java
author mchung
Fri, 22 May 2015 16:43:39 -0700
changeset 30789 9eca83469588
parent 25859 3317bb8137f4
child 32649 2ee9017c7597
permissions -rw-r--r--
8074431: Remove native2ascii tool Reviewed-by: erikj, alanb, okutsu, mfang, naoto
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 22078
diff changeset
     2
 * Copyright (c) 1996, 2013, 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() {
17708
eccbcb1b38be 7056126: DateFormatSymbols documentation has incorrect description about DateFormat
naoto
parents: 7816
diff changeset
    92
 *         return new Object[][] {
2
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>
21949
d97fd8123cbe 8028368: There is no description whether or not java.util.ResourceBundle is thread-safe
naoto
parents: 17708
diff changeset
   108
 *
d97fd8123cbe 8028368: There is no description whether or not java.util.ResourceBundle is thread-safe
naoto
parents: 17708
diff changeset
   109
 * <p>
d97fd8123cbe 8028368: There is no description whether or not java.util.ResourceBundle is thread-safe
naoto
parents: 17708
diff changeset
   110
 * The implementation of a {@code ListResourceBundle} subclass must be thread-safe
d97fd8123cbe 8028368: There is no description whether or not java.util.ResourceBundle is thread-safe
naoto
parents: 17708
diff changeset
   111
 * if it's simultaneously used by multiple threads. The default implementations
d97fd8123cbe 8028368: There is no description whether or not java.util.ResourceBundle is thread-safe
naoto
parents: 17708
diff changeset
   112
 * of the methods in this class are thread-safe.
d97fd8123cbe 8028368: There is no description whether or not java.util.ResourceBundle is thread-safe
naoto
parents: 17708
diff changeset
   113
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * @see ResourceBundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * @see PropertyResourceBundle
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
   116
 * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
public abstract class ListResourceBundle extends ResourceBundle {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Sole constructor.  (For invocation by subclass constructors, typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * implicit.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public ListResourceBundle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    // Implements java.util.ResourceBundle.handleGetObject; inherits javadoc specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public final Object handleGetObject(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        // lazily load the lookup hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (lookup == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            loadLookup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return lookup.get(key); // this class ignores locales
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Returns an <code>Enumeration</code> of the keys contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * this <code>ResourceBundle</code> and its parent bundles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @return an <code>Enumeration</code> of the keys contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *         this <code>ResourceBundle</code> and its parent bundles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @see #keySet()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public Enumeration<String> getKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        // lazily load the lookup hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (lookup == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            loadLookup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        ResourceBundle parent = this.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return new ResourceBundleEnumeration(lookup.keySet(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                (parent != null) ? parent.getKeys() : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Returns a <code>Set</code> of the keys contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * <em>only</em> in this <code>ResourceBundle</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @return a <code>Set</code> of the keys contained only in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *         <code>ResourceBundle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @see #keySet()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    protected Set<String> handleKeySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (lookup == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            loadLookup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return lookup.keySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Returns an array in which each item is a pair of objects in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * <code>Object</code> array. The first element of each pair is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * the key, which must be a <code>String</code>, and the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * element is the value associated with that key.  See the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * description for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @return an array of an <code>Object</code> array representing a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * key-value pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    abstract protected Object[][] getContents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    // ==================privates====================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * We lazily load the lookup hashtable.  This function does the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * loading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private synchronized void loadLookup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (lookup != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        Object[][] contents = getContents();
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
   196
        HashMap<String,Object> temp = new HashMap<>(contents.length);
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21949
diff changeset
   197
        for (Object[] content : contents) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            // key must be non-null String, value must be non-null
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21949
diff changeset
   199
            String key = (String) content[0];
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21949
diff changeset
   200
            Object value = content[1];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            if (key == null || value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            temp.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        lookup = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    private Map<String,Object> lookup = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
}