001 /*
002 * Copyright 2008-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package griffon.util;
017
018 import javax.annotation.Nonnull;
019 import javax.annotation.Nullable;
020 import java.util.*;
021
022 import static griffon.util.ConfigUtils.getConfigValue;
023 import static griffon.util.GriffonNameUtils.requireNonBlank;
024 import static java.util.Objects.requireNonNull;
025
026 /**
027 * @author Andres Almiray
028 * @since 2.0.0
029 */
030 public class ExpandableResourceBundle extends ResourceBundle {
031 private final Map<String, Object> entries = new LinkedHashMap<>();
032
033 @Nonnull
034 public static ResourceBundle wrapResourceBundle(@Nonnull ResourceBundle resourceBundle) {
035 requireNonNull(resourceBundle, "Argument 'resourceBundle' must not be null");
036 if (!(resourceBundle instanceof ExpandableResourceBundle)) {
037 return new ExpandableResourceBundle(resourceBundle);
038 }
039 return resourceBundle;
040 }
041
042 public ExpandableResourceBundle(@Nonnull ResourceBundle delegate) {
043 requireNonNull(delegate, "Argument 'delegate' must not be null");
044 for (String key : delegate.keySet()) {
045 Object value = getConfigValue(delegate, key);
046 processKey(key, entries, value);
047 entries.put(key, value);
048 }
049 }
050
051 @SuppressWarnings("unchecked")
052 private void processKey(@Nonnull String key, @Nonnull Map<String, Object> map, @Nullable Object value) {
053 String[] keys = split(key);
054 if (keys[1] == null) {
055 map.put(keys[0], value);
056 } else {
057 Map<String, Object> m = (Map<String, Object>) map.get(keys[0]);
058 if (m == null) {
059 m = new LinkedHashMap<>();
060 map.put(keys[0], m);
061 }
062 processKey(keys[1], m, value);
063 }
064 }
065
066 @Nonnull
067 private String[] split(@Nonnull String input) {
068 int split = input.indexOf(".");
069 String head = split < 0 ? input : input.substring(0, split);
070 String tail = split > 0 ? input.substring(split + 1) : null;
071 return new String[]{head, tail};
072 }
073
074 @Nullable
075 @Override
076 protected final Object handleGetObject(@Nonnull String key) {
077 return entries.get(requireNonBlank(key, "Argument 'key' must not be blank"));
078 }
079
080 @Nonnull
081 @Override
082 public final Enumeration<String> getKeys() {
083 return new IteratorAsEnumeration<>(entries.keySet().iterator());
084 }
085
086 private static class IteratorAsEnumeration<E> implements Enumeration<E> {
087 private final Iterator<E> iterator;
088
089 public IteratorAsEnumeration(Iterator<E> iterator) {
090 this.iterator = iterator;
091 }
092
093 public boolean hasMoreElements() {
094 return iterator.hasNext();
095 }
096
097 public E nextElement() {
098 return iterator.next();
099 }
100 }
101 }
|