Home | History | Annotate | Download | only in util
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 package java.util;
     19 
     20 /**
     21  * {@code ListResourceBundle} is the abstract superclass of classes which provide
     22  * resources by implementing the {@code getContents()} method to return
     23  * the list of resources.
     24  *
     25  * @see ResourceBundle
     26  * @since 1.1
     27  */
     28 public abstract class ListResourceBundle extends ResourceBundle {
     29     HashMap<String, Object> table;
     30 
     31     /**
     32      * Constructs a new instance of this class.
     33      */
     34     public ListResourceBundle() {
     35         super();
     36     }
     37 
     38     /**
     39      * Returns an {@code Object} array which contains the resources of this
     40      * {@code ListResourceBundle}. Each element in the array is an array of two
     41      * elements, the first is the resource key string and the second is the
     42      * resource.
     43      *
     44      * @return a {@code Object} array containing the resources.
     45      */
     46     protected abstract Object[][] getContents();
     47 
     48     /**
     49      * Returns the names of the resources contained in this {@code ListResourceBundle}.
     50      *
     51      * @return an {@code Enumeration} of the resource names.
     52      */
     53     @Override
     54     public Enumeration<String> getKeys() {
     55         initializeTable();
     56         if (parent != null) {
     57             return new Enumeration<String>() {
     58                 Iterator<String> local = table.keySet().iterator();
     59 
     60                 Enumeration<String> pEnum = parent.getKeys();
     61 
     62                 String nextElement;
     63 
     64                 private boolean findNext() {
     65                     if (nextElement != null) {
     66                         return true;
     67                     }
     68                     while (pEnum.hasMoreElements()) {
     69                         String next = pEnum.nextElement();
     70                         if (!table.containsKey(next)) {
     71                             nextElement = next;
     72                             return true;
     73                         }
     74                     }
     75                     return false;
     76                 }
     77 
     78                 public boolean hasMoreElements() {
     79                     if (local.hasNext()) {
     80                         return true;
     81                     }
     82                     return findNext();
     83                 }
     84 
     85                 public String nextElement() {
     86                     if (local.hasNext()) {
     87                         return local.next();
     88                     }
     89                     if (findNext()) {
     90                         String result = nextElement;
     91                         nextElement = null;
     92                         return result;
     93                     }
     94                     // Cause an exception
     95                     return pEnum.nextElement();
     96                 }
     97             };
     98         } else {
     99             return new Enumeration<String>() {
    100                 Iterator<String> it = table.keySet().iterator();
    101 
    102                 public boolean hasMoreElements() {
    103                     return it.hasNext();
    104                 }
    105 
    106                 public String nextElement() {
    107                     return it.next();
    108                 }
    109             };
    110         }
    111     }
    112 
    113     @Override
    114     public final Object handleGetObject(String key) {
    115         initializeTable();
    116         if (key == null) {
    117             throw new NullPointerException();
    118         }
    119         return table.get(key);
    120     }
    121 
    122     private synchronized void initializeTable() {
    123         if (table == null) {
    124             Object[][] contents = getContents();
    125             table = new HashMap<String, Object>(contents.length / 3 * 4 + 3);
    126             for (Object[] content : contents) {
    127                 if (content[0] == null || content[1] == null) {
    128                     throw new NullPointerException();
    129                 }
    130                 table.put((String) content[0], content[1]);
    131             }
    132         }
    133     }
    134 }
    135