CompositeResourceBundle.java
001 /*
002  * Copyright 2008-2016 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 org.slf4j.Logger;
019 import org.slf4j.LoggerFactory;
020 
021 import javax.annotation.Nonnull;
022 import javax.annotation.Nullable;
023 import java.util.ArrayList;
024 import java.util.Collection;
025 import java.util.Enumeration;
026 import java.util.Iterator;
027 import java.util.List;
028 import java.util.ResourceBundle;
029 
030 import static griffon.util.ExpandableResourceBundle.wrapResourceBundle;
031 import static griffon.util.GriffonClassUtils.requireState;
032 import static griffon.util.GriffonNameUtils.requireNonBlank;
033 import static java.util.Objects.requireNonNull;
034 
035 /**
036  @author Andres Almiray
037  @since 2.0.0
038  */
039 public class CompositeResourceBundle extends ResourceBundle {
040     private static final Logger LOG = LoggerFactory.getLogger(CompositeResourceBundle.class);
041     private final ResourceBundle[] bundles;
042     private final List<String> keys = new ArrayList<>();
043 
044     public CompositeResourceBundle(@Nonnull Collection<ResourceBundle> bundles) {
045         this(toResourceBundleArray(bundles));
046     }
047 
048     public CompositeResourceBundle(@Nonnull ResourceBundle[] bundles) {
049         requireNonNull(bundles, "Argument 'bundles' must not be null");
050         requireState(bundles.length > 0"Argument 'bundles' must not be empty");
051         this.bundles = new ResourceBundle[bundles.length];
052         for (int i = 0; i < bundles.length; i++) {
053             this.bundles[i= wrapResourceBundle(bundles[i]);
054         }
055 
056         for (ResourceBundle bundle : this.bundles) {
057             Enumeration<String> ks = bundle.getKeys();
058             while (ks.hasMoreElements()) {
059                 String key = ks.nextElement();
060                 if (!keys.contains(key)) {
061                     keys.add(key);
062                 }
063             }
064         }
065     }
066 
067     @Nullable
068     protected Object handleGetObject(@Nonnull String key) {
069         requireNonBlank(key, "Arguments 'key' must not be blank");
070 
071         LOG.trace("Searching key={}", key);
072         for (ResourceBundle bundle : bundles) {
073             try {
074                 Object value = bundle.getObject(key);
075                 LOG.trace("Bundle {}; key={}; value='{}'", bundle, key, value);
076                 if (value != null) {
077                     return value;
078                 }
079             catch (Exception e) {
080                 // ignore
081             }
082         }
083         return null;
084     }
085 
086     @Nonnull
087     @Override
088     public Enumeration<String> getKeys() {
089         return new IteratorAsEnumeration<>(keys.iterator());
090     }
091 
092     private static class IteratorAsEnumeration<E> implements Enumeration<E> {
093         private final Iterator<E> iterator;
094 
095         public IteratorAsEnumeration(Iterator<E> iterator) {
096             this.iterator = iterator;
097         }
098 
099         public boolean hasMoreElements() {
100             return iterator.hasNext();
101         }
102 
103         public E nextElement() {
104             return iterator.next();
105         }
106     }
107 
108     @Nonnull
109     private static ResourceBundle[] toResourceBundleArray(@Nonnull Collection<ResourceBundle> bundles) {
110         requireNonNull(bundles, "Argument 'bundles' must not be null");
111         if (bundles.isEmpty()) {
112             return new ResourceBundle[0];
113         }
114         return bundles.toArray(new ResourceBundle[bundles.size()]);
115     }
116 }