litesoft
@ 626
litesoft / trunk / GWT_Sandbox / FormEngine / src / com / temp / shared / utils / ObjectUtils.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
package com.temp.shared.utils; /** * Utility methods around Objects to reduce the code in other places (e.g. the * Activities). * * @author georgs */ public class ObjectUtils { @SuppressWarnings("unchecked") public static <T> T cast(Object o) { return (T) o; } public static <T> boolean oneOf(T toTest, T... options) { if (toTest != null) { for (T option : options) { if (toTest.equals(option)) { return true; } } } return false; } public static <T> T oneOfToString(String toTest, T... options) { if (toTest != null) { for (T option : options) { if (option != null && toTest.equals(option.toString())) { return option; } } } return null; } public static <T> T oneOfIgnoreCase(String toTest, T... options) { if (toTest != null) { for (T option : options) { if (option != null && toTest.equalsIgnoreCase(option.toString())) { return option; } } } return null; } public static <T> T deNull(T toTest, T defaultValue) { return (toTest != null) ? toTest : defaultValue; } public static boolean areEqual(Object o1, Object o2) { return (o1 == o2) || // Same Object or both null ((o1 != null) && o1.equals(o2)); } public static <O,C extends Comparable<C>> boolean areEqual(O o1, O o2, NoEqualsHelper<O,C> noEqualsHelper) { return (o1 == o2) || // Same Object or both null areEqual(noEqualsHelper.getProxyValue(o1), noEqualsHelper.getProxyValue(o2)); } /** * return the Simple Name of an Object (should NOT be used w/ Arrays). * * For inner classes the Simple Name is either the right side of the '$' if * it does NOT start with a digit, or the left side if it does! */ public static String getSimpleClassName(Object o) { return (o == null) ? "null" : getSimpleName(o.getClass()); } /** * return the Simple Name of a Class. * * For inner classes the Simple Name is either the right side of the '$' if * it does NOT start with a digit, or the left side if it does! */ public static String getSimpleName(Class<?> klass) { if (klass == null) { return "null"; } String name = "." + klass.getName(); name = name.substring(name.lastIndexOf('.') + 1); for (int at; -1 != (at = name.lastIndexOf('$'));) { String right = StringUtils.noEmpty(name.substring(at + 1), "1"); // "1" force ignore if Empty char c = right.charAt(0); name = ((c < '0') || ('9' < c)) ? right : StringUtils.noEmpty(name.substring(0, at), "?"); // "?" if Empty } return name; } public static int hashCode(Object value) { return (value == null) ? 0 : value.hashCode(); } public static int hashEm(Object... values) { int hashCode = 0; if (values != null) { for (Object value : values) { hashCode = (hashCode * 31) + hashCode(value); } } return hashCode; } public static <T> T oneOfToStringIgnoreCase( String value, T... options ) { if ( (value != null) && (options != null) ) { for ( T option : options ) { if ( (option != null) && value.equalsIgnoreCase( option.toString() ) ) { return option; } } } return null; } } |