1 23 package com.sun.appserv.management.util.misc; 24 25 import java.util.List ; 26 import java.util.ArrayList ; 27 import java.util.Iterator ; 28 29 public final class IteratorUtil 30 { 31 private 32 IteratorUtil( ) 33 { 34 } 36 37 38 public static <T> Object [] 39 toArray( final Iterator <T> iter) 40 { 41 final List <T> list = new ArrayList <T>(); 42 43 while ( iter.hasNext() ) 44 { 45 final T elem = iter.next(); 46 list.add( elem ); 47 } 48 49 final Object [] result = new Object [ list.size() ]; 50 list.toArray( result ); 51 52 return( ArrayConversion.specializeArray( result ) ); 53 } 54 55 public static Class 56 getUniformClass( final Iterator <?> iter) 57 { 58 Class 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 iter, 80 final Class <T> theClass, 81 final boolean exactMatch ) 82 { 83 boolean isUniform = true; 84 85 while ( iter.hasNext() ) 86 { 87 final Class <?> 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 |