KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > util > misc > IteratorUtil


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.appserv.management.util.misc;
24
25 import java.util.List JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 public final class IteratorUtil
30 {
31         private
32     IteratorUtil( )
33     {
34         // disallow instantiation
35
}
36     
37     
38         public static <T> Object JavaDoc[]
39     toArray( final Iterator JavaDoc<T> iter)
40     {
41         final List JavaDoc<T> list = new ArrayList JavaDoc<T>();
42         
43         while ( iter.hasNext() )
44         {
45             final T elem = iter.next();
46             list.add( elem );
47         }
48         
49         final Object JavaDoc[] result = new Object JavaDoc[ list.size() ];
50         list.toArray( result );
51         
52         return( ArrayConversion.specializeArray( result ) );
53     }
54     
55         public static Class JavaDoc
56     getUniformClass( final Iterator JavaDoc<?> iter)
57     {
58         Class JavaDoc theClass = null;
59         
60         if ( iter.hasNext() )
61         {
62             theClass = iter.next().getClass();
63         }
64
65         while ( iter.hasNext() )
66         {
67             if ( iter.next().getClass() != theClass )
68             {
69                 theClass = null;
70                 break;
71             }
72         }
73         
74         return( theClass );
75     }
76     
77         public static <T> boolean
78     isUniformClass(
79         final Iterator JavaDoc iter,
80         final Class JavaDoc<T> theClass,
81         final boolean exactMatch )
82     {
83         boolean isUniform = true;
84         
85         while ( iter.hasNext() )
86         {
87             final Class JavaDoc<?> nextClass = iter.next().getClass();
88             
89             if ( nextClass != theClass )
90             {
91                 if ( exactMatch )
92                 {
93                     isUniform = false;
94                     break;
95                 }
96                 
97                 if ( ! theClass.isAssignableFrom( nextClass ) )
98                 {
99                     isUniform = false;
100                     break;
101                 }
102             }
103         }
104         
105         return( isUniform );
106     }
107 }
108
109
Popular Tags