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