KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > bsf > smartValueObject > container > SmartIterator


1 package org.bsf.smartValueObject.container;
2
3 import org.bsf.smartValueObject.VersionableFilter;
4 import org.bsf.smartValueObject.Versionable;
5
6 import java.util.*;
7
8 /**
9  * A replacement for <tt>java.util.Iterator</tt>.
10  * <p>This iterator respects the state of versionable objects,
11  * as they can be marked 'deleted' and should not be given out
12  * by an interator. It is configured via an <tt>VersionableFilter</tt>.
13  *
14  * @see org.bsf.smartValueObject.container.SmartCollection
15  * @see org.bsf.smartValueObject.container.SmartMap
16  * @see org.bsf.smartValueObject.VersionableFilter
17  */

18 public class SmartIterator implements Iterator {
19     /** Marker object as placeholder. */
20     private static final Object JavaDoc NOOBJECT = new Object JavaDoc();
21     /** The underlying iterator. */
22     private Iterator it;
23     private Object JavaDoc next = NOOBJECT;
24     /** This filter decides which objects are visible to the user. */
25     private VersionableFilter filter;
26
27     /**
28      * Initializes this iterator with another iterator and
29      * a <tt>VersionableFilter</tt>.
30      *
31      * @param it the underlying iterator.
32      * @param f the filter to use while iterating.
33      */

34     public SmartIterator(Iterator it, VersionableFilter f) {
35         this.it = it;
36         this.filter = f;
37     }
38
39     public boolean hasNext() {
40        while (it.hasNext()) {
41            next = it.next();
42            if (next instanceof Versionable) {
43                if (!filter.accept((Versionable)next)) {
44                    // skip filtered objects
45
continue;
46                } else {
47                    return true;
48                }
49            } else {
50                // no versionable Object found
51
continue;
52            }
53        }
54        return false;
55     }
56
57     public Object JavaDoc next() {
58         if (next == NOOBJECT) {
59             if (!hasNext()) {
60                 throw new NoSuchElementException();
61             }
62
63             Object JavaDoc temp = next;
64             next = NOOBJECT;
65             return temp;
66
67         } else {
68             return next;
69         }
70     }
71
72     /**
73      * We dont't support this.
74      */

75     public void remove() {
76         throw new UnsupportedOperationException JavaDoc();
77     }
78 }
79
Popular Tags