01 /*
02 * Copyright 2008-2014 the original author or authors.
03 *
04 * Licensed under the Apache License, Version 2.0 (the "License");
05 * you may not use this file except in compliance with the License.
06 * You may obtain a copy of the License at
07 *
08 * http://www.apache.org/licenses/LICENSE-2.0
09 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package griffon.util;
17
18 import javax.annotation.Nonnull;
19 import javax.annotation.Nullable;
20 import java.util.*;
21
22 import static griffon.util.ConfigUtils.collectKeys;
23 import static griffon.util.GriffonNameUtils.requireNonBlank;
24
25 /**
26 * @author Andres Almiray
27 * @since 2.0.0
28 */
29 public abstract class AbstractMapResourceBundle extends ResourceBundle {
30 private final Map<String, Object> entries = new LinkedHashMap<>();
31 private volatile Set<String> keys;
32
33 public AbstractMapResourceBundle() {
34 initialize(entries);
35 keys = collectKeys(entries);
36 // keys = new HashSet<>(entries.keySet());
37 }
38
39 protected abstract void initialize(@Nonnull Map<String, Object> entries);
40
41 @Nullable
42 @Override
43 protected final Object handleGetObject(@Nonnull String key) {
44 return entries.get(requireNonBlank(key, "Argument 'key' must not be blank"));
45 }
46
47 @Nonnull
48 @Override
49 public final Enumeration<String> getKeys() {
50 return new IteratorAsEnumeration<>(keys.iterator());
51 }
52
53 private static class IteratorAsEnumeration<E> implements Enumeration<E> {
54 private final Iterator<E> iterator;
55
56 public IteratorAsEnumeration(Iterator<E> iterator) {
57 this.iterator = iterator;
58 }
59
60 public boolean hasMoreElements() {
61 return iterator.hasNext();
62 }
63
64 public E nextElement() {
65 return iterator.next();
66 }
67 }
68
69 @Override
70 protected Set<String> handleKeySet() {
71 return keys;
72 }
73 }
|