as(), casts the argument or returns null if it's not an instance of
the specified class. Yeah. I've had this laying around for a long time, almost embarrassed by it. But I keep running into places where it'd be handy, and now I'm going for it. git-svn-id: https://samskivert.googlecode.com/svn/trunk@2940 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -32,6 +32,14 @@ import com.samskivert.annotation.*;
|
|||||||
*/
|
*/
|
||||||
public class ObjectUtil
|
public class ObjectUtil
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Cast the specified Object, or return null if it is not an instance.
|
||||||
|
*/
|
||||||
|
public static <T> T as (Object obj, Class<T> clazz)
|
||||||
|
{
|
||||||
|
return clazz.isInstance(obj) ? clazz.cast(obj) : null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test two objects for equality safely.
|
* Test two objects for equality safely.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user