|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.xshare.base.iterator.RangeIterator
Specialized Iterator
implementation - based on
UnaryPredicates
-
to limit the returned elements of another Iterator
pointing to a sorted list of elements.
In some cases you would like to process only some elements of
an iterator.
Mostly the FilterIterator
is the best choice
for that task. If the iterator is not sorted and doesn't
point to a huge amount of objects, you should use the
FilterIterator
.
In some seldom cases, you will
get a iterator pointing to a huge sorted set of elements, e.g.
a iterator with all customers of your company stored in a collection
- maintained by a OO database - and sorted by name. Now imagine you
want to process
all members starting with C. If you choose the
FilterIterator
, this iterator will process all elements of
your source iterator to find out, which elements to return.
Choosing this iterator implementation you can avoid
this and
configure this iterator in such a way, that it stops to return
elements if the name of a customer starts with D and not
with C.
UnaryPredicates
. A very clean
solution to express the conditions. Starting from now I will
call this predicates borders. A RangeIterator
can have two borders: One for specifing the left border of the
element range and another one for the right border.
The predicate for the left border specifies the
first object returned by this iterator and the one for the right border
specifies the object where this iterator stops to return objects.
This object is not returned.
This example will show how to filter all customers starting with "C" and "D" stored in a huge collection which is sorted by name.
// The customer class class Customer { String getName() { ... } } ... public Iterator getAllCustomersWithCAndD() { return new RangeIterator( // predicate for the left border. we start to return objects // if we find a 'C' new UnaryPredicate() { boolean exec(Object pObject) { return ((Customer)pObject).getName().startsWith("C"); } } // source iterator customerSortedList.iterator(), // predicate for the right border. we stop, if we find a 'E' new UnaryPredicate() { boolean exec(Object pObject) { return ((Customer)pObject).getName().startsWith("E"); } }); } ...
next()
or hasNext()
and not
after creating an instance of this iterator.
FilterIterator
Field Summary | |
protected UnaryPredicate |
leftBorder
Specifies the left border of the element range. |
protected UnaryPredicate |
rightBorder
Specifies the right border of the element range. |
Constructor Summary | |
RangeIterator(java.util.Iterator pSourceIterator,
UnaryPredicate pRightBorder)
|
|
RangeIterator(UnaryPredicate pLeftBorder,
java.util.Iterator pSourceIterator)
|
|
RangeIterator(UnaryPredicate pLeftBorder,
java.util.Iterator pSourceIterator,
UnaryPredicate pRightBorder)
|
Method Summary | |
boolean |
hasNext()
Checks if there is a element left to be returned by next() . |
java.lang.Object |
next()
Returns the next object of the specified range. |
void |
remove()
This operation is not supported since this iterator implementaiton is only a proxy for another one. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
protected UnaryPredicate leftBorder
protected UnaryPredicate rightBorder
Constructor Detail |
public RangeIterator(UnaryPredicate pLeftBorder, java.util.Iterator pSourceIterator, UnaryPredicate pRightBorder)
public RangeIterator(UnaryPredicate pLeftBorder, java.util.Iterator pSourceIterator)
public RangeIterator(java.util.Iterator pSourceIterator, UnaryPredicate pRightBorder)
Method Detail |
public void remove()
remove
in interface java.util.Iterator
java.lang.UnsupportedOperationException
- if this method is
called.public java.lang.Object next()
next
in interface java.util.Iterator
java.util.NoSuchElementException
- if hasNext()
returns false
.hasNext()
public boolean hasNext()
next()
.
hasNext
in interface java.util.Iterator
boolean
value which is
true
if there are elements of the
specified range left, which can be returned via
next()
. Otherwise this method returns
false
.next()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |