KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: WrappedIterator.java,v 1.8 2005/02/21 12:19:17 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.util.iterator;
8
9 import java.util.*;
10
11 /**
12     a WrappedIterator is an ExtendedIterator wrapping around a plain (or
13     presented as plain) Iterator. The wrapping allows the usual extended
14     operations (filtering, concatenating) to be done on an Iterator derived
15     from some other source.
16 <br>
17     @author kers
18 */

19
20 public class WrappedIterator extends NiceIterator
21     {
22     /**
23          set to <code>true</code> if this wrapping doesn't permit the use of .remove().
24          Otherwise the .remove() is delegated to the base iterator.
25     */

26     protected boolean removeDenied;
27     
28     /**
29         factory method for creating a wrapper around _it_. We reserve
30         the right to deliver the argument if it's already an extended iterator.
31     */

32     public static ExtendedIterator create( Iterator it )
33         { return it instanceof ExtendedIterator ? (ExtendedIterator) it : new WrappedIterator( it, false ); }
34     
35     public static WrappedIterator createNoRemove( Iterator it )
36         { return new WrappedIterator( it, true ); }
37       
38     /** the base iterator that we wrap */
39     private Iterator base;
40     
41     /** constructor: remember the base iterator */
42     protected WrappedIterator( Iterator base )
43         { this( base, false ); }
44     
45     /**
46          Initialise this wrapping with the given base iterator and remove-control.
47      * @param base the base iterator that this tierator wraps
48      * @param removeDenied true if .remove() must throw an exception
49      */

50     protected WrappedIterator( Iterator base, boolean removeDenied )
51         { this.base = base;
52         this.removeDenied = removeDenied; }
53         
54     /** hasNext: defer to the base iterator */
55     public boolean hasNext()
56         { return base.hasNext(); }
57         
58     /** next: defer to the base iterator */
59     public Object JavaDoc next()
60         { return base.next(); }
61         
62     /**
63          if .remove() is allowed, delegate to the abse iterator's .remove; otherwise,
64          throw an UnsupportedOperationException.
65     */

66     public void remove()
67         {
68         if (removeDenied) throw new UnsupportedOperationException JavaDoc();
69         base.remove();
70         }
71         
72     /** close: defer to the base, iff it is closable */
73     public void close()
74         { close( base ); }
75
76     /**
77         if _it_ is a Closableiterator, close it. Abstracts away from
78         tests [that were] scattered through the code.
79     */

80     public static void close( Iterator it )
81         { NiceIterator.close( it ); }
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
92     1. Redistributions of source code must retain the above copyright
93        notice, this list of conditions and the following disclaimer.
94
95     2. Redistributions in binary form must reproduce the above copyright
96        notice, this list of conditions and the following disclaimer in the
97        documentation and/or other materials provided with the distribution.
98
99     3. The name of the author may not be used to endorse or promote products
100        derived from this software without specific prior written permission.
101
102     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
103     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
104     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
105     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
106     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
107     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
108     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
109     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
110     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
111     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
112 */

113
Popular Tags