All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.cuioss.tools.base.BooleanOperations Maven / Gradle / Ivy

Go to download

Utility Library acting as a replacement for googles guava, certain apache-commons libraries and logging facades/frameworks.

There is a newer version: 2.1.1
Show newest version
/*
 * Copyright 2023 the original author or authors.
 * 

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

* https://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package de.cuioss.tools.base; import lombok.experimental.UtilityClass; /** * Utility Class providing some boolean operations. In essence, it simplifies * 'or' and 'and' operations. Some examples from the unit-tests: * *

 *
 * @Test
 * void shouldDetectAnyTrue() {
 *     assertTrue(BooleanOperations.isAnyTrue(true));
 *     assertTrue(BooleanOperations.isAnyTrue(true, true));
 *     assertTrue(BooleanOperations.isAnyTrue(true, false));
 *     assertFalse(BooleanOperations.isAnyTrue(false, false));
 *     // Not really sensible, but defined contract -> Corner Case
 *     assertFalse(BooleanOperations.isAnyTrue());
 *     assertFalse(BooleanOperations.isAnyTrue(null));
 * }
 *
 * @Test
 * void shouldDetectAnyFalse() {
 *     assertFalse(BooleanOperations.isAnyFalse(true));
 *     assertTrue(BooleanOperations.isAnyFalse(true, false));
 *     assertTrue(BooleanOperations.isAnyFalse(false, false));
 *     // Not really sensible, but defined contract -> Corner Case
 *     assertFalse(BooleanOperations.isAnyFalse());
 *     assertFalse(BooleanOperations.isAnyFalse(null));
 * }
 *
 * @Test
 * void shouldDetectAllFalse() {
 *     assertFalse(BooleanOperations.areAllFalse(true));
 *     assertFalse(BooleanOperations.areAllFalse(true, false));
 *     assertFalse(BooleanOperations.areAllFalse(true, true));
 *     assertTrue(BooleanOperations.areAllFalse(false, false));
 *     // Not really sensible, but defined contract -> Corner Case
 *     assertFalse(BooleanOperations.areAllFalse());
 *     assertFalse(BooleanOperations.areAllFalse(null));
 * }
 *
 * @Test
 * void shouldDetectAllTrue() {
 *     assertTrue(BooleanOperations.areAllTrue(true));
 *     assertFalse(BooleanOperations.areAllTrue(true, false));
 *     assertTrue(BooleanOperations.areAllTrue(true, true));
 *     assertFalse(BooleanOperations.areAllTrue(false, false));
 *     // Not really sensible, but defined contract -> Corner Case
 *     assertTrue(BooleanOperations.areAllTrue());
 *     assertTrue(BooleanOperations.areAllTrue(null));
 * }
 * 
* * * * @author Eugen Fischer * @author Oliver Wolff */ @UtilityClass public final class BooleanOperations { private static boolean containsTrue(final boolean... parameter) { if (isEmpty(parameter)) { return false; } for (boolean element : parameter) { if (element) { return true; } } return false; } private static boolean isEmpty(boolean[] parameter) { return null == parameter || 0 == parameter.length; } private static boolean containsFalse(final boolean... parameter) { if (isEmpty(parameter)) { return false; } for (boolean element : parameter) { if (!element) { return true; } } return false; } /** * Shorthand for checking if at least one of the given booleans is {@code true} * * @param parameter ellipsis of boolean values * @return {@code true} if one of parameters is {@code true}, {@code false} * otherwise */ public static boolean isAnyTrue(final boolean... parameter) { return containsTrue(parameter); } /** * Shorthand for checking if all the given booleans are {@code true} * * @param parameter ellipsis of boolean values * @return {@code true} if all parameters are {@code true} or no parameter is * given ratio: no given false, {@code false} otherwise */ public static boolean areAllTrue(final boolean... parameter) { if (isEmpty(parameter)) { return true; } return !containsFalse(parameter); } /** * Shorthand for checking if all given booleans are {@code false} * * @param parameter ellipsis of boolean values * @return {@code true} if all parameters are {@code false}, {@code true} * otherwise. {@code false} if no parameter is passed, ratio: no given * false */ public static boolean areAllFalse(final boolean... parameter) { if (isEmpty(parameter)) { return false; } return !containsTrue(parameter); } /** * Shorthand for checking if at least one of the given booleans is {@code false} * * @param parameter ellipsis of boolean values * @return {@code true} if one of parameters is {@code false}, {@code true} * otherwise */ public static boolean isAnyFalse(final boolean... parameter) { return containsFalse(parameter); } /** * @param value to be checked * @return true, if the given value represents a boolean value i.e. "true" or * "false" ignoring case. */ public static boolean isValidBoolean(String value) { return Boolean.TRUE.toString().equalsIgnoreCase(value) || Boolean.FALSE.toString().equalsIgnoreCase(value); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy