| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Truth |
|
| 3.0;3 |
| 1 | /** | |
| 2 | * Copyright 2005-2011 The Kuali Foundation | |
| 3 | * | |
| 4 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
| 5 | * you may not use this file except in compliance with the License. | |
| 6 | * You may obtain a copy of the License at | |
| 7 | * | |
| 8 | * http://www.opensource.org/licenses/ecl2.php | |
| 9 | * | |
| 10 | * Unless required by applicable law or agreed to in writing, software | |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | * See the License for the specific language governing permissions and | |
| 14 | * limitations under the License. | |
| 15 | */ | |
| 16 | package org.kuali.rice.core.api.util; | |
| 17 | ||
| 18 | import java.util.Arrays; | |
| 19 | import java.util.Collection; | |
| 20 | import java.util.Collections; | |
| 21 | ||
| 22 | 0 | public enum Truth { |
| 23 | 0 | TRUE("true", "yes", "Y", "on", "1", "t", "enabled"), |
| 24 | 0 | FALSE("false", "no", "N", "off", "0", "f", "disabled"); |
| 25 | ||
| 26 | private final Collection<String> truthStrings; | |
| 27 | ||
| 28 | 0 | private Truth(String... vals) { |
| 29 | 0 | truthStrings = Collections.unmodifiableCollection(Arrays.asList(vals)); |
| 30 | 0 | } |
| 31 | ||
| 32 | public Collection<String> getTruthStrings() { | |
| 33 | 0 | return truthStrings; |
| 34 | } | |
| 35 | ||
| 36 | /** | |
| 37 | * Returns the Boolean value of a string ignoring case. | |
| 38 | * | |
| 39 | * If the string is not a recognized boolean value, then this method | |
| 40 | * will return null. If the str is null, this method will return null. | |
| 41 | * | |
| 42 | * @param str the string. | |
| 43 | * @return the boolean value or null. | |
| 44 | */ | |
| 45 | public static Boolean strToBooleanIgnoreCase(String str) { | |
| 46 | 0 | return strToBooleanIgnoreCase(str, null); |
| 47 | } | |
| 48 | ||
| 49 | /** | |
| 50 | * Returns the Boolean value of a string ignoring case. | |
| 51 | * | |
| 52 | * If the string is not a recognized boolean value, then this method | |
| 53 | * will return null. If the str is null, this method will return null. | |
| 54 | * | |
| 55 | * @param str the string. | |
| 56 | * @param defaultValue default value to use when the str is not a recognized as a boolean value. | |
| 57 | * @return the boolean value or the specified default value. | |
| 58 | */ | |
| 59 | public static Boolean strToBooleanIgnoreCase(String str, Boolean defaultValue) { | |
| 60 | 0 | if (str == null) { |
| 61 | 0 | return defaultValue; |
| 62 | } | |
| 63 | ||
| 64 | 0 | for (String s : TRUE.getTruthStrings()) { |
| 65 | 0 | if (s.equalsIgnoreCase(str)) { |
| 66 | 0 | return Boolean.TRUE; |
| 67 | } | |
| 68 | } | |
| 69 | ||
| 70 | 0 | for (String s : FALSE.getTruthStrings()) { |
| 71 | 0 | if (s.equalsIgnoreCase(str)) { |
| 72 | 0 | return Boolean.FALSE; |
| 73 | } | |
| 74 | } | |
| 75 | ||
| 76 | 0 | return defaultValue; |
| 77 | } | |
| 78 | } |