KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v1 > util > IteratorUtils


1 /*
2  * Distributed as part of debuggen v.0.1.0
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v1.util;
25
26 import java.util.*;
27 import java.lang.reflect.Array JavaDoc;
28
29 public final class IteratorUtils
30 {
31     public final static Iterator EMPTY_ITERATOR = new Iterator()
32     {
33     public boolean hasNext()
34     { return false; }
35
36     public Object JavaDoc next()
37     { throw new NoSuchElementException(); }
38
39     public void remove()
40     { throw new IllegalStateException JavaDoc(); }
41     };
42
43     public static Iterator oneElementUnmodifiableIterator(final Object JavaDoc elem)
44     {
45     return new Iterator()
46         {
47         boolean shot = false;
48
49         public boolean hasNext() { return (!shot); }
50
51         public Object JavaDoc next()
52         {
53             if (shot)
54             throw new NoSuchElementException();
55             else
56             {
57                 shot = true;
58                 return elem;
59             }
60         }
61
62         public void remove()
63         { throw new UnsupportedOperationException JavaDoc("remove() not supported."); }
64         };
65     }
66
67     public static boolean equivalent(Iterator ii, Iterator jj)
68     {
69     while (true)
70         {
71         boolean ii_hasnext = ii.hasNext();
72         boolean jj_hasnext = jj.hasNext();
73         if (ii_hasnext ^ jj_hasnext)
74             return false;
75         else if (ii_hasnext)
76             {
77             Object JavaDoc iiNext = ii.next();
78             Object JavaDoc jjNext = jj.next();
79             if (iiNext == jjNext)
80                 continue;
81             else if (iiNext == null)
82                 return false;
83             else if (!iiNext.equals(jjNext))
84                 return false;
85             }
86         else return true;
87         }
88     }
89
90     public static ArrayList toArrayList(Iterator ii, int initial_capacity)
91     {
92     ArrayList out = new ArrayList(initial_capacity);
93     while (ii.hasNext())
94         out.add(ii.next());
95     return out;
96     }
97
98     /**
99      * Fills an array with the contents of an iterator. If the array is too small,
100      * it will contain the first portion of the iterator. If the array can contain
101      * more elements than the iterator, extra elements are left untouched, unless
102      * null_terminate is set to true, in which case the element immediately following
103      * the last from the iterator is set to null. (This method is intended to make
104      * it easy to implement Collection.toArray(Object[] oo) methods...
105      *
106      * @param null_terminate iff there is extra space in the array, set the element
107      * immediately after the last from the iterator to null.
108      */

109     public static void fillArray(Iterator ii, Object JavaDoc[] fillMe, boolean null_terminate)
110     {
111     int i = 0;
112     int len = fillMe.length;
113     while ( i < len && ii.hasNext() )
114         fillMe[ i++ ] = ii.next();
115     if (null_terminate && i < len)
116         fillMe[i] = null;
117     }
118
119     public static void fillArray(Iterator ii, Object JavaDoc[] fillMe)
120     { fillArray( ii, fillMe, false); }
121
122     /**
123      * @param null_terminate iff there is extra space in the array, set the element
124      * immediately after the last from the iterator to null.
125      */

126     public static Object JavaDoc[] toArray(Iterator ii, int array_size, Class JavaDoc componentClass, boolean null_terminate)
127     {
128     Object JavaDoc[] out = (Object JavaDoc[]) Array.newInstance( componentClass, array_size );
129     fillArray(ii, out, null_terminate);
130     return out;
131     }
132
133     public static Object JavaDoc[] toArray(Iterator ii, int array_size, Class JavaDoc componentClass)
134     { return toArray( ii, array_size, componentClass, false ); }
135
136     /**
137      * Designed to help implement Collection.toArray(Object[] )methods... does
138      * the right thing if you can express an iterator and know the size of your
139      * Collection.
140      */

141     public static Object JavaDoc[] toArray(Iterator ii, int ii_size, Object JavaDoc[] maybeFillMe)
142     {
143     if (maybeFillMe.length >= ii_size)
144         {
145         fillArray( ii, maybeFillMe, true );
146         return maybeFillMe;
147         }
148     else
149         {
150         Class JavaDoc componentType = maybeFillMe.getClass().getComponentType();
151         return toArray( ii, ii_size, componentType );
152         }
153     }
154
155     private IteratorUtils()
156     {}
157 }
158
159
160
Popular Tags