View Javadoc

1   /* TransformingIteratorWrapper
2   *
3   * $Id: TransformingIteratorWrapper.java 4650 2006-09-25 18:09:42Z paul_jack $
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;
28  
29  /***
30   * Superclass for Iterators which transform and/or filter results
31   * from a wrapped Iterator. Because transform() has the option of 
32   * discarding an item from the inner Iterator (by returning null), 
33   * this is a kind of LookaheadIterator. 
34   * 
35   * @author gojomo
36   */
37  public abstract class TransformingIteratorWrapper<Original,Transformed> 
38  extends LookaheadIterator<Transformed> {
39      protected Iterator<Original> inner;
40      
41      
42      /* (non-Javadoc)
43       * @see org.archive.util.iterator.LookaheadIterator#lookahead()
44       */
45      protected boolean lookahead() {
46          assert next == null : "looking ahead when next is already loaded";
47          while(inner.hasNext()) {
48              next = transform(inner.next());
49              if(next!=null) {
50                  return true;
51              }
52          }
53          noteExhausted();
54          return false;
55      }
56  
57      /***
58       * Any cleanup to occur when hasNext() is about to return false
59       */
60      protected void noteExhausted() {
61          // by default, do nothing
62          
63      }
64  
65      /***
66       * @param object Object to transform.
67       * @return Transfomed object.
68       */
69      protected abstract Transformed transform(Original object);
70  
71  }