001: // ============================================================================
002: // $Id: EnumerationIterator.java,v 1.7 2005/08/02 23:45:22 davidahall Exp $
003: // Copyright (c) 2003-2005 David A. Hall
004: // ============================================================================
005: // The contents of this file are subject to the Common Development and
006: // Distribution License (CDDL), Version 1.0 (the License); you may not use this
007: // file except in compliance with the License. You should have received a copy
008: // of the the License along with this file: if not, a copy of the License is
009: // available from Sun Microsystems, Inc.
010: //
011: // http://www.sun.com/cddl/cddl.html
012: //
013: // From time to time, the license steward (initially Sun Microsystems, Inc.) may
014: // publish revised and/or new versions of the License. You may not use,
015: // distribute, or otherwise make this file available under subsequent versions
016: // of the License.
017: //
018: // Alternatively, the contents of this file may be used under the terms of the
019: // GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
020: // case the provisions of the LGPL are applicable instead of those above. If you
021: // wish to allow use of your version of this file only under the terms of the
022: // LGPL, and not to allow others to use your version of this file under the
023: // terms of the CDDL, indicate your decision by deleting the provisions above
024: // and replace them with the notice and other provisions required by the LGPL.
025: // If you do not delete the provisions above, a recipient may use your version
026: // of this file under the terms of either the CDDL or the LGPL.
027: //
028: // This library is distributed in the hope that it will be useful,
029: // but WITHOUT ANY WARRANTY; without even the implied warranty of
030: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
031: // ============================================================================
032:
033: package net.sf.jga.util;
034:
035: import java.util.Enumeration;
036: import java.util.NoSuchElementException;
037: import java.util.Iterator;
038:
039: /**
040: * Adapts an Enumeration to the Iterator and Iterable interfaces.
041: * <p>
042: * Copyright © 2003-2005 David A. Hall
043: *
044: * @author <a href="mailto:davidahall@users.sourceforge.net">David A. Hall</a>
045: **/
046:
047: public class EnumerationIterator<T> implements Iterator<T>, Iterable<T> {
048:
049: // The base enumeration (as if that wasn't self-evident)
050: private Enumeration<T> _base;
051:
052: /**
053: * Builds an EnumerationIterator that adapts the given enumeration to the
054: * Iterator and Iterable interfaces.
055: * @throws IllegalArgumentException if the given enumeration is null.
056: */
057:
058: public EnumerationIterator(Enumeration<T> enumeration) {
059: if (enumeration == null)
060: throw new IllegalArgumentException();
061:
062: _base = enumeration;
063: }
064:
065: // - - - - - - - - - - -
066: // Iterable<T> interface
067: // - - - - - - - - - - -
068:
069: public Iterator<T> iterator() {
070: return this ;
071: }
072:
073: // - - - - - - - - - - -
074: // Iterator<T> interface
075: // - - - - - - - - - - -
076:
077: /**
078: * Returns true if the base enumeration has elements remaining.
079: * @return true if the base enumeration has elements remaining.
080: */
081:
082: public boolean hasNext() {
083: return _base.hasMoreElements();
084: }
085:
086: /**
087: * Returns the next element in the base enumeration.
088: * @return the next element in the base enumeration.
089: * @throws NoSuchElementException if the base enumeration is at its end.
090: */
091:
092: public T next() {
093: return _base.nextElement();
094: }
095:
096: /**
097: * throws UnsupportedOperationException: Enumerations do not support the
098: * removal of elements.
099: * @throws UnsupportedOperationException when called.
100: */
101:
102: public void remove() throws UnsupportedOperationException {
103: throw new UnsupportedOperationException();
104: }
105: }
|