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