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
18package 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 */
28public 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    }
36
37    /**
38     * Returns an {@code Object} array containing the resources of this
39     * {@code ListResourceBundle}. Each element in the array is an array of two
40     * elements, the first is the resource key string and the second is the
41     * resource.
42     *
43     * @return a {@code Object} array containing the resources.
44     */
45    protected abstract Object[][] getContents();
46
47    @Override
48    public Enumeration<String> getKeys() {
49        initializeTable();
50        if (parent != null) {
51            return new Enumeration<String>() {
52                Iterator<String> local = table.keySet().iterator();
53
54                Enumeration<String> pEnum = parent.getKeys();
55
56                String nextElement;
57
58                private boolean findNext() {
59                    if (nextElement != null) {
60                        return true;
61                    }
62                    while (pEnum.hasMoreElements()) {
63                        String next = pEnum.nextElement();
64                        if (!table.containsKey(next)) {
65                            nextElement = next;
66                            return true;
67                        }
68                    }
69                    return false;
70                }
71
72                public boolean hasMoreElements() {
73                    if (local.hasNext()) {
74                        return true;
75                    }
76                    return findNext();
77                }
78
79                public String nextElement() {
80                    if (local.hasNext()) {
81                        return local.next();
82                    }
83                    if (findNext()) {
84                        String result = nextElement;
85                        nextElement = null;
86                        return result;
87                    }
88                    // Cause an exception
89                    return pEnum.nextElement();
90                }
91            };
92        } else {
93            return new Enumeration<String>() {
94                Iterator<String> it = table.keySet().iterator();
95
96                public boolean hasMoreElements() {
97                    return it.hasNext();
98                }
99
100                public String nextElement() {
101                    return it.next();
102                }
103            };
104        }
105    }
106
107    @Override
108    public final Object handleGetObject(String key) {
109        initializeTable();
110        if (key == null) {
111            throw new NullPointerException("key == null");
112        }
113        return table.get(key);
114    }
115
116    private synchronized void initializeTable() {
117        if (table == null) {
118            Object[][] contents = getContents();
119            table = new HashMap<String, Object>(contents.length / 3 * 4 + 3);
120            for (Object[] content : contents) {
121                if (content[0] == null || content[1] == null) {
122                    throw new NullPointerException();
123                }
124                table.put((String) content[0], content[1]);
125            }
126        }
127    }
128
129    /**
130     * Returns a set of the keys in this ResourceBundle but not in its parents.
131     *
132     * @return a set of the keys in this ResourceBundle but not in its parents.
133     * @since 1.6
134     */
135    protected Set<String> handleKeySet() {
136        initializeTable();
137        return table.keySet();
138    }
139}
140