KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > util > iterator > UniqueExtendedIterator


1 /******************************************************************
2  * File: UniqueExtendedIterator.java
3  * Created by: Dave Reynolds
4  * Created on: 28-Jan-2003
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: UniqueExtendedIterator.java,v 1.12 2005/02/21 12:19:17 andy_seaborne Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.util.iterator;
11
12 import java.util.*;
13
14 /**
15  * A variant on the closable/extended iterator that filters out
16  * duplicate values. There is one complication that the value
17  * which filtering is done on might not be the actual value
18  * to be returned by the iterator.
19  *
20  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
21  * @version $Revision: 1.12 $ on $Date: 2005/02/21 12:19:17 $
22  */

23 public class UniqueExtendedIterator extends WrappedIterator {
24
25     /** The set of objects already seen */
26     protected HashSet seen = new HashSet();
27     
28     /** One level lookahead */
29     protected Object JavaDoc next = null;
30     
31     /**
32      * Constructor. Note the use of {@link #create} as reliable means of
33      * creating a unique iterator without double-wrapping iterators that
34      * are already unique iterators.
35      */

36     public UniqueExtendedIterator(Iterator underlying) {
37         super(underlying);
38     }
39     
40     /**
41      * Factory method for generating an iterator that is guaranteed
42      * only to return one instance of every result from the wrapped
43      * iterator <code>it</code>.
44      * @param it An iterator to wrap
45      * @return A iterator that returns the elements of the wrapped
46      * iterator exactly once. If <code>it</code> is already a unique
47      * extended iteator, it is not further wrapped.
48      */

49     public static ExtendedIterator create( Iterator it ) {
50         return (it instanceof UniqueExtendedIterator) ?
51                     ((UniqueExtendedIterator) it) : new UniqueExtendedIterator( it );
52     }
53     
54     /**
55      * Fetch the next object to be returned, only if not already seen.
56      * Subclasses which need to filter on different objects than the
57      * return values should override this method.
58      * @return the object to be returned or null if the object has been filtered.
59      */

60     protected Object JavaDoc nextIfNew() {
61         Object JavaDoc value = super.next();
62         return seen.add( value ) ? value : null;
63     }
64     
65     /**
66      * @see Iterator#hasNext()
67      */

68     public boolean hasNext() {
69         while (next == null && super.hasNext()) next = nextIfNew();
70         return next != null;
71     }
72
73     /**
74      * @see Iterator#next()
75      */

76     public Object JavaDoc next() {
77         ensureHasNext();
78         Object JavaDoc result = next;
79         next = null;
80         return result;
81     }
82 }
83
84 /*
85  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
86  * All rights reserved.
87  *
88  * Redistribution and use in source and binary forms, with or without
89  * modification, are permitted provided that the following conditions
90  * are met:
91  * 1. Redistributions of source code must retain the above copyright
92  * notice, this list of conditions and the following disclaimer.
93  * 2. Redistributions in binary form must reproduce the above copyright
94  * notice, this list of conditions and the following disclaimer in the
95  * documentation and/or other materials provided with the distribution.
96  * 3. The name of the author may not be used to endorse or promote products
97  * derived from this software without specific prior written permission.
98  *
99  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
100  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
101  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
102  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
103  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
104  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
105  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
106  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
107  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
108  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
109  */

110
111
Popular Tags