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                    if (_nextElement != null) {
056                            return true;
057                    }
058    
059                    return false;
060            }
061    
062            @Override
063            public String nextElement() {
064                    if (hasMoreElements()) {
065                            String nextElement = _nextElement;
066    
067                            _nextElement = null;
068    
069                            return nextElement;
070                    }
071    
072                    throw new NoSuchElementException();
073            }
074    
075            private final Enumeration<String> _enumeration;
076            private final Iterator<String> _iterator;
077            private String _nextElement;
078            private final Set<String> _set;
079    
080    }