KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*****************************************************************************
2  * Source code information
3  * -----------------------
4  * Original author Ian Dickinson, HP Labs Bristol
5  * Author email Ian.Dickinson@hp.com
6  * Package Jena
7  * Created 8 Aug 2001
8  * Filename $RCSfile: ConcatenatedIterator.java,v $
9  * Revision $Revision: 1.10 $
10  * Release status Preview-release $State: Exp $
11  *
12  * Last modified on $Date: 2005/02/21 12:19:14 $
13  * by $Author: andy_seaborne $
14  *
15  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
16  * (see footer for full conditions)
17  *****************************************************************************/

18
19 // Package
20
///////////////
21
package com.hp.hpl.jena.util.iterator;
22
23
24 // Imports
25
///////////////
26
import java.util.*;
27
28 /**
29  * An iterator that represents the concatenation of two individual iterators.
30  * The concatenated iterator will range over the elements of the first iterator,
31  * followed by the elements of the second.
32  *
33  * @author Ian Dickinson, HP Labs (<a HREF="mailto:Ian.Dickinson@hp.com">email</a>)
34  * @version CVS info: $Id: ConcatenatedIterator.java,v 1.10 2005/02/21 12:19:14 andy_seaborne Exp $
35  */

36 public class ConcatenatedIterator
37     implements Iterator
38 {
39     // Constants
40
//////////////////////////////////
41

42
43     // Static variables
44
//////////////////////////////////
45

46
47     // Instance variables
48
//////////////////////////////////
49

50     /** The first iterator */
51     private Iterator m_iter0 = null;
52
53     /** The second iterator */
54     private Iterator m_iter1 = null;
55
56     /** The default value for the iterator, or null if no default */
57     protected Object JavaDoc m_defaultValue = null;
58
59     /** A flag to show that the default value has been returned */
60     protected boolean m_defaultValueSeen = false;
61
62
63
64     // Constructors
65
//////////////////////////////////
66

67     /**
68      * Construct an iterator that is the concatenation of the two
69      * given iterators. Either iterator may be a Java iterator, or a Jena
70      * node or resource iterator.
71      *
72      * @param iter0 The first iterator. Elements of this iterator will appear
73      * first in the elements read from the concatenation.
74      * @param iter1 The second iterator. Elements of this iterator will appear
75      * second in the elements read from the concatenation.
76      */

77     public ConcatenatedIterator( Iterator iter0, Iterator iter1 ) {
78         m_iter0 = iter0;
79         m_iter1 = iter1;
80     }
81
82
83     // External signature methods
84
//////////////////////////////////
85

86     /**
87      * Returns true if the iteration has more elements. This will be
88      * true if either of the underlying iterators has more elements.
89      *
90      * @return true if the iterator has more elements.
91      */

92     public boolean hasNext() {
93         return m_iter0.hasNext() || m_iter1.hasNext() || (hasDefaultValue() && !m_defaultValueSeen);
94     }
95
96
97     /**
98      * Returns the next element in the interation.
99      *
100      * @return The next object in the iteration, which will correspond to the next object in the
101      * underlying iteration, projected to the range of the projection function.
102      * @exception NoSuchElementException - iteration has no more elements.
103      */

104     public Object JavaDoc next() {
105         boolean next0 = m_iter0.hasNext();
106         boolean next1 = m_iter1.hasNext();
107
108         // are there any more values from the encapsulted iterations?
109
if (next0 || next1) {
110             Object JavaDoc next = (next0) ? m_iter0.next() : m_iter1.next();
111
112             // is this the default value?
113
if (hasDefaultValue() && m_defaultValue.equals( next )) {
114                 m_defaultValueSeen = true;
115             }
116
117             return next;
118         }
119         else if (hasDefaultValue() && !m_defaultValueSeen) {
120             // return the default value for this iterator
121
m_defaultValueSeen = true;
122             return m_defaultValue;
123         }
124         else {
125             // no more nodes, so this is an error
126
throw new NoSuchElementException( "Tried to access next() element from empty concatenated iterator" );
127         }
128     }
129
130
131     /**
132      * Removes from the underlying collection the last element returned by
133      * the iterator (optional operation). Not supported on a concatenated
134      * iterator.
135      *
136      * @exception UnsupportedOperationException - if the remove operation is not
137      * supported by this Iterator.
138      * @exception IllegalStateException - if the next method has not yet been
139      * called, or the remove method has already been called after the
140      * last call to the next method.
141      */

142     public void remove() {
143         throw new UnsupportedOperationException JavaDoc( "Cannot remove elements from concatenated iterator" );
144     }
145
146
147     /**
148      * Set the default value for this iteration, which will be a value that
149      * is guaranteed to be returned as a member of the iteration. To guarantee
150      * that the default value is only returned if it has not already been
151      * returned by the iterator, setting the default value should occur before
152      * the first call to {@link #next}.
153      *
154      * @param defaultValue The default value for the iteration, or null for
155      * there to be no default value. The default default
156      * value is null.
157      */

158     public void setDefaultValue( Object JavaDoc defaultValue ) {
159         m_defaultValue = defaultValue;
160     }
161
162
163     /**
164      * Answer true if this iteration has a default value.
165      *
166      * @return true if there is a default value
167      */

168     public boolean hasDefaultValue() {
169         return m_defaultValue != null;
170     }
171
172
173
174     // Internal implementation methods
175
//////////////////////////////////
176

177
178     //==============================================================================
179
// Inner class definitions
180
//==============================================================================
181

182
183 }
184
185
186 /*
187     (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
188     All rights reserved.
189
190     Redistribution and use in source and binary forms, with or without
191     modification, are permitted provided that the following conditions
192     are met:
193
194     1. Redistributions of source code must retain the above copyright
195        notice, this list of conditions and the following disclaimer.
196
197     2. Redistributions in binary form must reproduce the above copyright
198        notice, this list of conditions and the following disclaimer in the
199        documentation and/or other materials provided with the distribution.
200
201     3. The name of the author may not be used to endorse or promote products
202        derived from this software without specific prior written permission.
203
204     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
205     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
206     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
207     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
208     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
209     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
210     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
211     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
212     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
213     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
214 */

215
216
Popular Tags