1 23 24 package com.sun.appserv.management.util.misc; 25 26 import java.util.Collection ; 27 import java.util.Iterator ; 28 import java.util.Arrays ; 29 import java.lang.reflect.Array ; 30 31 import com.sun.appserv.management.util.stringifier.SmartStringifier; 32 33 36 public final class CollectionUtil 37 { 38 private 39 CollectionUtil( ) 40 { 41 } 43 44 47 public static String 48 toString( 49 final Collection c, 50 final String delim ) 51 { 52 final String [] strings = toStringArray( c ); 53 Arrays.sort( strings ); 54 55 return StringUtil.toString( delim, (Object [])strings ); 56 } 57 58 public static String 59 toString( final Collection c ) 60 { 61 return toString( c, ", " ); 62 } 63 64 65 public static <T> T 66 getSingleton( final Collection <T> s ) 67 { 68 if ( s.size() != 1 ) 69 { 70 throw new IllegalArgumentException (); 71 } 72 return( s.iterator().next() ); 73 } 74 75 78 public static <T> void 79 addArray( 80 final Collection <T> c, 81 final T[] array ) 82 { 83 for( int i = 0; i < array.length; ++i ) 84 { 85 c.add( array[ i ] ); 86 } 87 } 88 89 92 public static String [] 93 toStringArray( final Collection c ) 94 { 95 final String [] strings = new String [ c.size() ]; 96 97 int i = 0; 98 for( final Object o : c ) 99 { 100 strings[ i ] = SmartStringifier.toString( o ); 101 ++i; 102 } 103 104 return( strings ); 105 } 106 107 112 public static <T> T[] 113 toArray( 114 final Collection <? extends T> c, 115 final Class <T> elementClass ) 116 { 117 final T[] items = ArrayUtil.newArray( elementClass, c.size() ); 118 119 c.toArray( items ); 120 121 return items; 122 } 123 124 125 128 public static boolean 129 isAllStrings( final Collection <?> c ) 130 { 131 return IteratorUtil.getUniformClass( c.iterator() ) == String .class; 132 } 133 } 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | Popular Tags |