1 23 package com.sun.appserv.management.util.misc; 24 25 26 import java.util.List ; 27 import java.util.Map ; 28 import java.util.Set ; 29 import java.util.SortedSet ; 30 import java.util.Collection ; 31 import java.util.Collections ; 32 import java.util.Hashtable ; 33 34 import java.io.Serializable ; 35 36 import javax.management.ObjectName ; 37 38 39 88 public final class TypeCast 89 { 90 91 98 @SuppressWarnings ("unchecked") public static <T> Collection <T> 100 asCollection( final Object o ) 101 { 102 return (Collection <T>)Collection .class.cast( o ); 103 } 104 105 111 public static <T extends Serializable > Collection <T> 112 asSerializableCollection( final Object c ) 113 { 114 final Collection <T> result = asCollection( c ); 115 checkSerializable( result ); 116 return result; 117 } 118 119 126 @SuppressWarnings ("unchecked") public static <K,V> Map <K,V> 128 asMap( final Object m) 129 { 130 return (Map <K,V>)Map .class.cast( m ); 131 } 132 133 139 public static <K extends Serializable ,V extends Serializable > Map <K,V> 140 asSerializableMap( final Object m) 141 { 142 final Map <K,V> result = asMap( m ); 143 checkSerializable( result ); 144 145 return result; 146 } 147 148 154 @SuppressWarnings ("unchecked") public static <K,V> Hashtable <K,V> 156 asHashtable( final Object o ) 157 { 158 return (Hashtable <K,V>)Hashtable .class.cast( o ); 159 } 160 161 167 @SuppressWarnings ("unchecked") public static <T> List <T> 169 asList( final Object list) 170 { 171 return (List <T>)List .class.cast( list ); 172 } 173 174 180 public static <T extends Serializable > List <T> 181 asSerializableList( final Object list) 182 { 183 final List <T> result = asList( list ); 184 checkSerializable( result ); 185 return result; 186 } 187 188 194 @SuppressWarnings ("unchecked") public static <T> Set <T> 196 asSet( final Object s ) 197 { 198 return (Set <T>)Set .class.cast( s ); 199 } 200 201 207 @SuppressWarnings ("unchecked") public static <T> SortedSet <T> 209 asSortedSet( final Object s ) 210 { 211 return (SortedSet <T>)Set .class.cast( s ); 212 } 213 214 220 public static <T extends Serializable > Set <T> 221 asSerializableSet( final Object s ) 222 { 223 final Set <T> result = asSet( s ); 224 checkSerializable( result ); 225 return result; 226 } 227 228 229 233 @SuppressWarnings ("unchecked") public static <T> Class <T> 235 asClass( final Class <?> c ) 236 { 237 if ( ! ( c instanceof Class ) ) 238 { 239 throw new IllegalArgumentException ( "" + c ); 240 } 241 242 return (Class <T>)Class .class.cast( c ); 243 } 244 245 246 250 @SuppressWarnings ("unchecked") public static <T> T[] 252 asArray( final Object o ) 253 { 254 return (T[])Object [].class.cast( o ); 255 } 256 257 258 262 public static void 263 checkSerializable( final Object [] a ) 264 { 265 for( final Object o : a ) 266 { 267 checkSerializable( o ); 268 } 269 } 270 271 275 public static void 276 checkSerializableElements( final Collection <?> l ) 277 { 278 for( final Object o : l ) 279 { 280 checkSerializable( o ); 281 } 282 } 283 284 285 289 public static Collection <Serializable > 290 checkSerializable( final Collection <?> l ) 291 { 292 checkSerializable( l, true ); 293 return asCollection( l ); 294 } 295 296 303 public static Collection <Serializable > 304 checkSerializable( final Collection <?> l, boolean collectionItself ) 305 { 306 if ( collectionItself ) 307 { 308 checkSerializable( (Object )l ); 309 } 310 311 checkSerializableElements( l ); 312 313 return asCollection( l ); 314 } 315 316 321 public static Map <Serializable ,Serializable > 322 checkSerializable( final Map <?,?> m ) 323 { 324 checkSerializable( (Object )m ); 325 326 checkSerializable( m.keySet(), false); 332 checkSerializable( m.values(), false); 333 334 return asMap( m ); 335 } 336 337 341 public static Serializable 342 checkSerializable( final Object o ) 343 { 344 if ( (o != null) && ! (o instanceof Serializable ) ) 345 { 346 throw new ClassCastException ( "Object not Serializable, class = " + o.getClass().getName() ); 347 } 348 349 return Serializable .class.cast( o ); 350 } 351 352 353 355 362 public static <T> Collection <T> 363 checkCollection( final Collection <?> c, final Class <T> theClass ) 364 { 365 if ( c != null ) 366 { 367 for( final Object o : c ) 368 { 369 checkObject( o, theClass ); 370 } 371 } 372 373 return asCollection(c); 374 } 375 376 383 public static <T> List <T> 384 checkList( final List <?> l, final Class <T> theClass ) 385 { 386 if ( l != null ) 387 { 388 for( final Object o : l ) 389 { 390 checkObject( o, theClass ); 391 } 392 } 393 394 return asList(l); 395 } 396 397 404 public static <T> Set <T> 405 checkSet( final Set <?> s, final Class <T> theClass ) 406 { 407 if ( s != null ) 408 { 409 for( final Object o : s ) 410 { 411 checkObject( o, theClass ); 412 } 413 } 414 415 return asSet(s); 416 } 417 418 426 public static <K,V> Map <K,V> 427 checkMap( 428 final Map <?,?> m, 429 final Class <K> keyClass, 430 final Class <V> valueClass ) 431 { 432 if ( m != null ) 433 { 434 checkSet( m.keySet(), keyClass ); 435 checkCollection( m.values(), valueClass ); 436 } 437 438 return asMap( m ); 439 } 440 441 448 @SuppressWarnings ("unchecked") 449 public static <T> T 450 checkObject( final Object o, final Class <T> theClass ) 451 { 452 if ( o != null && ! theClass.isAssignableFrom( o.getClass() ) ) 453 { 454 throw new ClassCastException ( "Object of class " + o.getClass().getName() + 455 " not assignment compatible with: " + theClass.getName() ); 456 } 457 return (T)o; 458 } 459 460 467 public static <T> void 468 checkArray( final Object [] a, final Class <T> theClass ) 469 { 470 for( final Object o : a ) 471 { 472 checkObject( o, theClass ); 473 } 474 } 475 476 477 483 public static Collection <String > 484 checkedStringCollection( final Collection <?> c ) 485 { 486 return checkedCollection( c, String .class ); 487 } 488 489 495 public static Set <String > 496 checkedStringSet( final Set <?> s ) 497 { 498 return checkedSet( s, String .class ); 499 } 500 501 507 public static List <String > 508 checkedStringList( final List <?> l ) 509 { 510 return checkedList( l, String .class ); 511 } 512 513 519 public static Map <String ,String > 520 checkedStringMap( final Map <?,?> m ) 521 { 522 return checkedMap( m, String .class, String .class); 523 } 524 525 526 527 533 public static <T> Collection <T> 534 checkedCollection( final Collection <?> c, final Class <T> theClass ) 535 { 536 final Collection <T> cc = checkCollection( c, theClass ); 537 return Collections.checkedCollection( cc, theClass ); 538 } 539 540 546 public static <T> Set <T> 547 checkedSet( final Set <?> s, final Class <T> theClass ) 548 { 549 final Set <T> cs = checkSet( s, theClass ); 550 return Collections.checkedSet( cs, theClass ); 551 } 552 553 559 public static <T> List <T> 560 checkedList( final List <?> l, final Class <T> theClass ) 561 { 562 final List <T> cl = checkList( l, theClass ); 563 return Collections.checkedList( cl, theClass ); 564 } 565 566 572 public static <K,V> Map <K,V> 573 checkedMap( final Map <?,?> m, final Class <K> keyClass, final Class <V> valueClass ) 574 { 575 final Map <K,V> cm = checkMap( m, keyClass, valueClass ); 576 return Collections.checkedMap( cm, keyClass, valueClass ); 577 } 578 } 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 | Popular Tags |