01: /*
02: (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
03: [See end of file]
04: $Id: IteratorCollection.java,v 1.5 2008/01/02 12:07:44 andy_seaborne Exp $
05: */
06:
07: package com.hp.hpl.jena.util;
08:
09: import java.util.ArrayList;
10: import java.util.Iterator;
11: import java.util.List;
12: import java.util.Set;
13:
14: import com.hp.hpl.jena.util.iterator.NiceIterator;
15:
16: /**
17: @author hedgehog
18: */
19: public class IteratorCollection {
20: /**
21: Only static methods here - the class cannot be instantiated.
22: */
23: private IteratorCollection() {
24: }
25:
26: /**
27: Answer the elements of the given iterator as a set. The iterator is consumed
28: by the operation. Even if an exception is thrown, the iterator will be closed.
29: @param i the iterator to convert
30: @return A set of the members of i
31: */
32: public static Set iteratorToSet(Iterator i) {
33: Set result = CollectionFactory.createHashedSet();
34: try {
35: while (i.hasNext())
36: result.add(i.next());
37: } finally {
38: NiceIterator.close(i);
39: }
40: return result;
41: }
42:
43: /**
44: Answer the elements of the given iterator as a list, in the order that they
45: arrived from the iterator. The iterator is consumed by this operation:
46: even if an exception is thrown, the iterator will be closed.
47: @param it the iterator to convert
48: @return a list of the elements of <code>it</code>, in order
49: */
50: public static List iteratorToList(Iterator it) {
51: List result = new ArrayList();
52: try {
53: while (it.hasNext())
54: result.add(it.next());
55: } finally {
56: NiceIterator.close(it);
57: }
58: return result;
59: }
60:
61: }
62:
63: /*
64: (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
65: All rights reserved.
66:
67: Redistribution and use in source and binary forms, with or without
68: modification, are permitted provided that the following conditions
69: are met:
70:
71: 1. Redistributions of source code must retain the above copyright
72: notice, this list of conditions and the following disclaimer.
73:
74: 2. Redistributions in binary form must reproduce the above copyright
75: notice, this list of conditions and the following disclaimer in the
76: documentation and/or other materials provided with the distribution.
77:
78: 3. The name of the author may not be used to endorse or promote products
79: derived from this software without specific prior written permission.
80:
81: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
82: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
83: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
84: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
85: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
86: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
87: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
88: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
89: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
90: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
91: */
|