01: /* TransformingIteratorWrapper
02: *
03: * $Id: TransformingIteratorWrapper.java 4650 2006-09-25 18:09:42Z paul_jack $
04: *
05: * Created on Mar 25, 2005
06: *
07: * Copyright (C) 2005 Internet Archive.
08: *
09: * 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: /* (non-Javadoc)
42: * @see org.archive.util.iterator.LookaheadIterator#lookahead()
43: */
44: protected boolean lookahead() {
45: assert next == null : "looking ahead when next is already loaded";
46: while (inner.hasNext()) {
47: next = transform(inner.next());
48: if (next != null) {
49: return true;
50: }
51: }
52: noteExhausted();
53: return false;
54: }
55:
56: /**
57: * Any cleanup to occur when hasNext() is about to return false
58: */
59: protected void noteExhausted() {
60: // by default, do nothing
61:
62: }
63:
64: /**
65: * @param object Object to transform.
66: * @return Transfomed object.
67: */
68: protected abstract Transformed transform(Original object);
69:
70: }
|