1 23 package com.sun.appserv.management.util.misc; 24 25 import java.util.Set ; 26 import java.util.Collection ; 27 import java.util.Collections ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 31 34 public final class SetUtil 35 { 36 private 37 SetUtil( ) 38 { 39 } 41 42 public static <T> T 43 getSingleton( final Set <T> s ) 44 { 45 if ( s.size() != 1 ) 46 { 47 throw new IllegalArgumentException ( s.toString() ); 48 } 49 return( s.iterator().next() ); 50 } 51 52 55 public static <T> void 56 addArray( 57 final Set <T> set, 58 final T[] array ) 59 { 60 for( int i = 0; i < array.length; ++i ) 61 { 62 set.add( array[ i ] ); 63 } 64 } 65 66 69 public static String [] 70 toStringArray( final Set set ) 71 { 72 final String [] strings = new String [ set.size() ]; 73 74 final Iterator iter = set.iterator(); 75 int i = 0; 76 while ( iter.hasNext() ) 77 { 78 strings[ i ] = iter.next().toString(); 79 ++i; 80 } 81 82 return( strings ); 83 } 84 85 88 public static Object [] 89 toArray( final Set <?> set ) 90 { 91 final Object [] names = new Object [ set.size() ]; 92 set.toArray( names ); 93 94 return( names ); 95 } 96 97 100 public static <T> Set <T> 101 newSet( final Collection <T> c ) 102 { 103 final Set <T> set = new HashSet <T>(); 104 105 set.addAll( c ); 106 107 return( set ); 108 } 109 110 111 112 113 117 public static <T> Set <T> 118 newSingletonSet( final T m1 ) 119 { 120 final Set <T> set = new HashSet <T>(); 121 122 set.add( m1 ); 123 124 return( set ); 125 } 126 127 131 public static <T> HashSet <T> 132 copySet( final Set <T> s1 ) 133 { 134 final HashSet <T> set = new HashSet <T>(); 135 136 set.addAll( s1 ); 137 138 return( set ); 139 } 140 141 142 145 public static <T> Set <T> 146 newSet( final Set <? extends T>[] sets ) 147 { 148 final Set <T> s = new HashSet <T>(); 149 150 for( int i = 0; i < sets.length; ++i ) 151 { 152 s.addAll( sets[ i ] ); 153 } 154 155 return( s ); 156 } 157 158 188 189 190 191 194 public static <T> Set <T> 195 newSet( 196 final Set <? extends T> s1, 197 final Set <? extends T> s2) 198 { 199 final Set <T> result = new HashSet <T>(); 200 result.addAll( s1 ); 201 result.addAll( s2 ); 202 203 return result; 204 } 205 206 209 public static <T> Set <T> 210 newSet( 211 final Set <? extends T> s1, 212 final Set <? extends T> s2, 213 final Set <? extends T> s3 ) 214 { 215 return newSet( newSet( s1, s2 ), s3 ); 216 } 217 218 221 public static <T> Set <T> 222 newSet( 223 final Set <? extends T> s1, 224 final Set <? extends T> s2, 225 final Set <? extends T> s3, 226 final Set <? extends T> s4 ) 227 { 228 return newSet( newSet( s1, s2 ), newSet( s3, s4) ); 229 } 230 231 public static Set <String > 232 newUnmodifiableSet( final String [] objects ) 233 { 234 return GSetUtil.newUnmodifiableStringSet( objects ); 235 } 236 237 240 public static <T> Set <T> 241 newSet( final T... objects ) 242 { 243 return( newSet( objects, 0, objects.length ) ); 244 } 245 246 247 250 public static <T> Set <T> 251 newSet( 252 final T [] objects, 253 final int startIndex, 254 final int numItems ) 255 { 256 final Set <T> set = new HashSet <T>(); 257 258 for( int i = 0; i < numItems; ++i ) 259 { 260 set.add( objects[ startIndex + i ] ); 261 } 262 263 return( set ); 264 } 265 266 269 public static <T> Set <T> 270 intersectSets( 271 final Set <T> set1, 272 final Set <T> set2 ) 273 { 274 final Set <T> result = SetUtil.newSet( set1 ); 275 result.retainAll( set2 ); 276 277 return( result ); 278 } 279 280 283 public static <T> Set <T> 284 removeSet( 285 final Set <T> set1, 286 final Set <T> set2 ) 287 { 288 final Set <T> result = SetUtil.newSet( set1 ); 289 result.removeAll( set2 ); 290 291 return( result ); 292 } 293 294 297 public static <T> Set <T> 298 newNotCommonSet( 299 final Set <T> set1, 300 final Set <T> set2 ) 301 { 302 final Set <T> result = SetUtil.newSet( set1, set2 ); 303 final Set <T> common = intersectSets( set1, set2); 304 305 result.removeAll( common ); 306 307 return( result ); 308 } 309 310 } 311 312 | Popular Tags |