Add BooleanUtils.values() and forEach().

This commit is contained in:
Gary Gregory 2022-09-16 10:22:39 -04:00
parent 19612d4134
commit e81855a208
3 changed files with 50 additions and 3 deletions

View File

@ -177,6 +177,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Processor.Type.getLabel().</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Processor.toString().</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add HashCodeBuilder.equals(Object).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add BooleanUtils.values() and forEach().</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Dependabot, XenoAmess, Gary Gregory">Bump actions/cache from 2.1.4 to 3.0.8 #742, #752, #764, #833, #867.</action>
<action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump actions/checkout from 2 to 3.0.2 #819, #825, #859.</action>

View File

@ -16,6 +16,11 @@
*/
package org.apache.commons.lang3;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import org.apache.commons.lang3.math.NumberUtils;
/**
@ -29,6 +34,9 @@ import org.apache.commons.lang3.math.NumberUtils;
* @since 2.0
*/
public class BooleanUtils {
private static final List<Boolean> BOOLEAN_LIST = Collections.unmodifiableList(Arrays.asList(Boolean.FALSE, Boolean.TRUE));
/**
* The false String {@code "false"}.
*
@ -152,6 +160,16 @@ public class BooleanUtils {
return x ? 1 : -1;
}
/**
* Performs the given action for each Boolean {@link BooleanUtils#values()}.
*
* @param action The action to be performed for each element
* @since 3.13.0
*/
public static void forEach(final Consumer<Boolean> action) {
values().forEach(action);
}
/**
* Checks if a {@link Boolean} value is {@code false},
* handling {@code null} by returning {@code false}.
@ -223,7 +241,6 @@ public class BooleanUtils {
public static boolean isTrue(final Boolean bool) {
return Boolean.TRUE.equals(bool);
}
/**
* Negates the specified boolean.
*
@ -247,6 +264,7 @@ public class BooleanUtils {
}
return bool.booleanValue() ? Boolean.FALSE : Boolean.TRUE;
}
/**
* Performs a one-hot on an array of booleans.
* <p>
@ -1121,6 +1139,16 @@ public class BooleanUtils {
return toString(bool, YES, NO, null);
}
/**
* Returns an unmodifiable list of Booleans {@code [false, true]}.
*
* @return an unmodifiable list of Booleans {@code [false, true]}.
* @since 3.13.0
*/
public static List<Boolean> values() {
return BOOLEAN_LIST;
}
/**
* Performs an xor on a set of booleans.
* <p>

View File

@ -28,20 +28,38 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.Test;
/**
* Unit tests {@link org.apache.commons.lang3.BooleanUtils}.
* Unit tests {@link BooleanUtils}.
*/
public class BooleanUtilsTest extends AbstractLangTest {
@Test
public void test_booleanValues() {
final Boolean[] expected = {false, true};
final Boolean[] expected = {Boolean.FALSE, Boolean.TRUE};
assertArrayEquals(sort(expected), BooleanUtils.booleanValues());
}
@Test
public void test_values() {
final List<Boolean> expected = Arrays.asList(Boolean.FALSE, Boolean.TRUE);
Collections.sort(expected);
assertEquals(expected, BooleanUtils.values());
}
@Test
public void test_forEach() {
final List<Boolean> list = new ArrayList<>();
BooleanUtils.forEach(list::add);
assertEquals(Arrays.asList(Boolean.FALSE, Boolean.TRUE), list);
}
@Test
public void test_isFalse_Boolean() {
assertFalse(BooleanUtils.isFalse(Boolean.TRUE));