From 73013697601b95922be5cb40bc09904d262fde04 Mon Sep 17 00:00:00 2001 From: "ray.j.greenwell" Date: Tue, 9 Nov 2010 00:53:26 +0000 Subject: [PATCH] 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 --- src/main/java/com/samskivert/util/ObjectUtil.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/com/samskivert/util/ObjectUtil.java b/src/main/java/com/samskivert/util/ObjectUtil.java index 0c1a442c..d6aa6d24 100644 --- a/src/main/java/com/samskivert/util/ObjectUtil.java +++ b/src/main/java/com/samskivert/util/ObjectUtil.java @@ -32,6 +32,14 @@ import com.samskivert.annotation.*; */ public class ObjectUtil { + /** + * Cast the specified Object, or return null if it is not an instance. + */ + public static T as (Object obj, Class clazz) + { + return clazz.isInstance(obj) ? clazz.cast(obj) : null; + } + /** * Test two objects for equality safely. */