KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > util > iterator > LookaheadIterator


1 /* TransformingIteratorWrapper
2 *
3 * $Id: LookaheadIterator.java,v 1.3.4.1 2007/01/13 01:31:40 stack-sf Exp $
4 *
5 * Created on Mar 25, 2005
6 *
7 * Copyright (C) 2005 Internet Archive.
8 *
9 * This file is part of the Heritrix web crawler (crawler.archive.org).
10 *
11 * Heritrix is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * any later version.
15 *
16 * Heritrix is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser Public License
22 * along with Heritrix; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */

25 package org.archive.util.iterator;
26
27 import java.util.Iterator JavaDoc;
28 import java.util.NoSuchElementException JavaDoc;
29
30 /**
31  * Superclass for Iterators which must probe ahead to know if
32  * a 'next' exists, and thus have a cached next between a call
33  * to hasNext() and next().
34  *
35  * @author gojomo
36  *
37  */

38 public abstract class LookaheadIterator<T> implements Iterator JavaDoc<T> {
39     protected T next;
40
41     /**
42      * Test whether any items remain; loads next item into
43      * holding 'next' field.
44      *
45      * @see java.util.Iterator#hasNext()
46      */

47     public boolean hasNext() {
48         return (this.next != null)? true: lookahead();
49     }
50     
51     /**
52      * Caches the next item if available.
53      *
54      * @return true if there was a next item to cache, false otherwise
55      */

56     protected abstract boolean lookahead();
57
58     /**
59      * Return the next item.
60      *
61      * @see java.util.Iterator#next()
62      */

63     public T next() {
64         if (!hasNext()) {
65             throw new NoSuchElementException JavaDoc();
66         }
67         // 'next' is guaranteed non-null by a hasNext() which returned true
68
T returnObj = this.next;
69         this.next = null;
70         return returnObj;
71     }
72     
73     /* (non-Javadoc)
74      * @see java.util.Iterator#remove()
75      */

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