PropertiesResourceBundle.java
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 java.util.Enumeration;
22 import java.util.Iterator;
23 import java.util.LinkedHashMap;
24 import java.util.Map;
25 import java.util.NoSuchElementException;
26 import java.util.Properties;
27 import java.util.ResourceBundle;
28 import java.util.Set;
29 
30 import static griffon.util.GriffonNameUtils.requireNonBlank;
31 
32 /**
33  @author Andres Almiray
34  @since 2.10.0
35  */
36 public class PropertiesResourceBundle extends ResourceBundle {
37     private Map<String, Object> storage;
38 
39     @SuppressWarnings({"unchecked""rawtypes"})
40     public PropertiesResourceBundle(@Nonnull Properties properties) {
41         storage = new LinkedHashMap(properties);
42     }
43 
44     public Object handleGetObject(String key) {
45         requireNonBlank(key, "Argument 'key' must not be null");
46         return storage.get(key);
47     }
48 
49     public Enumeration<String> getKeys() {
50         ResourceBundle parent = this.parent;
51         final Enumeration<String> parentEnumeration = parent != null ? parent.getKeys() null;
52         final Set<String> keySet = storage.keySet();
53         final Iterator<String> iterator = keySet.iterator();
54 
55         return new Enumeration<String>() {
56             private String next = null;
57 
58             @Override
59             public boolean hasMoreElements() {
60                 if (this.next == null) {
61                     if (iterator.hasNext()) {
62                         this.next = iterator.next();
63                     else if (parentEnumeration != null) {
64                         while (this.next == null && parentEnumeration.hasMoreElements()) {
65                             this.next = parentEnumeration.nextElement();
66                             if (keySet.contains(this.next)) {
67                                 this.next = null;
68                             }
69                         }
70                     }
71                 }
72 
73                 return this.next != null;
74             }
75 
76             @Override
77             public String nextElement() {
78                 if (hasMoreElements()) {
79                     String key = this.next;
80                     this.next = null;
81                     return key;
82                 else {
83                     throw new NoSuchElementException();
84                 }
85             }
86         };
87     }
88 
89     @Override
90     protected Set<String> handleKeySet() {
91         return storage.keySet();
92     }
93 }