001: /*
002: (c) Copyright 2003, 2004, 2005 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: NiceIterator.java,v 1.17 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: NiceIterator is the standard base class implementing ExtendedIterator. It provides
013: the static methods for <code>andThen</code>, <code>filterKeep</code> and
014: <code>filterDrop</code>; these can be reused from any other class. It defines
015: equivalent instance methods for descendants and to satisfy ExtendedIterator.
016: @author kers
017: */
018:
019: public class NiceIterator implements ExtendedIterator {
020: public NiceIterator() {
021: super ();
022: }
023:
024: /**
025: default close: don't need to do anything.
026: */
027: public void close() {
028: }
029:
030: /**
031: default hasNext: no elements, return false.
032: */
033: public boolean hasNext() {
034: return false;
035: }
036:
037: protected void ensureHasNext() {
038: if (hasNext() == false)
039: throw new NoSuchElementException();
040: }
041:
042: /**
043: default next: throw an exception.
044: */
045: public Object next() {
046: return noElements("empty NiceIterator");
047: }
048:
049: /**
050: Utility method for this and other (sub)classes: raise the appropriate
051: "no more elements" exception. I note that we raised the wrong exception
052: in at least one case ...
053:
054: @param message the string to include in the exception
055: @return never - but we have a return type to please the compiler
056: */
057: protected Object noElements(String message) {
058: throw new NoSuchElementException(message);
059: }
060:
061: /**
062: default remove: we have no elements, so we can't remove any.
063: */
064: public void remove() {
065: throw new UnsupportedOperationException(
066: "remove not supported for this iterator");
067: }
068:
069: /**
070: Answer the next object, and remove it.
071: */
072: public Object removeNext() {
073: Object result = next();
074: remove();
075: return result;
076: }
077:
078: /**
079: concatenate two closable iterators.
080: */
081:
082: public static ExtendedIterator andThen(final Iterator a,
083: final Iterator b) {
084: final List L = new ArrayList(2);
085: L.add(b);
086: return new NiceIterator() {
087: private int index = 0;
088:
089: private Iterator current = a;
090:
091: public boolean hasNext() {
092: while (current.hasNext() == false && index < L.size())
093: current = (Iterator) L.get(index++);
094: return current.hasNext();
095: }
096:
097: public Object next() {
098: return hasNext() ? current.next()
099: : noElements("concatenation");
100: }
101:
102: public void close() {
103: close(current);
104: for (int i = index; i < L.size(); i += 1)
105: close((Iterator) L.get(i));
106: }
107:
108: public void remove() {
109: current.remove();
110: }
111:
112: public ExtendedIterator andThen(ClosableIterator other) {
113: L.add(other);
114: return this ;
115: }
116: };
117: }
118:
119: /**
120: make a new iterator, which is us then the other chap.
121: */
122: public ExtendedIterator andThen(ClosableIterator other) {
123: return andThen(this , other);
124: }
125:
126: /**
127: make a new iterator, which is our elements that pass the filter
128: */
129: public ExtendedIterator filterKeep(Filter f) {
130: return new FilterKeepIterator(f, this );
131: }
132:
133: /**
134: make a new iterator, which is our elements that do not pass the filter
135: */
136: public ExtendedIterator filterDrop(final Filter f) {
137: return new FilterDropIterator(f, this );
138: }
139:
140: /**
141: make a new iterator which is the elementwise _map1_ of the base iterator.
142: */
143: public ExtendedIterator mapWith(Map1 map1) {
144: return new Map1Iterator(map1, this );
145: }
146:
147: /**
148: If <code>it</code> is a Closableiterator, close it. Abstracts away from
149: tests [that were] scattered through the code.
150: */
151: public static void close(Iterator it) {
152: if (it instanceof ClosableIterator)
153: ((ClosableIterator) it).close();
154: }
155:
156: static final private NiceIterator emptyInstance = new NiceIterator();
157:
158: /**
159: * An iterator over no elements.
160: * @return A class singleton which doesn't iterate.
161: */
162: static public ExtendedIterator emptyIterator() {
163: return emptyInstance;
164: }
165:
166: /**
167: Answer a list of the elements in order, consuming this iterator.
168: */
169: public List toList() {
170: return asList(this );
171: }
172:
173: /**
174: Answer a list of the elements in order, consuming this iterator.
175: */
176: public Set toSet() {
177: return asSet(this );
178: }
179:
180: /**
181: Answer a list of the elements of <code>it</code> in order, consuming this iterator.
182: Canonical implementation of toSet().
183: */
184: public static Set asSet(ExtendedIterator it) {
185: Set result = new HashSet();
186: while (it.hasNext())
187: result.add(it.next());
188: return result;
189: }
190:
191: /**
192: Answer a list of the elements from <code>it</code>, in order, consuming
193: that iterator. Canonical implementation of toList().
194: */
195: public static List asList(ExtendedIterator it) {
196: List result = new ArrayList();
197: while (it.hasNext())
198: result.add(it.next());
199: return result;
200: }
201: }
202:
203: /*
204: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
205: All rights reserved.
206:
207: Redistribution and use in source and binary forms, with or without
208: modification, are permitted provided that the following conditions
209: are met:
210:
211: 1. Redistributions of source code must retain the above copyright
212: notice, this list of conditions and the following disclaimer.
213:
214: 2. Redistributions in binary form must reproduce the above copyright
215: notice, this list of conditions and the following disclaimer in the
216: documentation and/or other materials provided with the distribution.
217:
218: 3. The name of the author may not be used to endorse or promote products
219: derived from this software without specific prior written permission.
220:
221: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
222: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
223: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
224: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
225: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
226: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
227: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
228: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
229: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
230: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
231: */
|