|
| 1 | +/* |
| 2 | + * Copyright (C) 2024-2024 OnixByte. |
| 3 | + * |
| 4 | + * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0 |
| 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 | + * |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.onixbyte.devkit.utils; |
| 19 | + |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Objects; |
| 22 | +import java.util.function.BooleanSupplier; |
| 23 | + |
| 24 | +/** |
| 25 | + * A util for boolean calculations. |
| 26 | + * |
| 27 | + * @author shaoxinke |
| 28 | + * @version 1.6.2 |
| 29 | + */ |
| 30 | +public final class BoolUtil { |
| 31 | + |
| 32 | + /** |
| 33 | + * Logical and calculation. |
| 34 | + * |
| 35 | + * @param values the values to be calculated |
| 36 | + * @return {@code true} if all value in values is {@code true}, otherwise {@code false} |
| 37 | + */ |
| 38 | + public static boolean and(Boolean... values) { |
| 39 | + return Arrays.stream(values) |
| 40 | + .filter(Objects::nonNull) |
| 41 | + .allMatch(Boolean::booleanValue); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Logical and calculation. |
| 46 | + * |
| 47 | + * @param valueSuppliers the suppliers of value to be calculated |
| 48 | + * @return {@code true} if all value in values is {@code true}, otherwise {@code false} |
| 49 | + */ |
| 50 | + public static boolean and(BooleanSupplier... valueSuppliers) { |
| 51 | + return Arrays.stream(valueSuppliers) |
| 52 | + .filter(Objects::nonNull) |
| 53 | + .allMatch(BooleanSupplier::getAsBoolean); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Logical or calculation. |
| 58 | + * |
| 59 | + * @param values the values to be calculated |
| 60 | + * @return {@code true} if any value in values is {@code true}, otherwise {@code false} |
| 61 | + */ |
| 62 | + public static boolean or(Boolean... values) { |
| 63 | + return Arrays.stream(values) |
| 64 | + .filter(Objects::nonNull) |
| 65 | + .anyMatch(Boolean::booleanValue); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Logical or calculation. |
| 70 | + * |
| 71 | + * @param valueSuppliers the suppliers of value to be calculated |
| 72 | + * @return {@code true} if any value in values is {@code true}, otherwise {@code false} |
| 73 | + */ |
| 74 | + public static boolean or(BooleanSupplier... valueSuppliers) { |
| 75 | + return Arrays.stream(valueSuppliers) |
| 76 | + .filter(Objects::nonNull) |
| 77 | + .anyMatch(BooleanSupplier::getAsBoolean); |
| 78 | + } |
| 79 | + |
| 80 | +} |
0 commit comments