public final class Enumerations extends Object
Enumeration
.
Allows composition of existing enumerations, filtering their contents, and/or modifying them.
All of this is designed to be done lazily, i.e. elements created on demand.Modifier and Type | Class and Description |
---|---|
static interface |
Enumerations.Processor<T,R>
Processor interface that can filter out objects from the enumeration,
change them or add aditional objects to the end of the current enumeration.
|
Modifier and Type | Method and Description |
---|---|
static <T> Enumeration<T> |
array(T... arr)
Returns an enumeration that iterates over provided array.
|
static <T> Enumeration<T> |
concat(Enumeration<? extends Enumeration<? extends T>> enumOfEnums)
Concatenates the content of many enumerations.
|
static <T> Enumeration<T> |
concat(Enumeration<? extends T> en1,
Enumeration<? extends T> en2)
Concatenates the content of two enumerations into one.
|
static <T,R> Enumeration<R> |
convert(Enumeration<? extends T> en,
Enumerations.Processor<T,R> processor)
For each element of the input enumeration
en asks the
Enumerations.Processor to provide a replacement. |
static <T> Enumeration<T> |
empty()
An empty enumeration.
|
static <T,R> Enumeration<R> |
filter(Enumeration<? extends T> en,
Enumerations.Processor<T,R> filter)
Filters some elements out from the input enumeration.
|
static <T,R> Enumeration<R> |
queue(Enumeration<? extends T> en,
Enumerations.Processor<T,R> filter)
Support for breadth-first enumerating.
|
static <T> Enumeration<T> |
removeDuplicates(Enumeration<T> en)
Filters the input enumeration to new one that should contain
each of the provided elements just once.
|
static <T> Enumeration<T> |
removeNulls(Enumeration<T> en)
Removes all
null s from the input enumeration. |
static <T> Enumeration<T> |
singleton(T obj)
Creates an enumeration with one element.
|
public static final <T> Enumeration<T> empty()
false
from
empty().hasMoreElements()
and throws NoSuchElementException
from empty().nextElement()
.public static <T> Enumeration<T> singleton(T obj)
obj
- the element to be present in the enumeration.public static <T> Enumeration<T> concat(Enumeration<? extends T> en1, Enumeration<? extends T> en2)
en1
is reached its elements are being served.
As soon as the en1
has no more elements, the content
of en2
is being returned.en1
- first enumerationen2
- second enumerationpublic static <T> Enumeration<T> concat(Enumeration<? extends Enumeration<? extends T>> enumOfEnums)
enumOfEnums
- Enumeration of Enumeration elementspublic static <T> Enumeration<T> removeDuplicates(Enumeration<T> en)
equals
and hashCode
methods.en
- enumeration to filterpublic static <T> Enumeration<T> array(T... arr)
arr
- the array of objectpublic static <T> Enumeration<T> removeNulls(Enumeration<T> en)
null
s from the input enumeration.en
- enumeration that can contain nullspublic static <T,R> Enumeration<R> convert(Enumeration<? extends T> en, Enumerations.Processor<T,R> processor)
en
asks the
Enumerations.Processor
to provide a replacement.
The toAdd
argument of the processor is always null.
Example to convert any objects into strings:
Processor convertToString = new Processor() { public Object process(Object obj, Collection alwaysNull) { return obj.toString(); // converts to string } }; Enumeration strings = Enumerations.convert(elems, convertToString);
en
- enumeration of any objectsprocessor
- a callback processor for the elements (its toAdd arguments is always null)public static <T,R> Enumeration<R> filter(Enumeration<? extends T> en, Enumerations.Processor<T,R> filter)
Enumerations.Processor
return null
. Please notice the toAdd
argument of the processor is always null
.
Example to remove all objects that are not strings:
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);
en
- enumeration of any objectsfilter
- a callback processor for the elements (its toAdd arguments is always null)NbCollections.checkedEnumerationByFilter(java.util.Enumeration, java.lang.Class<E>, boolean)
public static <T,R> Enumeration<R> queue(Enumeration<? extends T> en, Enumerations.Processor<T,R> filter)
Enumerations.Processor
and
the processor is allowed to modify it and also add additional elements
at the (current) end of the queueby calling
toAdd.add
or toAdd.addAll
. No other methods can be called on the
provided toAdd
collection.
Example of doing breadth-first walk through a tree:
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);
en
- initial content of the resulting enumerationfilter
- 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 returnednull
if the filter returned null
from its
Enumerations.Processor.process(T, java.util.Collection<T>)
method.