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            private static final long serialVersionUID = -4754357100171982705L;
032    
033            /** Iterate the set */
034        public Enumeration elements() {
035            return new Enumeration() {
036                Iterator i=iterator();
037                public boolean hasMoreElements() {
038                    return i.hasNext();
039                }
040                public Object nextElement() {
041                    return i.next();
042                }
043            };
044            
045        }
046        /** Add an element into the set */
047        public void addElement(Object o) {
048            add(o);
049        }
050        /** Remove an element from the set */
051        public boolean removeElement(Object o) {
052            return remove(o);
053        }
054        /** First element in the set (first using {@link HashSet#iterator()}) */
055        public Object firstElement() {
056            return (isEmpty()?null:elements().nextElement());
057        }
058    }