jstrachan    01/08/29 08:48:42

  Modified:    collections/src/java/org/apache/commons/collections
                        EnumerationIterator.java
  Log:
  Added Daniel Rall's patch to support remove() from the EnuemerationIterator
  
  Revision  Changes    Path
  1.2       +85 -53    
jakarta-commons/collections/src/java/org/apache/commons/collections/EnumerationIterator.java
  
  Index: EnumerationIterator.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/EnumerationIterator.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- EnumerationIterator.java  2001/05/06 11:04:25     1.1
  +++ EnumerationIterator.java  2001/08/29 15:48:42     1.2
  @@ -1,53 +1,85 @@
  -/*
  - * Copyright (C) The Apache Software Foundation. All rights reserved.
  - *
  - * This software is published under the terms of the Apache Software License
  - * version 1.1, a copy of which has been included with this distribution in
  - * the LICENSE file.
  - */
  -package org.apache.commons.collections;
  -
  -import java.util.Enumeration;
  -import java.util.Iterator;
  -
  -/** Adapter to make {@link Enumeration Enumeration} instances appear to be {@link 
Iterator Iterator} instances
  -  *
  -  * @author <a href="mailto:[EMAIL PROTECTED]";>James Strachan</a>
  -  */
  -
  -public class EnumerationIterator implements Iterator {
  -    
  -    private Enumeration enumeration;
  -
  -    
  -    public EnumerationIterator() {
  -    }
  -    
  -    public EnumerationIterator( Enumeration enumeration ) {
  -        this.enumeration = enumeration;
  -    }
  -
  -    // Iterator interface
  -    //-------------------------------------------------------------------------
  -    public boolean hasNext() {
  -        return enumeration.hasMoreElements();
  -    }
  -
  -    public Object next() {
  -        return enumeration.nextElement();
  -    }
  -
  -    public void remove() {
  -        throw new UnsupportedOperationException( "remove() method is not supported" 
);
  -    }
  -
  -    // Properties
  -    //-------------------------------------------------------------------------
  -    public Enumeration getEnumeration() {
  -        return enumeration;
  -    }
  -    
  -    public void setEnumeration( Enumeration enumeration ) {
  -        this.enumeration = enumeration;
  -    }
  -}
  +/*
  + * Copyright (C) The Apache Software Foundation. All rights reserved.
  + *
  + * This software is published under the terms of the Apache Software License
  + * version 1.1, a copy of which has been included with this distribution in
  + * the LICENSE file.
  + */
  +package org.apache.commons.collections;
  +
  +import java.util.Collection;
  +import java.util.Enumeration;
  +import java.util.Iterator;
  +
  +/** Adapter to make {@link Enumeration Enumeration} instances appear
  +  * to be {@link Iterator Iterator} instances.
  +  *
  +  * @author <a href="mailto:[EMAIL PROTECTED]";>James Strachan</a>
  +  * @author <a href="mailto:[EMAIL PROTECTED]";>Daniel Rall</a>
  +  */
  +public class EnumerationIterator implements Iterator {
  +    
  +    private Collection collection;
  +
  +    private Enumeration enumeration;
  +
  +    private Object last;
  +    
  +    public EnumerationIterator() {
  +        this(null, null);
  +    }
  +    
  +    public EnumerationIterator( Enumeration enumeration ) {
  +        this(enumeration, null);
  +    }
  +
  +    public EnumerationIterator( Enumeration enum, Collection collection ) {
  +        this.enumeration = enum;
  +        this.collection = collection;
  +        this.last = null;
  +    }
  +
  +    // Iterator interface
  +    //-------------------------------------------------------------------------
  +    public boolean hasNext() {
  +        return enumeration.hasMoreElements();
  +    }
  +
  +    public Object next() {
  +        last = enumeration.nextElement();
  +        return last;
  +    }
  +
  +    /**
  +     * Functions if an associated <code>Collection</code> is known.
  +     *
  +     * @exception IllegalStateException <code>next()</code> not called.
  +     * @exception UnsupportedOperationException No associated
  +     * <code>Collection</code>.
  +     */
  +    public void remove() {
  +        if (collection != null) {
  +            if (last != null) {
  +                collection.remove(last);
  +            }
  +            else {
  +                throw new IllegalStateException
  +                    ("next() must have been called for remove() to function");
  +            }
  +        }
  +        else {
  +            throw new UnsupportedOperationException
  +                ("No Collection associated with this Iterator");
  +        }
  +    }
  +
  +    // Properties
  +    //-------------------------------------------------------------------------
  +    public Enumeration getEnumeration() {
  +        return enumeration;
  +    }
  +    
  +    public void setEnumeration( Enumeration enumeration ) {
  +        this.enumeration = enumeration;
  +    }
  +}
  
  
  

Reply via email to