From: Roland Häder Date: Sat, 12 Nov 2022 01:13:08 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=5e25b3104ba355649043d89c7cec51aa4cf366f3;p=jcore-utils.git Continued: - added null-safe utility class BooleanUtils --- diff --git a/src/org/mxchange/jcoreutils/bool/BooleanUtils.java b/src/org/mxchange/jcoreutils/bool/BooleanUtils.java new file mode 100644 index 0000000..c1e041d --- /dev/null +++ b/src/org/mxchange/jcoreutils/bool/BooleanUtils.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2022 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.jcoreutils.bool; + +import java.util.Objects; + +/** + * Utilities class for Boolean instances (not primitive boolean) + *

+ * @author Roland Häder + */ +public class BooleanUtils { + + /** + * Compares two Boolean instances with each other + *

+ * @param boolean1 Boolean instance 1 + * @param boolean2 Boolean instance 2 + *

+ * @return Comparison value + */ + public static int compare (final Boolean boolean1, final Boolean boolean2) { + // Check parameter + if (Objects.equals(boolean1, boolean2)) { + // Same object + return 0; + } else if (null == boolean1) { + // Left side is null + return -1; + } else if (null == boolean2) { + // Right side is null + return 1; + } + + // Compare instances + return boolean1.compareTo(boolean2); + } + + /** + * No instance from utility classes + */ + private BooleanUtils () { + } + +} diff --git a/test/org/mxchange/jcoreutils/bool/BooleanUtilsTest.java b/test/org/mxchange/jcoreutils/bool/BooleanUtilsTest.java new file mode 100644 index 0000000..2fafac9 --- /dev/null +++ b/test/org/mxchange/jcoreutils/bool/BooleanUtilsTest.java @@ -0,0 +1,116 @@ +/* + * Copyright (C) 2022 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.jcoreutils.bool; + +import org.testng.Assert; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +/** + * Unit tests for utilities class BooleanUtils + *

+ * @author Roland Häder + */ +public class BooleanUtilsTest { + + private static final Boolean BOOLEAN1 = Boolean.FALSE; + + private static final Boolean BOOLEAN2 = Boolean.TRUE; + + /** + * Default constructor + */ + public BooleanUtilsTest () { + } + + @DataProvider (name = "different-boolean-provider") + public Object[][] createDifferentBooleans () { + return new Object[][]{ + { + BOOLEAN1, + BOOLEAN2 + }, { + BOOLEAN2, + BOOLEAN1 + } + }; + } + + @DataProvider (name = "right-null-boolean-provider") + public Object[][] createRightNullBooleans () { + return new Object[][]{ + { + BOOLEAN1, + null + }, { + BOOLEAN2, + null + } + }; + } + + @DataProvider (name = "same-boolean-provider") + public Object[][] createSameBooleans () { + return new Object[][]{ + { + BOOLEAN1, + BOOLEAN1 + }, { + BOOLEAN2, + BOOLEAN2 + } + }; + } + + @DataProvider (name = "left-null-boolean-provider") + public Object[][] createLeftNullBooleans () { + return new Object[][]{ + { + null, + BOOLEAN1 + }, { + null, + BOOLEAN2 + } + }; + } + + @Test (description = "Tests method BooleanUtils.compare() when same boolean is provided", dataProvider = "same-boolean-provider") + public void testSameBoolean (final Boolean boolean1, final Boolean boolean2) { + // Should always return 0 + Assert.assertEquals(BooleanUtils.compare(boolean1, boolean2), 0); + } + + @Test (description = "Tests method BooleanUtils.compare() when left-null boolean is provided", dataProvider = "left-null-boolean-provider") + public void testLeftNullBoolean (final Boolean boolean1, final Boolean boolean2) { + // Should always return -1 + Assert.assertEquals(BooleanUtils.compare(boolean1, boolean2), -1); + } + + @Test (description = "Tests method BooleanUtils.compare() when right-null boolean is provided", dataProvider = "right-null-boolean-provider") + public void testRightNullBoolean (final Boolean boolean1, final Boolean boolean2) { + // Should always return 1 + Assert.assertEquals(BooleanUtils.compare(boolean1, boolean2), 1); + } + + @Test (description = "Tests method BooleanUtils.compare() when different boolean is provided", dataProvider = "different-boolean-provider") + public void testDifferentBoolean (final Boolean boolean1, final Boolean boolean2) { + // Should never return 0 + Assert.assertNotEquals(BooleanUtils.compare(boolean1, boolean2), 0); + } + +}