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