BooleanUtils Javadoc (#469)

* LANG-1480 getAbbreviatedName refactored to create appropriate length short class names

* LANG-1480 code fixed for special extreme case ".." abbreviated to 1 length should result ".." it was throwing exception. Tests are added

* import changed to avoid wild cards
apache master merged into current branch

* Mutable object import was moved to it's original place

* some accidental formatting reverted

* some accidental formatting reverted

* some accidental formatting reverted

* some accidental formatting reverted

* some accidental formatting reverted

* some accidental formatting reverted

* some accidental formatting reverted

* added another test case

* LANG-1480 fixing JavaDoc documentation as per requested by garydgregory

* LANG-1480 shortcut implemented, argument renamed, more tests

* LANG-1480 checkstyle update

* LANG-1492 tests methods modified to be public

* LANG-1480 imports rearranged

* LANG-1480 imports rearranged

* LANG-1480 imports rearranged

* some simplification that were there since before Java 1.4

* mainly unneccessary unboxing

* documentation partly reverted

* javadoc changes only

* ArrayUtils change reverted
This commit is contained in:
Peter Verhas 2019-12-21 14:39:19 +01:00 committed by Gary Gregory
parent 742eb8982b
commit 936dd601b7
1 changed files with 69 additions and 41 deletions

View File

@ -48,7 +48,8 @@ public class BooleanUtils {
*
* <p>If {@code null} is passed in, {@code null} will be returned.</p>
*
* <p>NOTE: This returns null and will throw a NullPointerException if unboxed to a boolean. </p>
* <p>NOTE: This returns {@code null} and will throw a {@code NullPointerException}
* if unboxed to a boolean. </p>
*
* <pre>
* BooleanUtils.negate(Boolean.TRUE) = Boolean.FALSE;
@ -78,7 +79,7 @@ public class BooleanUtils {
* BooleanUtils.isTrue(null) = false
* </pre>
*
* @param bool the boolean to check, null returns {@code false}
* @param bool the boolean to check, {@code null} returns {@code false}
* @return {@code true} only if the input is non-null and true
* @since 2.1
*/
@ -115,7 +116,7 @@ public class BooleanUtils {
* </pre>
*
* @param bool the boolean to check, null returns {@code false}
* @return {@code true} only if the input is non-null and false
* @return {@code true} only if the input is non-{@code null} and {@code false}
* @since 2.1
*/
public static boolean isFalse(final Boolean bool) {
@ -133,7 +134,7 @@ public class BooleanUtils {
* </pre>
*
* @param bool the boolean to check, null returns {@code true}
* @return {@code true} if the input is null or true
* @return {@code true} if the input is {@code null} or {@code true}
* @since 2.3
*/
public static boolean isNotFalse(final Boolean bool) {
@ -162,13 +163,16 @@ public class BooleanUtils {
* <p>Converts a Boolean to a boolean handling {@code null}.</p>
*
* <pre>
* BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true
* BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false
* BooleanUtils.toBooleanDefaultIfNull(null, true) = true
* BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, false) = true
* BooleanUtils.toBooleanDefaultIfNull(Boolean.TRUE, true) = true
* BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, true) = false
* BooleanUtils.toBooleanDefaultIfNull(Boolean.FALSE, false) = false
* BooleanUtils.toBooleanDefaultIfNull(null, true) = true
* BooleanUtils.toBooleanDefaultIfNull(null, false) = false
* </pre>
*
* @param bool the boolean to convert
* @param valueIfNull the boolean value to return if {@code null}
* @param bool the boolean object to convert to primitive
* @param valueIfNull the boolean value to return if the parameter {@code bool} is {@code null}
* @return {@code true} or {@code false}
*/
public static boolean toBooleanDefaultIfNull(final Boolean bool, final boolean valueIfNull) {
@ -182,7 +186,7 @@ public class BooleanUtils {
//-----------------------------------------------------------------------
/**
* <p>Converts an int to a boolean using the convention that {@code zero}
* is {@code false}.</p>
* is {@code false}, everything else is {@code true}.</p>
*
* <pre>
* BooleanUtils.toBoolean(0) = false
@ -200,7 +204,7 @@ public class BooleanUtils {
/**
* <p>Converts an int to a Boolean using the convention that {@code zero}
* is {@code false}.</p>
* is {@code false}, everything else is {@code true}.</p>
*
* <pre>
* BooleanUtils.toBoolean(0) = Boolean.FALSE
@ -218,11 +222,12 @@ public class BooleanUtils {
/**
* <p>Converts an Integer to a Boolean using the convention that {@code zero}
* is {@code false}.</p>
* is {@code false}, every other numeric value is {@code true}.</p>
*
* <p>{@code null} will be converted to {@code null}.</p>
*
* <p>NOTE: This returns null and will throw a NullPointerException if unboxed to a boolean. </p>
* <p>NOTE: This method may return {@code null} and may throw a {@code NullPointerException}
* if unboxed to a {@code boolean}.</p>
*
* <pre>
* BooleanUtils.toBoolean(Integer.valueOf(0)) = Boolean.FALSE
@ -244,18 +249,23 @@ public class BooleanUtils {
/**
* <p>Converts an int to a boolean specifying the conversion values.</p>
*
* <p>If the {@code trueValue} and {@code falseValue} are the same number then
* the return value will be {@code true} in case {@code value} matches it.</p>
*
* <pre>
* BooleanUtils.toBoolean(0, 1, 0) = false
* BooleanUtils.toBoolean(1, 1, 0) = true
* BooleanUtils.toBoolean(1, 1, 1) = true
* BooleanUtils.toBoolean(2, 1, 2) = false
* BooleanUtils.toBoolean(2, 2, 0) = true
* </pre>
*
* @param value the Integer to convert
* @param value the {@code Integer} to convert
* @param trueValue the value to match for {@code true}
* @param falseValue the value to match for {@code false}
* @return {@code true} or {@code false}
* @throws IllegalArgumentException if no match
* @throws IllegalArgumentException if {@code value} does not match neither
* {@code trueValue} no {@code falseValue}
*/
public static boolean toBoolean(final int value, final int trueValue, final int falseValue) {
if (value == trueValue) {
@ -264,7 +274,6 @@ public class BooleanUtils {
if (value == falseValue) {
return false;
}
// no match
throw new IllegalArgumentException("The Integer did not match either specified value");
}
@ -298,18 +307,24 @@ public class BooleanUtils {
} else if (value.equals(falseValue)) {
return false;
}
// no match
throw new IllegalArgumentException("The Integer did not match either specified value");
}
/**
* <p>Converts an int to a Boolean specifying the conversion values.</p>
*
* <p>NOTE: This returns null and will throw a NullPointerException if unboxed to a boolean. </p>
* <p>NOTE: This method may return {@code null} and may throw a {@code NullPointerException}
* if unboxed to a {@code boolean}.</p>
*
* <p>The checks are done first for the {@code trueValue}, then for the {@code falseValue} and
* finally for the {@code nullValue}.</p>
*
* <pre>
* BooleanUtils.toBooleanObject(0, 0, 2, 3) = Boolean.TRUE
* BooleanUtils.toBooleanObject(0, 0, 0, 3) = Boolean.TRUE
* BooleanUtils.toBooleanObject(0, 0, 0, 0) = Boolean.TRUE
* BooleanUtils.toBooleanObject(2, 1, 2, 3) = Boolean.FALSE
* BooleanUtils.toBooleanObject(2, 1, 2, 2) = Boolean.FALSE
* BooleanUtils.toBooleanObject(3, 1, 2, 3) = null
* </pre>
*
@ -330,18 +345,24 @@ public class BooleanUtils {
if (value == nullValue) {
return null;
}
// no match
throw new IllegalArgumentException("The Integer did not match any specified value");
}
/**
* <p>Converts an Integer to a Boolean specifying the conversion values.</p>
*
* <p>NOTE: This returns null and will throw a NullPointerException if unboxed to a boolean. </p>
* <p>NOTE: This method may return {@code null} and may throw a {@code NullPointerException}
* if unboxed to a {@code boolean}.</p>
*
* <p>The checks are done first for the {@code trueValue}, then for the {@code falseValue} and
* finally for the {@code nullValue}.</p>
**
* <pre>
* BooleanUtils.toBooleanObject(Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.TRUE
* BooleanUtils.toBooleanObject(Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(3)) = Boolean.TRUE
* BooleanUtils.toBooleanObject(Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0), Integer.valueOf(0)) = Boolean.TRUE
* BooleanUtils.toBooleanObject(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = Boolean.FALSE
* BooleanUtils.toBooleanObject(Integer.valueOf(2), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(2)) = Boolean.FALSE
* BooleanUtils.toBooleanObject(Integer.valueOf(3), Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)) = null
* </pre>
*
@ -370,7 +391,6 @@ public class BooleanUtils {
} else if (value.equals(nullValue)) {
return null;
}
// no match
throw new IllegalArgumentException("The Integer did not match any specified value");
}
@ -378,7 +398,7 @@ public class BooleanUtils {
//-----------------------------------------------------------------------
/**
* <p>Converts a boolean to an int using the convention that
* {@code zero} is {@code false}.</p>
* {@code true} is {@code 1} and {@code false} is {@code 0}.</p>
*
* <pre>
* BooleanUtils.toInteger(true) = 1
@ -394,7 +414,7 @@ public class BooleanUtils {
/**
* <p>Converts a boolean to an Integer using the convention that
* {@code zero} is {@code false}.</p>
* {@code true} is {@code 1} and {@code false} is {@code 0}.</p>
*
* <pre>
* BooleanUtils.toIntegerObject(true) = Integer.valueOf(1)
@ -518,7 +538,8 @@ public class BooleanUtils {
* (case insensitive) will return {@code false}.
* Otherwise, {@code null} is returned.</p>
*
* <p>NOTE: This returns null and will throw a NullPointerException if unboxed to a boolean. </p>
* <p>NOTE: This method may return {@code null} and may throw a {@code NullPointerException}
* if unboxed to a {@code boolean}.</p>
*
* <pre>
* // N.B. case is not significant
@ -636,12 +657,19 @@ public class BooleanUtils {
/**
* <p>Converts a String to a Boolean throwing an exception if no match.</p>
*
* <p>NOTE: This returns null and will throw a NullPointerException if unboxed to a boolean. </p>
* <p>NOTE: This method may return {@code null} and may throw a {@code NullPointerException}
* if unboxed to a {@code boolean}.</p>
*
* <pre>
* BooleanUtils.toBooleanObject("true", "true", "false", "null") = Boolean.TRUE
* BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE
* BooleanUtils.toBooleanObject("null", "true", "false", "null") = null
* BooleanUtils.toBooleanObject("true", "true", "false", "null") = Boolean.TRUE
* BooleanUtils.toBooleanObject(null, null, "false", "null") = Boolean.TRUE
* BooleanUtils.toBooleanObject(null, null, null, "null") = Boolean.TRUE
* BooleanUtils.toBooleanObject(null, null, null, null) = Boolean.TRUE
* BooleanUtils.toBooleanObject("false", "true", "false", "null") = Boolean.FALSE
* BooleanUtils.toBooleanObject("false", "true", "false", "false") = Boolean.FALSE
* BooleanUtils.toBooleanObject(null, "true", null, "false") = Boolean.FALSE
* BooleanUtils.toBooleanObject(null, "true", null, null) = Boolean.FALSE
* BooleanUtils.toBooleanObject("null", "true", "false", "null") = null
* </pre>
*
* @param str the String to check
@ -735,7 +763,6 @@ public class BooleanUtils {
return false;
}
}
// no match
throw new IllegalArgumentException("The String did not match either specified value");
}
@ -884,7 +911,7 @@ public class BooleanUtils {
// logical operations
// ----------------------------------------------------------------------
/**
* <p>Performs an and on a set of booleans.</p>
* <p>Performs an 'and' operation on a set of booleans.</p>
*
* <pre>
* BooleanUtils.and(true, true) = true
@ -895,7 +922,8 @@ public class BooleanUtils {
* </pre>
*
* @param array an array of {@code boolean}s
* @return {@code true} if the and is successful.
* @return the result of the logical 'and' operation. That is {@code false}
* if any of the parameters is {@code false} and {@code true} otherwise.
* @throws IllegalArgumentException if {@code array} is {@code null}
* @throws IllegalArgumentException if {@code array} is empty.
* @since 3.0.1
@ -917,7 +945,7 @@ public class BooleanUtils {
}
/**
* <p>Performs an and on an array of Booleans.</p>
* <p>Performs an 'and' operation on an array of Booleans.</p>
*
* <pre>
* BooleanUtils.and(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE
@ -929,7 +957,8 @@ public class BooleanUtils {
* </pre>
*
* @param array an array of {@code Boolean}s
* @return {@code true} if the and is successful.
* @return the result of the logical 'and' operation. That is {@code false}
* if any of the parameters is {@code false} and {@code true} otherwise.
* @throws IllegalArgumentException if {@code array} is {@code null}
* @throws IllegalArgumentException if {@code array} is empty.
* @throws IllegalArgumentException if {@code array} contains a {@code null}
@ -951,7 +980,7 @@ public class BooleanUtils {
}
/**
* <p>Performs an or on a set of booleans.</p>
* <p>Performs an 'or' operation on a set of booleans.</p>
*
* <pre>
* BooleanUtils.or(true, true) = true
@ -963,13 +992,12 @@ public class BooleanUtils {
* </pre>
*
* @param array an array of {@code boolean}s
* @return {@code true} if the or is successful.
* @return {@code true} if any of the arguments is {@code true}, and it returns {@code false} otherwise.
* @throws IllegalArgumentException if {@code array} is {@code null}
* @throws IllegalArgumentException if {@code array} is empty.
* @since 3.0.1
*/
public static boolean or(final boolean... array) {
// Validates input
if (array == null) {
throw new IllegalArgumentException("The Array must not be null");
}
@ -985,7 +1013,7 @@ public class BooleanUtils {
}
/**
* <p>Performs an or on an array of Booleans.</p>
* <p>Performs an 'or' operation on an array of Booleans.</p>
*
* <pre>
* BooleanUtils.or(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE
@ -998,7 +1026,7 @@ public class BooleanUtils {
* </pre>
*
* @param array an array of {@code Boolean}s
* @return {@code true} if the or is successful.
* @return {@code true} if any of the arguments is {@code true}, and it returns {@code false} otherwise.
* @throws IllegalArgumentException if {@code array} is {@code null}
* @throws IllegalArgumentException if {@code array} is empty.
* @throws IllegalArgumentException if {@code array} contains a {@code null}
@ -1029,12 +1057,11 @@ public class BooleanUtils {
* </pre>
*
* @param array an array of {@code boolean}s
* @return {@code true} if the xor is successful.
* @return the result of the xor operations
* @throws IllegalArgumentException if {@code array} is {@code null}
* @throws IllegalArgumentException if {@code array} is empty.
*/
public static boolean xor(final boolean... array) {
// Validates input
if (array == null) {
throw new IllegalArgumentException("The Array must not be null");
}
@ -1058,10 +1085,11 @@ public class BooleanUtils {
* BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) = Boolean.FALSE
* BooleanUtils.xor(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) = Boolean.FALSE
* BooleanUtils.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) = Boolean.TRUE
* BooleanUtils.xor(Boolean.TRUE, Boolean.FALSE, Boolean.FALSE) = Boolean.TRUE
* </pre>
*
* @param array an array of {@code Boolean}s
* @return {@code true} if the xor is successful.
* @return the result of the xor operations
* @throws IllegalArgumentException if {@code array} is {@code null}
* @throws IllegalArgumentException if {@code array} is empty.
* @throws IllegalArgumentException if {@code array} contains a {@code null}