KickJava   Java API By Example, From Geeks To Geeks.

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


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

6
7 package com.hp.hpl.jena.util.iterator;
8
9 import java.util.*;
10
11 /**
12     NiceIterator is the standard base class implementing ExtendedIterator. It provides
13     the static methods for <code>andThen</code>, <code>filterKeep</code> and
14     <code>filterDrop</code>; these can be reused from any other class. It defines
15     equivalent instance methods for descendants and to satisfy ExtendedIterator.
16     @author kers
17 */

18
19 public class NiceIterator implements ExtendedIterator
20     {
21     public NiceIterator()
22         { super(); }
23
24     /**
25         default close: don't need to do anything.
26     */

27     public void close()
28         { }
29
30     /**
31         default hasNext: no elements, return false.
32     */

33     public boolean hasNext()
34         { return false; }
35
36     protected void ensureHasNext()
37         { if (hasNext() == false) throw new NoSuchElementException(); }
38     
39     /**
40         default next: throw an exception.
41     */

42     public Object JavaDoc next()
43         { return noElements( "empty NiceIterator" ); }
44         
45     /**
46         Utility method for this and other (sub)classes: raise the appropriate
47         "no more elements" exception. I note that we raised the wrong exception
48         in at least one case ...
49     
50         @param message the string to include in the exception
51         @return never - but we have a return type to please the compiler
52     */

53     protected Object JavaDoc noElements( String JavaDoc message )
54         { throw new NoSuchElementException( message ); }
55         
56     /**
57         default remove: we have no elements, so we can't remove any.
58     */

59     public void remove()
60         {
61         throw new UnsupportedOperationException JavaDoc( "remove not supported for this iterator" );
62         }
63     
64     /**
65          Answer the next object, and remove it.
66     */

67     public Object JavaDoc removeNext()
68         { Object JavaDoc result = next(); remove(); return result; }
69         
70     /**
71         concatenate two closable iterators.
72     */

73     
74     public static ExtendedIterator andThen( final Iterator a, final Iterator b )
75         {
76         return new NiceIterator()
77             {
78             private boolean walkingA = true;
79             
80             public boolean hasNext()
81                 { return (walkingA = a.hasNext()) || b.hasNext(); }
82                 
83             public Object JavaDoc next()
84                 { return (walkingA = a.hasNext()) ? a.next() : b.next(); }
85                 
86             public void close()
87                 {
88                 close( a );
89                 close( b );
90                 }
91                 
92             public void remove()
93                 { (walkingA ? a : b).remove(); }
94             };
95         }
96         
97     /**
98         make a new iterator, which is us then the other chap.
99     */

100     public ExtendedIterator andThen( ClosableIterator other )
101         { return andThen( this, other ); }
102         
103     /**
104         make a new iterator, which is our elements that pass the filter
105     */

106     public ExtendedIterator filterKeep( Filter f )
107         { return new FilterIterator( f, this ); }
108
109     /**
110         make a new iterator, which is our elements that pass the filter
111     */

112     public ExtendedIterator filterDrop( final Filter f )
113         {
114         Filter notF = new Filter() { public boolean accept( Object JavaDoc x ) { return !f.accept( x ); } };
115         return new FilterIterator( notF, this );
116         }
117    
118     /**
119         make a new iterator which is the elementwise _map1_ of the base iterator.
120     */

121     public ExtendedIterator mapWith( Map1 map1 )
122         { return new Map1Iterator( map1, this ); }
123
124     /**
125         If <code>it</code> is a Closableiterator, close it. Abstracts away from
126         tests [that were] scattered through the code.
127     */

128     public static void close( Iterator it )
129         { if (it instanceof ClosableIterator) ((ClosableIterator) it).close(); }
130    
131     static final private NiceIterator emptyInstance = new NiceIterator();
132     
133     /**
134      * An iterator over no elements.
135      * @return A class singleton which doesn't iterate.
136      */

137     static public ExtendedIterator emptyIterator() {
138         return emptyInstance;
139     }
140   
141     }
142
143 /*
144     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
145     All rights reserved.
146
147     Redistribution and use in source and binary forms, with or without
148     modification, are permitted provided that the following conditions
149     are met:
150
151     1. Redistributions of source code must retain the above copyright
152        notice, this list of conditions and the following disclaimer.
153
154     2. Redistributions in binary form must reproduce the above copyright
155        notice, this list of conditions and the following disclaimer in the
156        documentation and/or other materials provided with the distribution.
157
158     3. The name of the author may not be used to endorse or promote products
159        derived from this software without specific prior written permission.
160
161     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
162     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
163     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
164     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
165     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
166     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
167     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
168     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
169     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
170     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
171 */

172
Popular Tags