KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > util > IteratorCollection


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

6
7 package com.hp.hpl.jena.util;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.Set JavaDoc;
13
14 import com.hp.hpl.jena.util.iterator.NiceIterator;
15
16
17 /**
18  @author hedgehog
19  */

20 public class IteratorCollection
21     {
22     /**
23         Only static methods here - the class cannot be instantiated.
24     */

25     private IteratorCollection()
26         {}
27     
28     /**
29         Answer the elements of the given iterator as a set. The iterator is consumed
30         by the operation. Even if an exception is thrown, the iterator will be closed.
31         @param i the iterator to convert
32         @return A set of the members of i
33     */

34     public static Set JavaDoc iteratorToSet( Iterator JavaDoc i )
35         {
36         Set JavaDoc result = CollectionFactory.createHashedSet();
37         try { while (i.hasNext()) result.add( i.next() ); }
38         finally { NiceIterator.close( i ); }
39         return result;
40         }
41
42     /**
43         Answer the elements of the given iterator as a list, in the order that they
44         arrived from the iterator. The iterator is consumed by this operation:
45         even if an exception is thrown, the iterator will be closed.
46         @param it the iterator to convert
47         @return a list of the elements of <code>it</code>, in order
48      */

49     public static List JavaDoc iteratorToList( Iterator JavaDoc it )
50         {
51         List JavaDoc result = new ArrayList JavaDoc();
52         try { while (it.hasNext()) result.add( it.next() ); }
53         finally { NiceIterator.close( it ); }
54         return result;
55         }
56
57     }
58
59
60 /*
61     (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
62     All rights reserved.
63
64     Redistribution and use in source and binary forms, with or without
65     modification, are permitted provided that the following conditions
66     are met:
67
68     1. Redistributions of source code must retain the above copyright
69        notice, this list of conditions and the following disclaimer.
70
71     2. Redistributions in binary form must reproduce the above copyright
72        notice, this list of conditions and the following disclaimer in the
73        documentation and/or other materials provided with the distribution.
74
75     3. The name of the author may not be used to endorse or promote products
76        derived from this software without specific prior written permission.
77
78     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
79     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
80     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
81     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
82     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
83     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
84     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
85     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
86     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
87     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
88 */
Popular Tags