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