Java/Collections Data Structure/Enumerator
Содержание
- 1 A GOF Adapter to make instances of old Enumeration interface behave like new Iterator interface
- 2 A more robust enumeration system
- 3 An enumeration that iterates over an array.
- 4 Concatenates the content of two enumerations into one.
- 5 Empty Enumeration
- 6 Enumeration interface which enumerates the items of an array
- 7 Filtering Enumeration
- 8 Filters some elements out from the input enumeration.
- 9 For each element of the input enumeration asks the Processor to provide a replacement
- 10 ListOfFiles implements Enumeration<FileInputStream>
- 11 Removes all nulls from the input enumeration.
- 12 Single Item Enumeration
- 13 Support for breadth-first enumerating.
- 14 Treat an Enumeration as an Iterable
- 15 Wrapping an Iterator around an Enumeration
A GOF Adapter to make instances of old Enumeration interface behave like new Iterator interface
/*
* Copyright (c) Ian F. Darwin, http://www.darwinsys.ru/, 1996-2002.
* All rights reserved. Software written by Ian F. Darwin and others.
* $Id: LICENSE,v 1.8 2004/02/09 03:33:38 ian Exp $
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS""
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Java, the Duke mascot, and all variants of Sun"s Java "steaming coffee
* cup" logo are trademarks of Sun Microsystems. Sun"s, and James Gosling"s,
* pioneering role in inventing and promulgating (and standardizing) the Java
* language and environment is gratefully acknowledged.
*
* The pioneering role of Dennis Ritchie and Bjarne Stroustrup, of AT&T, for
* inventing predecessor languages C and C++ is also gratefully acknowledged.
*/
import java.util.Enumeration;
import java.util.Iterator;
/**
* A GOF Adapter to make instances of old Enumeration interface behave like new
* Iterator interface, so we only have to deal with one well-defined
* implementation of the Iterator pattern.
*/
public class EnumerationIterator implements Iterator {
/** The Enumeration being delegated to */
private final Enumeration oldEnum;
/**
* Construct an EnumerationIterator from an old-style Enumeration.
*
* @param old
* The Enumeration to be adapted.
*/
public EnumerationIterator(final Enumeration old) {
oldEnum = old;
}
/**
* Fulfuls the general contract of Iterator.hasNext(), that is, return true
* as long as there is at least one more item in the Iterator.
*/
public boolean hasNext() {
return oldEnum.hasMoreElements();
}
/**
* Fulfuls the general contract of Iterator.next(), that is, returns the
* next element in the Iterator.
*/
public Object next() {
return oldEnum.nextElement();
}
/**
* Remove is not implemented (optional method).
*
* @throws java.lang.UnsupportedOperationException
* in all cases.
*/
public void remove() {
throw new UnsupportedOperationException("remove");
}
}
A more robust enumeration system
// : c08:Month.java
// A more robust enumeration system.
// From "Thinking in Java, 3rd ed." (c) Bruce Eckel 2002
// www.BruceEckel.ru. See copyright notice in CopyRight.txt.
public class Month {
private String name;
private Month(String nm) {
name = nm;
}
public String toString() {
return name;
}
public static final Month JAN = new Month("January"), FEB = new Month(
"February"), MAR = new Month("March"), APR = new Month("April"),
MAY = new Month("May"), JUN = new Month("June"), JUL = new Month(
"July"), AUG = new Month("August"), SEP = new Month(
"September"), OCT = new Month("October"), NOV = new Month(
"November"), DEC = new Month("December");
public static final Month[] month = { JAN, FEB, MAR, APR, MAY, JUN, JUL,
AUG, SEP, OCT, NOV, DEC };
public static final Month number(int ord) {
return month[ord - 1];
}
public static void main(String[] args) {
Month m = Month.JAN;
System.out.println(m);
m = Month.number(12);
System.out.println(m);
System.out.println(m == Month.DEC);
System.out.println(m.equals(Month.DEC));
System.out.println(Month.month[3]);
}
} ///:~
An enumeration that iterates over an array.
/**
*
* JFreeReport : a free Java reporting library
*
*
* Project Info: http://reporting.pentaho.org/
*
* (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ------------
* ArrayEnumeration.java
* ------------
* (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
*/
import java.util.Enumeration;
import java.util.NoSuchElementException;
/**
* An enumeration that iterates over an array.
*
* @author Thomas Morgner
*/
public class ArrayEnumeration implements Enumeration
{
/** The base datasource. */
private Object[] objectarray = null;
/** The counter holds the current position inside the array. */
private int counter = 0;
/**
* Creates a new enumeration for the given array.
*
* @param objectarray the array over which to iterate
* @throws NullPointerException if the array is null.
*/
public ArrayEnumeration(final Object[] objectarray)
{
if (objectarray == null)
{
throw new NullPointerException("The array must not be null.");
}
this.objectarray = objectarray;
}
/**
* Returns true if this enumeration has at least one more Element.
*
* @return true, if there are more elements, false otherwise.
*/
public boolean hasMoreElements()
{
return (counter < objectarray.length);
}
/**
* Returns the next element in the Array.
*
* @return the next element in the array.
* @throws NoSuchElementException if no more elements exist.
*/
public Object nextElement()
{
if (counter >= objectarray.length)
{
throw new NoSuchElementException();
}
final Object retval = objectarray[counter];
counter += 1;
return retval;
}
}
Concatenates the content of two enumerations into one.
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License"). You may not use this file except in compliance with the
* License. You can obtain a copy of the License at
* http://www.netbeans.org/cddl-gplv2.html
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
* specific language governing permissions and limitations under the
* License. When distributing the software, include this License Header
* Notice in each file and include the License file at
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the
* License Header, with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Contributor(s):
*
* The Original Software is NetBeans. The Initial Developer of the Original
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
* Microsystems, Inc. All Rights Reserved.
*
* If you wish your version of this file to be governed by only the CDDL
* or only the GPL Version 2, indicate your decision by adding
* "[Contributor] elects to include this software in this distribution
* under the [CDDL or GPL Version 2] license." If you do not indicate a
* single choice of license, a recipient has the option to distribute
* your version of this file under either the CDDL, the GPL Version 2 or
* to extend the choice of license to its licensees as provided above.
* However, if you add GPL Version 2 code and therefore, elected the GPL
* Version 2 license, then the option applies only if the new code is
* made subject to such option by the copyright holder.
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* @since 4.37
* @author Jaroslav Tulach
*/
public class Utils {
/**
* Concatenates the content of two enumerations into one.
* Until the
* end of <code>en1</code> is reached its elements are being served.
* As soon as the <code>en1</code> has no more elements, the content
* of <code>en2</code> is being returned.
*
* @param en1 first enumeration
* @param en2 second enumeration
* @return enumeration
*/
public static <T> Enumeration<T> concat(Enumeration<? extends T> en1, Enumeration<? extends T> en2) {
ArrayList<Enumeration<? extends T>> two = new ArrayList<Enumeration<? extends T>>();
two.add(en1);
two.add(en2);
return new SeqEn<T>(Collections.enumeration(two));
}
}
class SeqEn<T> extends Object implements Enumeration<T> {
/** enumeration of Enumerations */
private Enumeration<? extends Enumeration<? extends T>> en;
/** current enumeration */
private Enumeration<? extends T> current;
/** is {@link #current} up-to-date and has more elements?
* The combination <CODE>current == null</CODE> and
* <CODE>checked == true means there are no more elements
* in this enumeration.
*/
private boolean checked = false;
/** Constructs new enumeration from already existing. The elements
* of <CODE>en</CODE> should be also enumerations. The resulting
* enumeration contains elements of such enumerations.
*
* @param en enumeration of Enumerations that should be sequenced
*/
public SeqEn(Enumeration<? extends Enumeration <? extends T>> en) {
this.en = en;
}
/** Ensures that current enumeration is set. If there aren"t more
* elements in the Enumerations, sets the field <CODE>current</CODE> to null.
*/
private void ensureCurrent() {
while ((current == null) || !current.hasMoreElements()) {
if (en.hasMoreElements()) {
current = en.nextElement();
} else {
// no next valid enumeration
current = null;
return;
}
}
}
/** @return true if we have more elements */
public boolean hasMoreElements() {
if (!checked) {
ensureCurrent();
checked = true;
}
return current != null;
}
/** @return next element
* @exception NoSuchElementException if there is no next element
*/
public T nextElement() {
if (!checked) {
ensureCurrent();
}
if (current != null) {
checked = false;
return current.nextElement();
} else {
checked = true;
throw new java.util.NoSuchElementException();
}
}
}
Empty Enumeration
/*
* Copyright Javelin Software, All rights reserved.
*/
import java.util.*;
/**
* An EmptyEnumeration.
*
* @author Robin Sharp
*/
public class EmptyEnumeration implements Enumeration
{
public static Enumeration getInstance()
{
return enumeration;
}
/**
* @return false;
*/
public boolean hasMoreElements()
{
return false;
}
/**
* @return null
*/
public Object nextElement()
{
return null;
}
protected static Enumeration enumeration = new EmptyEnumeration();
}
Enumeration interface which enumerates the items of an array
/**
* The utillib library.
* More information is available at http://www.jinchess.ru/.
* Copyright (C) 2002 Alexander Maryanovsky.
* All rights reserved.
*
* The utillib library is free software; you can redistribute
* it and/or modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* The utillib library is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with utillib library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
import java.util.Enumeration;
import java.util.NoSuchElementException;
/**
* An implementation of the Enumeration interface which enumerates the items of
* an array. Note: This class is not thread safe.
*/
public class ArrayEnumeration implements Enumeration{
/**
* The array.
*/
private Object [] arr;
/**
* The index of the first enumerated item.
*/
private final int offset;
/**
* The amount of the enumerated items.
*/
private final int count;
/**
* The index of the next returned item.
*/
private int curIndex;
/**
* Creates a new ArrayEnumeration which enumerates the items of the given
* array. The first <code>count</code> items starting at index
* <code>offset</code> are enumerated.
*
* @throws IllegalArgumentException if the offset and/or count parameters are
* invalid.
*/
public ArrayEnumeration(Object [] arr, int offset, int count){
if ((offset < 0) || (offset + count > arr.length) || (count < 0))
throw new IllegalArgumentException("Invalid enumeration range");
this.arr = new Object[arr.length];
System.arraycopy(arr, 0, this.arr, 0, arr.length);
this.offset = offset;
this.count = count;
curIndex = offset;
}
/**
* Creates a new ArrayEnumeration which enumerates all the items of the given
* array.
*/
public ArrayEnumeration(Object [] arr){
this(arr, 0, arr.length);
}
/**
* Returns the next element in the enumeration.
*/
public Object nextElement(){
if (!hasMoreElements())
throw new NoSuchElementException();
Object item = arr[curIndex];
arr[curIndex++] = null; // We don"t want to keep a reference to it any longer than we have to.
if (!hasMoreElements())
arr = null; // Neither do we need this any more.
return item;
}
/**
* Returns true if there are more elements in the enumeration.
*/
public boolean hasMoreElements(){
return curIndex < offset + count;
}
}
Filtering Enumeration
/**
* The utillib library.
* More information is available at http://www.jinchess.ru/.
* Copyright (C) 2003 Alexander Maryanovsky.
* All rights reserved.
*
* The utillib library is free software; you can redistribute
* it and/or modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* The utillib library is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with utillib library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
import java.util.Enumeration;
import java.util.NoSuchElementException;
/**
* An implementation of the <code>Enumeration</code> interface which delegates
* to another <code>Enumeration</code>, but only returns elements which pass
* the {@link #accept(Object)} method.
*/
public abstract class FilteringEnumeration implements Enumeration{
/**
* The delegate enumeration.
*/
private final Enumeration delegate;
/**
* The next element we"ll return. This is set by the <code>findNext</code>
* method.
*/
private Object next = null;
/**
* Creates a new <code>FilteringEnumeration</code> object with the specified
* delegate.
*/
public FilteringEnumeration(Enumeration delegate){
this.delegate = delegate;
}
/**
* Finds the next element in the delegate enumeration which passes
* <code>accept</code> and puts it in <code>next</code>.
*/
private void findNext(){
if (next != null)
return;
while (delegate.hasMoreElements()){
Object element = delegate.nextElement();
if (accept(element)){
next = element;
break;
}
}
}
/**
* Returns whether there are more elements in this <code>Enumeration</code>.
*/
public boolean hasMoreElements(){
findNext();
return next != null;
}
/**
* Returns the next element in the delegate enumeration which passes the
* <code>accept</code> method.
*/
public Object nextElement() throws NoSuchElementException{
findNext();
if (next == null)
throw new NoSuchElementException();
Object result = next;
next = null;
return result;
}
/**
* Returns whether the specified object passes the filter.
*/
public abstract boolean accept(Object element);
}
Filters some elements out from the input enumeration.
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License"). You may not use this file except in compliance with the
* License. You can obtain a copy of the License at
* http://www.netbeans.org/cddl-gplv2.html
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
* specific language governing permissions and limitations under the
* License. When distributing the software, include this License Header
* Notice in each file and include the License file at
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the
* License Header, with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Contributor(s):
*
* The Original Software is NetBeans. The Initial Developer of the Original
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
* Microsystems, Inc. All Rights Reserved.
*
* If you wish your version of this file to be governed by only the CDDL
* or only the GPL Version 2, indicate your decision by adding
* "[Contributor] elects to include this software in this distribution
* under the [CDDL or GPL Version 2] license." If you do not indicate a
* single choice of license, a recipient has the option to distribute
* your version of this file under either the CDDL, the GPL Version 2 or
* to extend the choice of license to its licensees as provided above.
* However, if you add GPL Version 2 code and therefore, elected the GPL
* Version 2 license, then the option applies only if the new code is
* made subject to such option by the copyright holder.
*/
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;
/**
* @since 4.37
* @author Jaroslav Tulach
*/
final class Enumerations extends Object {
/**
* Filters some elements out from the input enumeration.
* Just make the
* {@link Processor} return <code>null</code>. Please notice the <code>toAdd</code>
* argument of the processor is always <code>null</code>.
* <p>
* Example to remove all objects that are not strings:
* <pre>
* Processor onlyString = new Processor() {
* public Object process(Object obj, Collection alwaysNull) {
* if (obj instanceof String) {
* return obj;
* } else {
* return null;
* }
* }
* };
* Enumeration strings = Enumerations.filter(elems, onlyString);
* </pre>
*
* @param en enumeration of any objects
* @param filter a callback processor for the elements (its toAdd arguments is always null)
* @return new enumeration which does not include non-processed (returned null from processor) elements
* @see NbCollections#checkedEnumerationByFilter
*/
public static <T,R> Enumeration<R> filter(Enumeration<? extends T> en, Processor<T,R> filter) {
return new FilEn<T,R>(en, filter);
}
/**
* Support for breadth-first enumerating.
* Before any element is returned
* for the resulting enumeration it is processed in the {@link Processor} and
* the processor is allowed to modify it and also add additional elements
* at the (current) end of the <q>queue</q> by calling <code>toAdd.add</code>
* or <code>toAdd.addAll</code>. No other methods can be called on the
* provided <code>toAdd</code> collection.
* <p>
* Example of doing breadth-first walk through a tree:
* <pre>
* Processor queueSubnodes = new Processor() {
* public Object process(Object obj, Collection toAdd) {
* Node n = (Node)obj;
* toAdd.addAll (n.getChildrenList());
* return n;
* }
* };
* Enumeration strings = Enumerations.queue(elems, queueSubnodes);
* </pre>
*
* @param en initial content of the resulting enumeration
* @param filter the processor that is called for each element and can
* add and addAll elements to its toAdd Collection argument and
* also change the value to be returned
* @return enumeration with the initial and queued content (it can contain
* <code>null</code> if the filter returned <code>null</code> from its
* {@link Processor#process} method.
*/
public static <T,R> Enumeration<R> queue(Enumeration<? extends T> en, Processor<T,R> filter) {
QEn<T,R> q = new QEn<T,R>(filter);
while (en.hasMoreElements()) {
q.put(en.nextElement());
}
return q;
}
/**
* Processor interface that can filter out objects from the enumeration,
* change them or add aditional objects to the end of the current enumeration.
*/
public static interface Processor<T,R> {
/** @param original the object that is going to be returned from the enumeration right now
* @return a replacement for this object
* @param toAdd can be non-null if one can add new objects at the end of the enumeration
*/
public R process(T original, Collection<T> toAdd);
}
/** Altering enumeration implementation */
private static final class AltEn<T,R> extends Object implements Enumeration<R> {
/** enumeration to filter */
private Enumeration<? extends T> en;
/** map to alter */
private Processor<T,R> process;
/**
* @param en enumeration to filter
*/
public AltEn(Enumeration<? extends T> en, Processor<T,R> process) {
this.en = en;
this.process = process;
}
/** @return true if there is more elements in the enumeration
*/
public boolean hasMoreElements() {
return en.hasMoreElements();
}
/** @return next object in the enumeration
* @exception NoSuchElementException can be thrown if there is no next object
* in the enumeration
*/
public R nextElement() {
return process.process(en.nextElement(), null);
}
}
// end of AltEn
/** QueueEnumeration
*/
private static class QEn<T,R> extends Object implements Enumeration<R> {
/** next object to be returned */
private ListItem<T> next = null;
/** last object in the queue */
private ListItem<T> last = null;
/** processor to use */
private Processor<T,R> processor;
public QEn(Processor<T,R> p) {
this.processor = p;
}
/** Put adds new object to the end of queue.
* @param o the object to add
*/
public void put(T o) {
if (last != null) {
ListItem<T> li = new ListItem<T>(o);
last.next = li;
last = li;
} else {
next = last = new ListItem<T>(o);
}
}
/** Adds array of objects into the queue.
* @param arr array of objects to put into the queue
*/
public void put(Collection<? extends T> arr) {
for (T e : arr) {
put(e);
}
}
/** Is there any next object?
* @return true if there is next object, false otherwise
*/
public boolean hasMoreElements() {
return next != null;
}
/** @return next object in enumeration
* @exception NoSuchElementException if there is no next object
*/
public R nextElement() {
if (next == null) {
throw new NoSuchElementException();
}
T res = next.object;
if ((next = next.next) == null) {
last = null;
}
;
ToAdd<T,R> toAdd = new ToAdd<T,R>(this);
R out = processor.process(res, toAdd);
toAdd.finish();
return out;
}
/** item in linked list of Objects */
private static final class ListItem<T> {
T object;
ListItem<T> next;
/** @param o the object for this item */
ListItem(T o) {
object = o;
}
}
/** Temporary collection that supports only add and addAll operations*/
private static final class ToAdd<T,R> extends Object implements Collection<T> {
private QEn<T,R> q;
public ToAdd(QEn<T,R> q) {
this.q = q;
}
public void finish() {
this.q = null;
}
public boolean add(T o) {
q.put(o);
return true;
}
public boolean addAll(Collection<? extends T> c) {
q.put(c);
return true;
}
private String msg() {
return "Only add and addAll are implemented"; // NOI18N
}
public void clear() {
throw new UnsupportedOperationException(msg());
}
public boolean contains(Object o) {
throw new UnsupportedOperationException(msg());
}
public boolean containsAll(Collection c) {
throw new UnsupportedOperationException(msg());
}
public boolean isEmpty() {
throw new UnsupportedOperationException(msg());
}
public Iterator<T> iterator() {
throw new UnsupportedOperationException(msg());
}
public boolean remove(Object o) {
throw new UnsupportedOperationException(msg());
}
public boolean removeAll(Collection c) {
throw new UnsupportedOperationException(msg());
}
public boolean retainAll(Collection c) {
throw new UnsupportedOperationException(msg());
}
public int size() {
throw new UnsupportedOperationException(msg());
}
public Object[] toArray() {
throw new UnsupportedOperationException(msg());
}
public<X> X[] toArray(X[] a) {
throw new UnsupportedOperationException(msg());
}
}
// end of ToAdd
}
// end of QEn
/** Filtering enumeration */
private static final class FilEn<T,R> extends Object implements Enumeration<R> {
/** marker object stating there is no nexte element prepared */
private static final Object EMPTY = new Object();
/** enumeration to filter */
private Enumeration<? extends T> en;
/** element to be returned next time or {@link #EMPTY} if there is
* no such element prepared */
private R next = empty();
/** the set to use as filter */
private Processor<T,R> filter;
/**
* @param en enumeration to filter
*/
public FilEn(Enumeration<? extends T> en, Processor<T,R> filter) {
this.en = en;
this.filter = filter;
}
/** @return true if there is more elements in the enumeration
*/
public boolean hasMoreElements() {
if (next != empty()) {
// there is a object already prepared
return true;
}
while (en.hasMoreElements()) {
// read next
next = filter.process(en.nextElement(), null);
if (next != null) {
// if the object is accepted
return true;
}
;
}
next = empty();
return false;
}
/** @return next object in the enumeration
* @exception NoSuchElementException can be thrown if there is no next object
* in the enumeration
*/
public R nextElement() {
if ((next == EMPTY) && !hasMoreElements()) {
throw new NoSuchElementException();
}
R res = next;
next = empty();
return res;
}
@SuppressWarnings("unchecked")
private R empty() {
return (R)EMPTY;
}
}
// end of FilEn
/** Returns true from contains if object is not null */
private static class RNulls<T> implements Processor<T,T> {
public T process(T original, Collection<T> toAdd) {
return original;
}
}
// end of RNulls
}
For each element of the input enumeration asks the Processor to provide a replacement
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License"). You may not use this file except in compliance with the
* License. You can obtain a copy of the License at
* http://www.netbeans.org/cddl-gplv2.html
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
* specific language governing permissions and limitations under the
* License. When distributing the software, include this License Header
* Notice in each file and include the License file at
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the
* License Header, with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Contributor(s):
*
* The Original Software is NetBeans. The Initial Developer of the Original
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
* Microsystems, Inc. All Rights Reserved.
*
* If you wish your version of this file to be governed by only the CDDL
* or only the GPL Version 2, indicate your decision by adding
* "[Contributor] elects to include this software in this distribution
* under the [CDDL or GPL Version 2] license." If you do not indicate a
* single choice of license, a recipient has the option to distribute
* your version of this file under either the CDDL, the GPL Version 2 or
* to extend the choice of license to its licensees as provided above.
* However, if you add GPL Version 2 code and therefore, elected the GPL
* Version 2 license, then the option applies only if the new code is
* made subject to such option by the copyright holder.
*/
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;
/**
* @since 4.37
* @author Jaroslav Tulach
*/
final class Enumerations extends Object {
/**
* For each element of the input enumeration <code>en</code> asks the
* {@link Processor} to provide a replacement.
* The <code>toAdd</code> argument of the processor is always null.
* <p>
* Example to convert any objects into strings:
* <pre>
* Processor convertToString = new Processor() {
* public Object process(Object obj, Collection alwaysNull) {
* return obj.toString(); // converts to string
* }
* };
* Enumeration strings = Enumerations.convert(elems, convertToString);
* </pre>
*
* @param en enumeration of any objects
* @param processor a callback processor for the elements (its toAdd arguments is always null)
* @return new enumeration where all elements has been processed
*/
public static <T,R> Enumeration<R> convert(Enumeration<? extends T> en, Processor<T,R> processor) {
return new AltEn<T,R>(en, processor);
}
/**
* Filters some elements out from the input enumeration.
* Just make the
* {@link Processor} return <code>null</code>. Please notice the <code>toAdd</code>
* argument of the processor is always <code>null</code>.
* <p>
* Example to remove all objects that are not strings:
* <pre>
* Processor onlyString = new Processor() {
* public Object process(Object obj, Collection alwaysNull) {
* if (obj instanceof String) {
* return obj;
* } else {
* return null;
* }
* }
* };
* Enumeration strings = Enumerations.filter(elems, onlyString);
* </pre>
*
* @param en enumeration of any objects
* @param filter a callback processor for the elements (its toAdd arguments is always null)
* @return new enumeration which does not include non-processed (returned null from processor) elements
* @see NbCollections#checkedEnumerationByFilter
*/
public static <T,R> Enumeration<R> filter(Enumeration<? extends T> en, Processor<T,R> filter) {
return new FilEn<T,R>(en, filter);
}
/**
* Support for breadth-first enumerating.
* Before any element is returned
* for the resulting enumeration it is processed in the {@link Processor} and
* the processor is allowed to modify it and also add additional elements
* at the (current) end of the <q>queue</q> by calling <code>toAdd.add</code>
* or <code>toAdd.addAll</code>. No other methods can be called on the
* provided <code>toAdd</code> collection.
* <p>
* Example of doing breadth-first walk through a tree:
* <pre>
* Processor queueSubnodes = new Processor() {
* public Object process(Object obj, Collection toAdd) {
* Node n = (Node)obj;
* toAdd.addAll (n.getChildrenList());
* return n;
* }
* };
* Enumeration strings = Enumerations.queue(elems, queueSubnodes);
* </pre>
*
* @param en initial content of the resulting enumeration
* @param filter the processor that is called for each element and can
* add and addAll elements to its toAdd Collection argument and
* also change the value to be returned
* @return enumeration with the initial and queued content (it can contain
* <code>null</code> if the filter returned <code>null</code> from its
* {@link Processor#process} method.
*/
public static <T,R> Enumeration<R> queue(Enumeration<? extends T> en, Processor<T,R> filter) {
QEn<T,R> q = new QEn<T,R>(filter);
while (en.hasMoreElements()) {
q.put(en.nextElement());
}
return q;
}
/**
* Processor interface that can filter out objects from the enumeration,
* change them or add aditional objects to the end of the current enumeration.
*/
public static interface Processor<T,R> {
/** @param original the object that is going to be returned from the enumeration right now
* @return a replacement for this object
* @param toAdd can be non-null if one can add new objects at the end of the enumeration
*/
public R process(T original, Collection<T> toAdd);
}
/** Altering enumeration implementation */
private static final class AltEn<T,R> extends Object implements Enumeration<R> {
/** enumeration to filter */
private Enumeration<? extends T> en;
/** map to alter */
private Processor<T,R> process;
/**
* @param en enumeration to filter
*/
public AltEn(Enumeration<? extends T> en, Processor<T,R> process) {
this.en = en;
this.process = process;
}
/** @return true if there is more elements in the enumeration
*/
public boolean hasMoreElements() {
return en.hasMoreElements();
}
/** @return next object in the enumeration
* @exception NoSuchElementException can be thrown if there is no next object
* in the enumeration
*/
public R nextElement() {
return process.process(en.nextElement(), null);
}
}
// end of AltEn
/** QueueEnumeration
*/
private static class QEn<T,R> extends Object implements Enumeration<R> {
/** next object to be returned */
private ListItem<T> next = null;
/** last object in the queue */
private ListItem<T> last = null;
/** processor to use */
private Processor<T,R> processor;
public QEn(Processor<T,R> p) {
this.processor = p;
}
/** Put adds new object to the end of queue.
* @param o the object to add
*/
public void put(T o) {
if (last != null) {
ListItem<T> li = new ListItem<T>(o);
last.next = li;
last = li;
} else {
next = last = new ListItem<T>(o);
}
}
/** Adds array of objects into the queue.
* @param arr array of objects to put into the queue
*/
public void put(Collection<? extends T> arr) {
for (T e : arr) {
put(e);
}
}
/** Is there any next object?
* @return true if there is next object, false otherwise
*/
public boolean hasMoreElements() {
return next != null;
}
/** @return next object in enumeration
* @exception NoSuchElementException if there is no next object
*/
public R nextElement() {
if (next == null) {
throw new NoSuchElementException();
}
T res = next.object;
if ((next = next.next) == null) {
last = null;
}
;
ToAdd<T,R> toAdd = new ToAdd<T,R>(this);
R out = processor.process(res, toAdd);
toAdd.finish();
return out;
}
/** item in linked list of Objects */
private static final class ListItem<T> {
T object;
ListItem<T> next;
/** @param o the object for this item */
ListItem(T o) {
object = o;
}
}
/** Temporary collection that supports only add and addAll operations*/
private static final class ToAdd<T,R> extends Object implements Collection<T> {
private QEn<T,R> q;
public ToAdd(QEn<T,R> q) {
this.q = q;
}
public void finish() {
this.q = null;
}
public boolean add(T o) {
q.put(o);
return true;
}
public boolean addAll(Collection<? extends T> c) {
q.put(c);
return true;
}
private String msg() {
return "Only add and addAll are implemented"; // NOI18N
}
public void clear() {
throw new UnsupportedOperationException(msg());
}
public boolean contains(Object o) {
throw new UnsupportedOperationException(msg());
}
public boolean containsAll(Collection c) {
throw new UnsupportedOperationException(msg());
}
public boolean isEmpty() {
throw new UnsupportedOperationException(msg());
}
public Iterator<T> iterator() {
throw new UnsupportedOperationException(msg());
}
public boolean remove(Object o) {
throw new UnsupportedOperationException(msg());
}
public boolean removeAll(Collection c) {
throw new UnsupportedOperationException(msg());
}
public boolean retainAll(Collection c) {
throw new UnsupportedOperationException(msg());
}
public int size() {
throw new UnsupportedOperationException(msg());
}
public Object[] toArray() {
throw new UnsupportedOperationException(msg());
}
public<X> X[] toArray(X[] a) {
throw new UnsupportedOperationException(msg());
}
}
// end of ToAdd
}
// end of QEn
/** Filtering enumeration */
private static final class FilEn<T,R> extends Object implements Enumeration<R> {
/** marker object stating there is no nexte element prepared */
private static final Object EMPTY = new Object();
/** enumeration to filter */
private Enumeration<? extends T> en;
/** element to be returned next time or {@link #EMPTY} if there is
* no such element prepared */
private R next = empty();
/** the set to use as filter */
private Processor<T,R> filter;
/**
* @param en enumeration to filter
*/
public FilEn(Enumeration<? extends T> en, Processor<T,R> filter) {
this.en = en;
this.filter = filter;
}
/** @return true if there is more elements in the enumeration
*/
public boolean hasMoreElements() {
if (next != empty()) {
// there is a object already prepared
return true;
}
while (en.hasMoreElements()) {
// read next
next = filter.process(en.nextElement(), null);
if (next != null) {
// if the object is accepted
return true;
}
;
}
next = empty();
return false;
}
/** @return next object in the enumeration
* @exception NoSuchElementException can be thrown if there is no next object
* in the enumeration
*/
public R nextElement() {
if ((next == EMPTY) && !hasMoreElements()) {
throw new NoSuchElementException();
}
R res = next;
next = empty();
return res;
}
@SuppressWarnings("unchecked")
private R empty() {
return (R)EMPTY;
}
}
// end of FilEn
/** Returns true from contains if object is not null */
private static class RNulls<T> implements Processor<T,T> {
public T process(T original, Collection<T> toAdd) {
return original;
}
}
// end of RNulls
}
ListOfFiles implements Enumeration<FileInputStream>
/*
* Copyright (c) 1995 - 2008 Sun Microsystems, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Sun Microsystems nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Enumeration;
import java.util.NoSuchElementException;
public class ListOfFiles implements Enumeration<FileInputStream> {
private String[] listOfFiles;
private int current = 0;
public ListOfFiles(String[] listOfFiles) {
this.listOfFiles = listOfFiles;
}
public boolean hasMoreElements() {
if (current < listOfFiles.length)
return true;
else
return false;
}
public FileInputStream nextElement() {
FileInputStream in = null;
if (!hasMoreElements())
throw new NoSuchElementException("No more files.");
else {
String nextElement = listOfFiles[current];
current++;
try {
in = new FileInputStream(nextElement);
} catch (FileNotFoundException e) {
System.err.println("ListOfFiles: Can"t open " + nextElement);
}
}
return in;
}
}
Removes all nulls from the input enumeration.
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License"). You may not use this file except in compliance with the
* License. You can obtain a copy of the License at
* http://www.netbeans.org/cddl-gplv2.html
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
* specific language governing permissions and limitations under the
* License. When distributing the software, include this License Header
* Notice in each file and include the License file at
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the
* License Header, with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Contributor(s):
*
* The Original Software is NetBeans. The Initial Developer of the Original
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
* Microsystems, Inc. All Rights Reserved.
*
* If you wish your version of this file to be governed by only the CDDL
* or only the GPL Version 2, indicate your decision by adding
* "[Contributor] elects to include this software in this distribution
* under the [CDDL or GPL Version 2] license." If you do not indicate a
* single choice of license, a recipient has the option to distribute
* your version of this file under either the CDDL, the GPL Version 2 or
* to extend the choice of license to its licensees as provided above.
* However, if you add GPL Version 2 code and therefore, elected the GPL
* Version 2 license, then the option applies only if the new code is
* made subject to such option by the copyright holder.
*/
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;
/**
* @since 4.37
* @author Jaroslav Tulach
*/
final class Enumerations extends Object {
/**
* Removes all <code>null</code>s from the input enumeration.
* @param en enumeration that can contain nulls
* @return new enumeration without null values
*/
public static <T> Enumeration<T> removeNulls(Enumeration<T> en) {
return filter(en, new RNulls<T>());
}
/**
* For each element of the input enumeration <code>en</code> asks the
* {@link Processor} to provide a replacement.
* The <code>toAdd</code> argument of the processor is always null.
* <p>
* Example to convert any objects into strings:
* <pre>
* Processor convertToString = new Processor() {
* public Object process(Object obj, Collection alwaysNull) {
* return obj.toString(); // converts to string
* }
* };
* Enumeration strings = Enumerations.convert(elems, convertToString);
* </pre>
*
* @param en enumeration of any objects
* @param processor a callback processor for the elements (its toAdd arguments is always null)
* @return new enumeration where all elements has been processed
*/
public static <T,R> Enumeration<R> convert(Enumeration<? extends T> en, Processor<T,R> processor) {
return new AltEn<T,R>(en, processor);
}
/**
* Filters some elements out from the input enumeration.
* Just make the
* {@link Processor} return <code>null</code>. Please notice the <code>toAdd</code>
* argument of the processor is always <code>null</code>.
* <p>
* Example to remove all objects that are not strings:
* <pre>
* Processor onlyString = new Processor() {
* public Object process(Object obj, Collection alwaysNull) {
* if (obj instanceof String) {
* return obj;
* } else {
* return null;
* }
* }
* };
* Enumeration strings = Enumerations.filter(elems, onlyString);
* </pre>
*
* @param en enumeration of any objects
* @param filter a callback processor for the elements (its toAdd arguments is always null)
* @return new enumeration which does not include non-processed (returned null from processor) elements
* @see NbCollections#checkedEnumerationByFilter
*/
public static <T,R> Enumeration<R> filter(Enumeration<? extends T> en, Processor<T,R> filter) {
return new FilEn<T,R>(en, filter);
}
/**
* Support for breadth-first enumerating.
* Before any element is returned
* for the resulting enumeration it is processed in the {@link Processor} and
* the processor is allowed to modify it and also add additional elements
* at the (current) end of the <q>queue</q> by calling <code>toAdd.add</code>
* or <code>toAdd.addAll</code>. No other methods can be called on the
* provided <code>toAdd</code> collection.
* <p>
* Example of doing breadth-first walk through a tree:
* <pre>
* Processor queueSubnodes = new Processor() {
* public Object process(Object obj, Collection toAdd) {
* Node n = (Node)obj;
* toAdd.addAll (n.getChildrenList());
* return n;
* }
* };
* Enumeration strings = Enumerations.queue(elems, queueSubnodes);
* </pre>
*
* @param en initial content of the resulting enumeration
* @param filter the processor that is called for each element and can
* add and addAll elements to its toAdd Collection argument and
* also change the value to be returned
* @return enumeration with the initial and queued content (it can contain
* <code>null</code> if the filter returned <code>null</code> from its
* {@link Processor#process} method.
*/
public static <T,R> Enumeration<R> queue(Enumeration<? extends T> en, Processor<T,R> filter) {
QEn<T,R> q = new QEn<T,R>(filter);
while (en.hasMoreElements()) {
q.put(en.nextElement());
}
return q;
}
/**
* Processor interface that can filter out objects from the enumeration,
* change them or add aditional objects to the end of the current enumeration.
*/
public static interface Processor<T,R> {
/** @param original the object that is going to be returned from the enumeration right now
* @return a replacement for this object
* @param toAdd can be non-null if one can add new objects at the end of the enumeration
*/
public R process(T original, Collection<T> toAdd);
}
/** Altering enumeration implementation */
private static final class AltEn<T,R> extends Object implements Enumeration<R> {
/** enumeration to filter */
private Enumeration<? extends T> en;
/** map to alter */
private Processor<T,R> process;
/**
* @param en enumeration to filter
*/
public AltEn(Enumeration<? extends T> en, Processor<T,R> process) {
this.en = en;
this.process = process;
}
/** @return true if there is more elements in the enumeration
*/
public boolean hasMoreElements() {
return en.hasMoreElements();
}
/** @return next object in the enumeration
* @exception NoSuchElementException can be thrown if there is no next object
* in the enumeration
*/
public R nextElement() {
return process.process(en.nextElement(), null);
}
}
// end of AltEn
/** QueueEnumeration
*/
private static class QEn<T,R> extends Object implements Enumeration<R> {
/** next object to be returned */
private ListItem<T> next = null;
/** last object in the queue */
private ListItem<T> last = null;
/** processor to use */
private Processor<T,R> processor;
public QEn(Processor<T,R> p) {
this.processor = p;
}
/** Put adds new object to the end of queue.
* @param o the object to add
*/
public void put(T o) {
if (last != null) {
ListItem<T> li = new ListItem<T>(o);
last.next = li;
last = li;
} else {
next = last = new ListItem<T>(o);
}
}
/** Adds array of objects into the queue.
* @param arr array of objects to put into the queue
*/
public void put(Collection<? extends T> arr) {
for (T e : arr) {
put(e);
}
}
/** Is there any next object?
* @return true if there is next object, false otherwise
*/
public boolean hasMoreElements() {
return next != null;
}
/** @return next object in enumeration
* @exception NoSuchElementException if there is no next object
*/
public R nextElement() {
if (next == null) {
throw new NoSuchElementException();
}
T res = next.object;
if ((next = next.next) == null) {
last = null;
}
;
ToAdd<T,R> toAdd = new ToAdd<T,R>(this);
R out = processor.process(res, toAdd);
toAdd.finish();
return out;
}
/** item in linked list of Objects */
private static final class ListItem<T> {
T object;
ListItem<T> next;
/** @param o the object for this item */
ListItem(T o) {
object = o;
}
}
/** Temporary collection that supports only add and addAll operations*/
private static final class ToAdd<T,R> extends Object implements Collection<T> {
private QEn<T,R> q;
public ToAdd(QEn<T,R> q) {
this.q = q;
}
public void finish() {
this.q = null;
}
public boolean add(T o) {
q.put(o);
return true;
}
public boolean addAll(Collection<? extends T> c) {
q.put(c);
return true;
}
private String msg() {
return "Only add and addAll are implemented"; // NOI18N
}
public void clear() {
throw new UnsupportedOperationException(msg());
}
public boolean contains(Object o) {
throw new UnsupportedOperationException(msg());
}
public boolean containsAll(Collection c) {
throw new UnsupportedOperationException(msg());
}
public boolean isEmpty() {
throw new UnsupportedOperationException(msg());
}
public Iterator<T> iterator() {
throw new UnsupportedOperationException(msg());
}
public boolean remove(Object o) {
throw new UnsupportedOperationException(msg());
}
public boolean removeAll(Collection c) {
throw new UnsupportedOperationException(msg());
}
public boolean retainAll(Collection c) {
throw new UnsupportedOperationException(msg());
}
public int size() {
throw new UnsupportedOperationException(msg());
}
public Object[] toArray() {
throw new UnsupportedOperationException(msg());
}
public<X> X[] toArray(X[] a) {
throw new UnsupportedOperationException(msg());
}
}
// end of ToAdd
}
// end of QEn
/** Filtering enumeration */
private static final class FilEn<T,R> extends Object implements Enumeration<R> {
/** marker object stating there is no nexte element prepared */
private static final Object EMPTY = new Object();
/** enumeration to filter */
private Enumeration<? extends T> en;
/** element to be returned next time or {@link #EMPTY} if there is
* no such element prepared */
private R next = empty();
/** the set to use as filter */
private Processor<T,R> filter;
/**
* @param en enumeration to filter
*/
public FilEn(Enumeration<? extends T> en, Processor<T,R> filter) {
this.en = en;
this.filter = filter;
}
/** @return true if there is more elements in the enumeration
*/
public boolean hasMoreElements() {
if (next != empty()) {
// there is a object already prepared
return true;
}
while (en.hasMoreElements()) {
// read next
next = filter.process(en.nextElement(), null);
if (next != null) {
// if the object is accepted
return true;
}
;
}
next = empty();
return false;
}
/** @return next object in the enumeration
* @exception NoSuchElementException can be thrown if there is no next object
* in the enumeration
*/
public R nextElement() {
if ((next == EMPTY) && !hasMoreElements()) {
throw new NoSuchElementException();
}
R res = next;
next = empty();
return res;
}
@SuppressWarnings("unchecked")
private R empty() {
return (R)EMPTY;
}
}
// end of FilEn
/** Returns true from contains if object is not null */
private static class RNulls<T> implements Processor<T,T> {
public T process(T original, Collection<T> toAdd) {
return original;
}
}
// end of RNulls
}
Single Item Enumeration
/**
* The utillib library.
* More information is available at http://www.jinchess.ru/.
* Copyright (C) 2002 Alexander Maryanovsky.
* All rights reserved.
*
* The utillib library is free software; you can redistribute
* it and/or modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* The utillib library is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with utillib library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
import java.util.Enumeration;
import java.util.NoSuchElementException;
/**
* An implementation of the Enumeration interface which enumerates a single
* item. Note: This class is not thread safe.
*/
public class SingleItemEnumeration implements Enumeration{
/**
* The sole item.
*/
private Object item;
/**
* Becomes true when we"ve returned the sole item.
*/
private boolean done = false;
/**
* Creates a new SingleItemEnumeration which enumerates the specified item.
*/
public SingleItemEnumeration(Object item){
this.item = item;
}
/**
* Returns the sole item or throws a <code>NoSuchElementException</code>.
*/
public Object nextElement(){
if (!hasMoreElements())
throw new NoSuchElementException();
done = true;
Object item = this.item;
this.item = null; // We don"t want to hold a reference to it any longer than we need.
return item;
}
/**
* Returns true if there are more elements in the enumeration.
*/
public boolean hasMoreElements(){
return !done;
}
}
Support for breadth-first enumerating.
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License"). You may not use this file except in compliance with the
* License. You can obtain a copy of the License at
* http://www.netbeans.org/cddl-gplv2.html
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
* specific language governing permissions and limitations under the
* License. When distributing the software, include this License Header
* Notice in each file and include the License file at
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the
* License Header, with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Contributor(s):
*
* The Original Software is NetBeans. The Initial Developer of the Original
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
* Microsystems, Inc. All Rights Reserved.
*
* If you wish your version of this file to be governed by only the CDDL
* or only the GPL Version 2, indicate your decision by adding
* "[Contributor] elects to include this software in this distribution
* under the [CDDL or GPL Version 2] license." If you do not indicate a
* single choice of license, a recipient has the option to distribute
* your version of this file under either the CDDL, the GPL Version 2 or
* to extend the choice of license to its licensees as provided above.
* However, if you add GPL Version 2 code and therefore, elected the GPL
* Version 2 license, then the option applies only if the new code is
* made subject to such option by the copyright holder.
*/
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;
/**
* @since 4.37
* @author Jaroslav Tulach
*/
final class Enumerations extends Object {
/**
* Support for breadth-first enumerating.
* Before any element is returned
* for the resulting enumeration it is processed in the {@link Processor} and
* the processor is allowed to modify it and also add additional elements
* at the (current) end of the <q>queue</q> by calling <code>toAdd.add</code>
* or <code>toAdd.addAll</code>. No other methods can be called on the
* provided <code>toAdd</code> collection.
* <p>
* Example of doing breadth-first walk through a tree:
* <pre>
* Processor queueSubnodes = new Processor() {
* public Object process(Object obj, Collection toAdd) {
* Node n = (Node)obj;
* toAdd.addAll (n.getChildrenList());
* return n;
* }
* };
* Enumeration strings = Enumerations.queue(elems, queueSubnodes);
* </pre>
*
* @param en initial content of the resulting enumeration
* @param filter the processor that is called for each element and can
* add and addAll elements to its toAdd Collection argument and
* also change the value to be returned
* @return enumeration with the initial and queued content (it can contain
* <code>null</code> if the filter returned <code>null</code> from its
* {@link Processor#process} method.
*/
public static <T,R> Enumeration<R> queue(Enumeration<? extends T> en, Processor<T,R> filter) {
QEn<T,R> q = new QEn<T,R>(filter);
while (en.hasMoreElements()) {
q.put(en.nextElement());
}
return q;
}
/**
* Processor interface that can filter out objects from the enumeration,
* change them or add aditional objects to the end of the current enumeration.
*/
public static interface Processor<T,R> {
/** @param original the object that is going to be returned from the enumeration right now
* @return a replacement for this object
* @param toAdd can be non-null if one can add new objects at the end of the enumeration
*/
public R process(T original, Collection<T> toAdd);
}
/** Altering enumeration implementation */
private static final class AltEn<T,R> extends Object implements Enumeration<R> {
/** enumeration to filter */
private Enumeration<? extends T> en;
/** map to alter */
private Processor<T,R> process;
/**
* @param en enumeration to filter
*/
public AltEn(Enumeration<? extends T> en, Processor<T,R> process) {
this.en = en;
this.process = process;
}
/** @return true if there is more elements in the enumeration
*/
public boolean hasMoreElements() {
return en.hasMoreElements();
}
/** @return next object in the enumeration
* @exception NoSuchElementException can be thrown if there is no next object
* in the enumeration
*/
public R nextElement() {
return process.process(en.nextElement(), null);
}
}
// end of AltEn
/** QueueEnumeration
*/
private static class QEn<T,R> extends Object implements Enumeration<R> {
/** next object to be returned */
private ListItem<T> next = null;
/** last object in the queue */
private ListItem<T> last = null;
/** processor to use */
private Processor<T,R> processor;
public QEn(Processor<T,R> p) {
this.processor = p;
}
/** Put adds new object to the end of queue.
* @param o the object to add
*/
public void put(T o) {
if (last != null) {
ListItem<T> li = new ListItem<T>(o);
last.next = li;
last = li;
} else {
next = last = new ListItem<T>(o);
}
}
/** Adds array of objects into the queue.
* @param arr array of objects to put into the queue
*/
public void put(Collection<? extends T> arr) {
for (T e : arr) {
put(e);
}
}
/** Is there any next object?
* @return true if there is next object, false otherwise
*/
public boolean hasMoreElements() {
return next != null;
}
/** @return next object in enumeration
* @exception NoSuchElementException if there is no next object
*/
public R nextElement() {
if (next == null) {
throw new NoSuchElementException();
}
T res = next.object;
if ((next = next.next) == null) {
last = null;
}
;
ToAdd<T,R> toAdd = new ToAdd<T,R>(this);
R out = processor.process(res, toAdd);
toAdd.finish();
return out;
}
/** item in linked list of Objects */
private static final class ListItem<T> {
T object;
ListItem<T> next;
/** @param o the object for this item */
ListItem(T o) {
object = o;
}
}
/** Temporary collection that supports only add and addAll operations*/
private static final class ToAdd<T,R> extends Object implements Collection<T> {
private QEn<T,R> q;
public ToAdd(QEn<T,R> q) {
this.q = q;
}
public void finish() {
this.q = null;
}
public boolean add(T o) {
q.put(o);
return true;
}
public boolean addAll(Collection<? extends T> c) {
q.put(c);
return true;
}
private String msg() {
return "Only add and addAll are implemented"; // NOI18N
}
public void clear() {
throw new UnsupportedOperationException(msg());
}
public boolean contains(Object o) {
throw new UnsupportedOperationException(msg());
}
public boolean containsAll(Collection c) {
throw new UnsupportedOperationException(msg());
}
public boolean isEmpty() {
throw new UnsupportedOperationException(msg());
}
public Iterator<T> iterator() {
throw new UnsupportedOperationException(msg());
}
public boolean remove(Object o) {
throw new UnsupportedOperationException(msg());
}
public boolean removeAll(Collection c) {
throw new UnsupportedOperationException(msg());
}
public boolean retainAll(Collection c) {
throw new UnsupportedOperationException(msg());
}
public int size() {
throw new UnsupportedOperationException(msg());
}
public Object[] toArray() {
throw new UnsupportedOperationException(msg());
}
public<X> X[] toArray(X[] a) {
throw new UnsupportedOperationException(msg());
}
}
// end of ToAdd
}
// end of QEn
/** Filtering enumeration */
private static final class FilEn<T,R> extends Object implements Enumeration<R> {
/** marker object stating there is no nexte element prepared */
private static final Object EMPTY = new Object();
/** enumeration to filter */
private Enumeration<? extends T> en;
/** element to be returned next time or {@link #EMPTY} if there is
* no such element prepared */
private R next = empty();
/** the set to use as filter */
private Processor<T,R> filter;
/**
* @param en enumeration to filter
*/
public FilEn(Enumeration<? extends T> en, Processor<T,R> filter) {
this.en = en;
this.filter = filter;
}
/** @return true if there is more elements in the enumeration
*/
public boolean hasMoreElements() {
if (next != empty()) {
// there is a object already prepared
return true;
}
while (en.hasMoreElements()) {
// read next
next = filter.process(en.nextElement(), null);
if (next != null) {
// if the object is accepted
return true;
}
;
}
next = empty();
return false;
}
/** @return next object in the enumeration
* @exception NoSuchElementException can be thrown if there is no next object
* in the enumeration
*/
public R nextElement() {
if ((next == EMPTY) && !hasMoreElements()) {
throw new NoSuchElementException();
}
R res = next;
next = empty();
return res;
}
@SuppressWarnings("unchecked")
private R empty() {
return (R)EMPTY;
}
}
// end of FilEn
/** Returns true from contains if object is not null */
private static class RNulls<T> implements Processor<T,T> {
public T process(T original, Collection<T> toAdd) {
return original;
}
}
// end of RNulls
}
Treat an Enumeration as an Iterable
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License"). You may not use this file except in compliance with the
* License. You can obtain a copy of the License at
* http://www.netbeans.org/cddl-gplv2.html
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
* specific language governing permissions and limitations under the
* License. When distributing the software, include this License Header
* Notice in each file and include the License file at
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the
* License Header, with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Contributor(s):
*
* The Original Software is NetBeans. The Initial Developer of the Original
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
* Microsystems, Inc. All Rights Reserved.
*
* If you wish your version of this file to be governed by only the CDDL
* or only the GPL Version 2, indicate your decision by adding
* "[Contributor] elects to include this software in this distribution
* under the [CDDL or GPL Version 2] license." If you do not indicate a
* single choice of license, a recipient has the option to distribute
* your version of this file under either the CDDL, the GPL Version 2 or
* to extend the choice of license to its licensees as provided above.
* However, if you add GPL Version 2 code and therefore, elected the GPL
* Version 2 license, then the option applies only if the new code is
* made subject to such option by the copyright holder.
*/
import java.util.Enumeration;
import java.util.Iterator;
/**
* @since 4.37
* @author Jaroslav Tulach
*/
public class Utils {
/**
* Treat an {@link Enumeration} as an {@link Iterable} so it can be used in an enhanced for-loop.
* Bear in mind that the enumeration is "consumed" by the loop and so should be used only once.
* Generally it is best to put the code which obtains the enumeration inside the loop header.
* <div class="nonnormative">
* <p>Example of correct usage:</p>
* <pre>
* ClassLoader loader = ...;
* String name = ...;
* for (URL resource : NbCollections.iterable(loader.{@link ClassLoader#getResources getResources}(name))) {
* // ...
* }
* </pre>
* </div>
* @param enumeration an enumeration
* @return an iterable wrapper which will traverse the enumeration once
* ({@link Iterator#remove} is not supported)
* @throws NullPointerException if the enumeration is null
* @see
* @since org.openide.util 7.5
*/
public static <E> Iterable<E> iterable(final Enumeration<E> enumeration) {
if (enumeration == null) {
throw new NullPointerException();
}
return new Iterable<E>() {
public Iterator<E> iterator() {
return new Iterator<E>() {
public boolean hasNext() {
return enumeration.hasMoreElements();
}
public E next() {
return enumeration.nextElement();
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}
}
Wrapping an Iterator around an Enumeration
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;
public class EnumerationIterator1 {
public static Iterator iterator(final Enumeration e) {
return new Iterator() {
public boolean hasNext() {
return e.hasMoreElements();
}
public Object next() {
return e.nextElement();
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
public static void main(String args[]) {
String elements[] = { "Java", "Source", "and", "Support", "." };
Vector v = new Vector(Arrays.asList(elements));
Enumeration e = v.elements();
Iterator itor = EnumerationIterator1.iterator(e);
while (itor.hasNext()) {
System.out.println(itor.next());
}
}
}