001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.util.Collections;
018    import java.util.Enumeration;
019    import java.util.HashSet;
020    import java.util.Locale;
021    import java.util.MissingResourceException;
022    import java.util.ResourceBundle;
023    import java.util.Set;
024    
025    /**
026     * @author Carlos Sierra Andr??s
027     */
028    public class AggregateResourceBundle extends ResourceBundle {
029    
030            public AggregateResourceBundle(ResourceBundle... resourceBundles) {
031                    _resourceBundles = resourceBundles;
032            }
033    
034            @Override
035            public boolean containsKey(String key) {
036                    if (key == null) {
037                            throw new NullPointerException();
038                    }
039    
040                    for (ResourceBundle resourceBundle : _resourceBundles) {
041                            if (resourceBundle.containsKey(key)) {
042                                    return true;
043                            }
044                    }
045    
046                    return false;
047            }
048    
049            @Override
050            public Enumeration<String> getKeys() {
051                    return Collections.enumeration(handleKeySet());
052            }
053    
054            @Override
055            public Locale getLocale() {
056                    for (ResourceBundle resourceBundle : _resourceBundles) {
057                            Locale locale = resourceBundle.getLocale();
058    
059                            if (locale != null) {
060                                    return locale;
061                            }
062                    }
063    
064                    return super.getLocale();
065            }
066    
067            @Override
068            protected Object handleGetObject(String key) {
069                    if (key == null) {
070                            throw new NullPointerException();
071                    }
072    
073                    for (ResourceBundle resourceBundle : _resourceBundles) {
074                            if (!resourceBundle.containsKey(key)) {
075                                    continue;
076                            }
077    
078                            try {
079                                    return resourceBundle.getObject(key);
080                            }
081                            catch (MissingResourceException mre) {
082                            }
083                    }
084    
085                    return null;
086            }
087    
088            @Override
089            protected Set<String> handleKeySet() {
090                    if (_keys == null) {
091                            Set<String> keys = new HashSet<>();
092    
093                            for (ResourceBundle resourceBundle : _resourceBundles) {
094                                    keys.addAll(resourceBundle.keySet());
095                            }
096    
097                            _keys = keys;
098                    }
099    
100                    return _keys;
101            }
102    
103            private volatile Set<String> _keys;
104            private final ResourceBundle[] _resourceBundles;
105    
106    }