1 /*
   2  * Copyright (c) 2005, 2007, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
  28  * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
  29  *
  30  * The original version of this source code and documentation
  31  * is copyrighted and owned by Taligent, Inc., a wholly-owned
  32  * subsidiary of IBM. These materials are provided under terms
  33  * of a License Agreement between Taligent and Sun. This technology
  34  * is protected by multiple US and International patents.
  35  *
  36  * This notice and attribution to Taligent may not be removed.
  37  * Taligent is a registered trademark of Taligent, Inc.
  38  *
  39  */
  40 
  41 package sun.util.resources;
  42 
  43 import java.util.Enumeration;
  44 import java.util.HashMap;
  45 import java.util.Map;
  46 import java.util.ResourceBundle;
  47 import java.util.Set;
  48 import sun.util.ResourceBundleEnumeration;
  49 
  50 /**
  51  * Subclass of <code>ResourceBundle</code> which mimics
  52  * <code>ListResourceBundle</code>, but provides more hooks
  53  * for specialized subclass behavior. For general description,
  54  * see {@link java.util.ListResourceBundle}.
  55  * <p>
  56  * This class leaves handleGetObject non-final, and
  57  * adds a method createMap which allows subclasses to
  58  * use specialized Map implementations.
  59  */
  60 public abstract class OpenListResourceBundle extends ResourceBundle {
  61     /**
  62      * Sole constructor.  (For invocation by subclass constructors, typically
  63      * implicit.)
  64      */
  65     protected OpenListResourceBundle() {
  66     }
  67 
  68     // Implements java.util.ResourceBundle.handleGetObject; inherits javadoc specification.
  69     public Object handleGetObject(String key) {
  70         if (key == null) {
  71             throw new NullPointerException();
  72         }
  73 
  74         loadLookupTablesIfNecessary();
  75         return lookup.get(key); // this class ignores locales
  76     }
  77 
  78     /**
  79      * Implementation of ResourceBundle.getKeys.
  80      */
  81     public Enumeration<String> getKeys() {
  82         ResourceBundle parent = this.parent;
  83         return new ResourceBundleEnumeration(handleGetKeys(),
  84                 (parent != null) ? parent.getKeys() : null);
  85     }
  86 
  87     /**
  88      * Returns a set of keys provided in this resource bundle
  89      */
  90     public Set<String> handleGetKeys() {
  91         loadLookupTablesIfNecessary();
  92 
  93         return lookup.keySet();
  94     }
  95 
  96     /**
  97      * Returns the parent bundle
  98      */
  99     public OpenListResourceBundle getParent() {
 100         return (OpenListResourceBundle)parent;
 101     }
 102 
 103     /**
 104      * See ListResourceBundle class description.
 105      */
 106     abstract protected Object[][] getContents();
 107 
 108     /**
 109      * Load lookup tables if they haven't been loaded already.
 110      */
 111     void loadLookupTablesIfNecessary() {
 112         if (lookup == null) {
 113             loadLookup();
 114         }
 115     }
 116 
 117     /**
 118      * We lazily load the lookup hashtable.  This function does the
 119      * loading.
 120      */
 121     private synchronized void loadLookup() {
 122         if (lookup != null) {
 123             return;
 124         }
 125 
 126         Object[][] contents = getContents();
 127         Map<String, Object> temp = createMap(contents.length);
 128         for (int i = 0; i < contents.length; ++i) {
 129             // key must be non-null String, value must be non-null
 130             String key = (String) contents[i][0];
 131             Object value = contents[i][1];
 132             if (key == null || value == null) {
 133                 throw new NullPointerException();
 134             }
 135             temp.put(key, value);
 136         }
 137         lookup = temp;
 138     }
 139 
 140     /**
 141      * Lets subclasses provide specialized Map implementations.
 142      * Default uses HashMap.
 143      */
 144     protected Map<String, Object> createMap(int size) {
 145         return new HashMap<>(size);
 146     }
 147 
 148     private Map<String, Object> lookup = null;
 149 }