1 23 package com.sun.appserv.management.util.misc; 24 25 import java.util.Set ; 26 import java.util.HashSet ; 27 import java.util.Collection ; 28 import java.util.Collections ; 29 import java.util.Arrays ; 30 31 32 35 public final class GSetUtil 36 { 37 private 38 GSetUtil( ) 39 { 40 } 42 43 public static <T> T 44 getSingleton( final Set <T> s ) 45 { 46 if ( s.size() != 1 ) 47 { 48 throw new IllegalArgumentException ( s.toString() ); 49 } 50 return( s.iterator().next() ); 51 } 52 53 public static <T> void 54 addArray( 55 final Set <T> set, 56 final T[] array ) 57 { 58 for( final T item : array ) 59 { 60 set.add( item ); 61 } 62 } 63 64 public static <T> Set <T> 65 newSet( final Collection <T> c ) 66 { 67 final HashSet <T> set = new HashSet <T>(); 68 69 set.addAll( c ); 70 71 return( set ); 72 } 73 74 75 78 public static <T> Set <T> 79 newSet( final T item ) 80 { 81 final Set <T> set = new HashSet <T>(); 82 set.add( item ); 83 84 return( set ); 85 } 86 87 91 public static <T> HashSet <T> 92 copySet( final Set <? extends T> s1 ) 93 { 94 final HashSet <T> set = new HashSet <T>(); 95 96 set.addAll( s1 ); 97 98 return( set ); 99 } 100 101 102 public static <T> Set <? extends T> 103 newSet( 104 final T m1, 105 final T m2 ) 106 { 107 final HashSet <T> set = new HashSet <T>(); 108 109 set.add( m1 ); 110 set.add( m2 ); 111 112 return( set ); 113 } 114 115 131 132 public static <T> Set <T> 133 newSet( 134 final T m1, 135 final T m2, 136 final T m3, 137 final T m4 ) 138 { 139 final HashSet <T> set = new HashSet <T>(); 140 141 set.add( m1 ); 142 set.add( m2 ); 143 set.add( m3 ); 144 set.add( m4 ); 145 146 return( set ); 147 } 148 149 150 153 public static <T> Set <T> 154 newSet( final T[] objects ) 155 { 156 return( newSet( objects, 0, objects.length ) ); 157 } 158 159 public static <T,TT extends T> Set <T> 160 newSet( final Set <T> s1, final Set <TT> s2 ) 161 { 162 final Set <T> both = new HashSet <T>(); 163 both.addAll( s1 ); 164 both.addAll( s2 ); 165 166 return both; 167 } 168 169 170 171 174 public static <T> Set <T> 175 newSet( 176 final T[] objects, 177 final int startIndex, 178 final int numItems ) 179 { 180 final Set <T> set = new HashSet <T>(); 181 182 for( int i = 0; i < numItems; ++i ) 183 { 184 set.add( objects[ startIndex + i ] ); 185 } 186 187 return( set ); 188 } 189 190 193 public static String [] 194 toStringArray( final Set <?> s ) 195 { 196 final String [] strings = new String [ s.size() ]; 197 198 int i = 0; 199 for( final Object o : s ) 200 { 201 strings[ i ] = "" + o; 202 ++i; 203 } 204 205 return( strings ); 206 } 207 208 public static String [] 209 toSortedStringArray( final Set <?> s ) 210 { 211 final String [] strings = toStringArray( s ); 212 213 Arrays.sort( strings ); 214 215 return( strings ); 216 } 217 218 public static Set <String > 219 newStringSet( final String ... args) 220 { 221 return newUnmodifiableSet( args ); 222 } 223 224 225 public static <T> Set <T> 226 newUnmodifiableSet( final T... args) 227 { 228 final Set <T> set = new HashSet <T>(); 229 230 for( final T s : args ) 231 { 232 set.add( s ); 233 } 234 return set; 235 } 236 237 public static Set <String > 238 newUnmodifiableStringSet( final String ... args) 239 { 240 return Collections.unmodifiableSet( newStringSet( args ) ); 241 } 242 243 public static Set <String > 244 newStringSet( final Object ... args) 245 { 246 final Set <String > set = new HashSet <String >(); 247 248 for( final Object o : args ) 249 { 250 set.add( o == null ? null : "" + o ); 251 } 252 return set; 253 } 254 255 256 260 public static <T> Set <T> 261 newSingletonSet( final T m1 ) 262 { 263 final Set <T> set = new HashSet <T>(); 264 265 set.add( m1 ); 266 267 return( set ); 268 } 269 270 271 274 public static <T> Set <T> 275 intersectSets( 276 final Set <T> set1, 277 final Set <T> set2 ) 278 { 279 final Set <T> result = newSet( set1 ); 280 result.retainAll( set2 ); 281 282 return( result ); 283 } 284 285 286 289 public static <T> Set <T> 290 removeSet( 291 final Set <T> set1, 292 final Set <T> set2 ) 293 { 294 final Set <T> result = SetUtil.newSet( set1 ); 295 result.removeAll( set2 ); 296 297 return( result ); 298 } 299 300 301 304 public static <T> Set <T> 305 newNotCommonSet( 306 final Set <T> set1, 307 final Set <T> set2 ) 308 { 309 final Set <T> result = newSet( set1, set2 ); 310 final Set <T> common = intersectSets( set1, set2); 311 312 result.removeAll( common ); 313 314 return( result ); 315 } 316 } 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | Popular Tags |