1 | |
package org.kuali.rice.core.api.util; |
2 | |
|
3 | |
import java.util.Arrays; |
4 | |
import java.util.Collection; |
5 | |
import java.util.Collections; |
6 | |
|
7 | 0 | public enum Truth { |
8 | 0 | TRUE("true", "yes", "Y", "on", "1", "t", "enabled"), |
9 | 0 | FALSE("false", "no", "N", "off", "0", "f", "disabled"); |
10 | |
|
11 | |
private final Collection<String> truthStrings; |
12 | |
|
13 | 0 | private Truth(String... vals) { |
14 | 0 | truthStrings = Collections.unmodifiableCollection(Arrays.asList(vals)); |
15 | 0 | } |
16 | |
|
17 | |
public Collection<String> getTruthStrings() { |
18 | 0 | return truthStrings; |
19 | |
} |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
public static Boolean strToBooleanIgnoreCase(String str) { |
31 | 0 | return strToBooleanIgnoreCase(str, null); |
32 | |
} |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public static Boolean strToBooleanIgnoreCase(String str, Boolean defaultValue) { |
45 | 0 | if (str == null) { |
46 | 0 | return defaultValue; |
47 | |
} |
48 | |
|
49 | 0 | for (String s : TRUE.getTruthStrings()) { |
50 | 0 | if (s.equalsIgnoreCase(str)) { |
51 | 0 | return Boolean.TRUE; |
52 | |
} |
53 | |
} |
54 | |
|
55 | 0 | for (String s : FALSE.getTruthStrings()) { |
56 | 0 | if (s.equalsIgnoreCase(str)) { |
57 | 0 | return Boolean.FALSE; |
58 | |
} |
59 | |
} |
60 | |
|
61 | 0 | return defaultValue; |
62 | |
} |
63 | |
} |