]> git.mxchange.org Git - jcore-utils.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Sat, 12 Nov 2022 01:13:08 +0000 (02:13 +0100)
committerRoland Häder <roland@mxchange.org>
Sat, 12 Nov 2022 01:13:08 +0000 (02:13 +0100)
- added null-safe utility class BooleanUtils

src/org/mxchange/jcoreutils/bool/BooleanUtils.java [new file with mode: 0644]
test/org/mxchange/jcoreutils/bool/BooleanUtilsTest.java [new file with mode: 0644]

diff --git a/src/org/mxchange/jcoreutils/bool/BooleanUtils.java b/src/org/mxchange/jcoreutils/bool/BooleanUtils.java
new file mode 100644 (file)
index 0000000..c1e041d
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2022 Roland Häder<roland@mxchange.org>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jcoreutils.bool;
+
+import java.util.Objects;
+
+/**
+ * Utilities class for Boolean instances (not primitive boolean)
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+public class BooleanUtils {
+
+       /**
+        * Compares two Boolean instances with each other
+        * <p>
+        * @param boolean1 Boolean instance 1
+        * @param boolean2 Boolean instance 2
+        * <p>
+        * @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 (file)
index 0000000..2fafac9
--- /dev/null
@@ -0,0 +1,116 @@
+/*
+ * Copyright (C) 2022 Roland Häder<roland@mxchange.org>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+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
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+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);
+       }
+
+}