| | |
| | | * Created by IntelliJ IDEA. |
| | | * User: Ulysses |
| | | * Date: 2007/6/15 |
| | | * Time: ¤W¤È 01:21:25 |
| | | * To change this template use File | Settings | File Templates. |
| | | */ |
| | | public class LangUtil |
| | | { |
| | | private static Map primitiveToWrapperMap = new HashMap() |
| | | { |
| | | public class LangUtil { |
| | | private static Map primitiveToWrapperMap = new HashMap() { |
| | | |
| | | { |
| | | put(byte.class, Byte.class); |
| | |
| | | } |
| | | }; |
| | | |
| | | public static String emptyStringIfNull(String s) |
| | | { |
| | | public static String emptyStringIfNull(String s) { |
| | | return (s == null) ? "" : s; |
| | | } |
| | | |
| | |
| | | * Useful because an expression used to generate o need only be |
| | | * evaluated once. |
| | | */ |
| | | public static Object ifNull(Object o, Object alternative) |
| | | { |
| | | public static Object ifNull(Object o, Object alternative) { |
| | | return (o == null) ? alternative : o; |
| | | } |
| | | |
| | | public static Object ifNotNull(Object o, Object alternative) |
| | | { |
| | | public static Object ifNotNull(Object o, Object alternative) { |
| | | return (o != null) ? alternative : o; |
| | | } |
| | | |
| | | public static Class toPrimitiveWrapperClass(Class primitiveClass) |
| | | { |
| | | public static Class toPrimitiveWrapperClass(Class primitiveClass) { |
| | | return (Class) primitiveToWrapperMap.get(primitiveClass); |
| | | } |
| | | |
| | | public static boolean isPrimitive(Class c) |
| | | { |
| | | public static boolean isPrimitive(Class c) { |
| | | return primitiveToWrapperMap.containsKey(c); |
| | | } |
| | | |
| | | public static boolean bothNullOrEqual(Object a, Object b) |
| | | { |
| | | public static boolean bothNullOrEqual(Object a, Object b) { |
| | | return (a == null && b == null) || (a != null && b != null && a.equals(b)); |
| | | } |
| | | |
| | | public static Object newInstance(Class c) |
| | | { |
| | | try |
| | | { |
| | | public static Object newInstance(Class c) { |
| | | try { |
| | | return c.newInstance(); |
| | | } catch (Exception e) |
| | | { |
| | | } catch (Exception e) { |
| | | Assert.shouldNeverReachHere(e.toString()); |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | public static Collection classesAndInterfaces(Class c) |
| | | { |
| | | public static Collection classesAndInterfaces(Class c) { |
| | | ArrayList classesAndInterfaces = new ArrayList(); |
| | | classesAndInterfaces.add(c); |
| | | superclasses(c, classesAndInterfaces); |
| | | for (Iterator i = new ArrayList(classesAndInterfaces).iterator(); i.hasNext();) |
| | | { |
| | | for (Iterator i = new ArrayList(classesAndInterfaces).iterator(); i.hasNext();) { |
| | | Class x = (Class) i.next(); |
| | | classesAndInterfaces.addAll(Arrays.asList(x.getInterfaces())); |
| | | } |
| | | return classesAndInterfaces; |
| | | } |
| | | |
| | | private static void superclasses(Class c, Collection results) |
| | | { |
| | | if (c.getSuperclass() == null) |
| | | { |
| | | private static void superclasses(Class c, Collection results) { |
| | | if (c.getSuperclass() == null) { |
| | | return; |
| | | } |
| | | results.add(c.getSuperclass()); |