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.language;
016    
017    import java.util.Enumeration;
018    import java.util.Iterator;
019    import java.util.NoSuchElementException;
020    import java.util.Set;
021    
022    /**
023     * @author Shuyang Zhou
024     */
025    public class ResourceBundleEnumeration implements Enumeration<String> {
026    
027            public ResourceBundleEnumeration(
028                    Set<String> set, Enumeration<String> enumeration) {
029    
030                    _set = set;
031                    _enumeration = enumeration;
032    
033                    _iterator = set.iterator();
034            }
035    
036            @Override
037            public boolean hasMoreElements() {
038                    if (_nextElement == null) {
039                            if (_iterator.hasNext()) {
040                                    _nextElement = _iterator.next();
041                            }
042                            else if (_enumeration != null) {
043                                    while ((_nextElement == null) &&
044                                               _enumeration.hasMoreElements()) {
045    
046                                            _nextElement = _enumeration.nextElement();
047    
048                                            if (_set.contains(_nextElement)) {
049                                                    _nextElement = null;
050                                            }
051                                    }
052                            }
053                    }
054    
055                    return _nextElement != null;
056            }
057    
058            @Override
059            public String nextElement() {
060                    if (hasMoreElements()) {
061                            String nextElement = _nextElement;
062    
063                            _nextElement = null;
064    
065                            return nextElement;
066                    }
067    
068                    throw new NoSuchElementException();
069            }
070    
071            private final Enumeration<String> _enumeration;
072            private final Iterator<String> _iterator;
073            private String _nextElement;
074            private final Set<String> _set;
075    
076    }