001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: WrappedIterator.java,v 1.13 2008/01/02 12:07:36 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.util.iterator;
008:
009: import java.util.*;
010:
011: /**
012: a WrappedIterator is an ExtendedIterator wrapping around a plain (or
013: presented as plain) Iterator. The wrapping allows the usual extended
014: operations (filtering, concatenating) to be done on an Iterator derived
015: from some other source.
016: <br>
017: @author kers
018: */
019:
020: public class WrappedIterator extends NiceIterator {
021: /**
022: set to <code>true</code> if this wrapping doesn't permit the use of
023: .remove(). Otherwise the .remove() is delegated to the base iterator.
024: */
025: protected boolean removeDenied;
026:
027: /**
028: Answer an ExtendedIterator returning the elements of <code>it</code>.
029: If <code>it</code> is itself an ExtendedIterator, return that; otherwise
030: wrap <code>it</code>.
031: */
032: public static ExtendedIterator create(Iterator it) {
033: return it instanceof ExtendedIterator ? (ExtendedIterator) it
034: : new WrappedIterator(it, false);
035: }
036:
037: /**
038: Answer an ExtendedIterator wrapped round <code>it</code> which does not
039: permit <code>.remove()</code> even if <code>it</code> does.
040: */
041: public static WrappedIterator createNoRemove(Iterator it) {
042: return new WrappedIterator(it, true);
043: }
044:
045: /** the base iterator that we wrap */
046: protected final Iterator base;
047:
048: public Iterator forTestingOnly_getBase() {
049: return base;
050: }
051:
052: /** constructor: remember the base iterator */
053: protected WrappedIterator(Iterator base) {
054: this (base, false);
055: }
056:
057: /**
058: Initialise this wrapping with the given base iterator and remove-control.
059: @param base the base iterator that this tierator wraps
060: @param removeDenied true if .remove() must throw an exception
061: */
062: protected WrappedIterator(Iterator base, boolean removeDenied) {
063: this .base = base;
064: this .removeDenied = removeDenied;
065: }
066:
067: /** hasNext: defer to the base iterator */
068: public boolean hasNext() {
069: return base.hasNext();
070: }
071:
072: /** next: defer to the base iterator */
073: public Object next() {
074: return base.next();
075: }
076:
077: /**
078: if .remove() is allowed, delegate to the base iterator's .remove;
079: otherwise, throw an UnsupportedOperationException.
080: */
081: public void remove() {
082: if (removeDenied)
083: throw new UnsupportedOperationException();
084: base.remove();
085: }
086:
087: /** close: defer to the base, iff it is closable */
088: public void close() {
089: close(base);
090: }
091:
092: /**
093: if <code>it</code> is a Closableiterator, close it. Abstracts away from
094: tests [that were] scattered through the code.
095: */
096: public static void close(Iterator it) {
097: NiceIterator.close(it);
098: }
099: }
100:
101: /*
102: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
103: All rights reserved.
104:
105: Redistribution and use in source and binary forms, with or without
106: modification, are permitted provided that the following conditions
107: are met:
108:
109: 1. Redistributions of source code must retain the above copyright
110: notice, this list of conditions and the following disclaimer.
111:
112: 2. Redistributions in binary form must reproduce the above copyright
113: notice, this list of conditions and the following disclaimer in the
114: documentation and/or other materials provided with the distribution.
115:
116: 3. The name of the author may not be used to endorse or promote products
117: derived from this software without specific prior written permission.
118:
119: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
120: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
121: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
122: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
123: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
124: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
125: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
126: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
127: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
128: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
129: */
|