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.MissingResourceException;
021    import java.util.ResourceBundle;
022    import java.util.Set;
023    
024    /**
025     * @author Carlos Sierra Andr??s
026     */
027    public class AggregateResourceBundle extends ResourceBundle {
028    
029            public AggregateResourceBundle(ResourceBundle... resourceBundles) {
030                    _resourceBundles = resourceBundles;
031            }
032    
033            @Override
034            public boolean containsKey(String key) {
035                    if (key == null) {
036                            throw new NullPointerException();
037                    }
038    
039                    for (ResourceBundle resourceBundle : _resourceBundles) {
040                            if (resourceBundle.containsKey(key)) {
041                                    return true;
042                            }
043                    }
044    
045                    return false;
046            }
047    
048            @Override
049            public Enumeration<String> getKeys() {
050                    return Collections.enumeration(handleKeySet());
051            }
052    
053            @Override
054            protected Object handleGetObject(String key) {
055                    if (key == null) {
056                            throw new NullPointerException();
057                    }
058    
059                    for (ResourceBundle resourceBundle : _resourceBundles) {
060                            if (!resourceBundle.containsKey(key)) {
061                                    continue;
062                            }
063    
064                            try {
065                                    return resourceBundle.getObject(key);
066                            }
067                            catch (MissingResourceException mre) {
068                            }
069                    }
070    
071                    return null;
072            }
073    
074            @Override
075            protected Set<String> handleKeySet() {
076                    if (_keys == null) {
077                            _keys = new HashSet<>();
078    
079                            for (ResourceBundle resourceBundle : _resourceBundles) {
080                                    _keys.addAll(resourceBundle.keySet());
081                            }
082                    }
083    
084                    return _keys;
085            }
086    
087            private Set<String> _keys;
088            private final ResourceBundle[] _resourceBundles;
089    
090    }