001    package net.sf.cpsolver.ifs.util;
002    
003    import java.util.Enumeration;
004    import java.util.HashSet;
005    import java.util.Iterator;
006    
007    /** 
008     * An extension of {@link HashSet} that implements {@link EnumerableCollection} interface.
009     *
010     * @version
011     * IFS 1.1 (Iterative Forward Search)<br>
012     * Copyright (C) 2006 Tomáš Müller<br>
013     * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
014     * Lazenska 391, 76314 Zlin, Czech Republic<br>
015     * <br>
016     * This library is free software; you can redistribute it and/or
017     * modify it under the terms of the GNU Lesser General Public
018     * License as published by the Free Software Foundation; either
019     * version 2.1 of the License, or (at your option) any later version.
020     * <br><br>
021     * This library is distributed in the hope that it will be useful,
022     * but WITHOUT ANY WARRANTY; without even the implied warranty of
023     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
024     * Lesser General Public License for more details.
025     * <br><br>
026     * You should have received a copy of the GNU Lesser General Public
027     * License along with this library; if not, write to the Free Software
028     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
029     */
030    public class EnumerableHashSet extends HashSet implements EnumerableCollection {
031        /** Iterate the set */
032        public Enumeration elements() {
033            return new Enumeration() {
034                Iterator i=iterator();
035                public boolean hasMoreElements() {
036                    return i.hasNext();
037                }
038                public Object nextElement() {
039                    return i.next();
040                }
041            };
042            
043        }
044        /** Add an element into the set */
045        public void addElement(Object o) {
046            add(o);
047        }
048        /** Remove an element from the set */
049        public boolean removeElement(Object o) {
050            return remove(o);
051        }
052        /** First element in the set (first using {@link HashSet#iterator()}) */
053        public Object firstElement() {
054            return (isEmpty()?null:elements().nextElement());
055        }
056    }