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