.~lock*
.gitcommits
/*.log
+jacoco.exec-*
import java.util.Objects;
/**
- * Utilities class for Boolean instances (not primitive boolean)
+ * Utilities class for Boolean instances (not primitive boolean), most methods
+ * here are null-safe or throw an NPE at you.
* <p>
* @author Roland Häder<roland@mxchange.org>
*/
public class BooleanUtils {
/**
- * Compares two Boolean instances with each other
+ * Compares two Boolean instances null-safe with each other
* <p>
* @param boolean1 Boolean instance 1
* @param boolean2 Boolean instance 2
--- /dev/null
+/*
+ * 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.enums;
+
+/**
+ * A utilities class for enumerations
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+public class EnumUtils {
+
+ /**
+ * Compares two enumerations with each other in a null-safe manner
+ * <p>
+ * @param enum1 Enumeration 1
+ * @param enum2 Enumeration 2
+ * <p>
+ * @return Comparison value
+ */
+ public static int compare (final Enum<?> enum1, final Enum<?> enum2) {
+ // Check parameters on null
+ if (enum1 == enum2) {
+ // Both are equal
+ return 0;
+ } else if (null == enum1) {
+ // Left one is null
+ return -1;
+ } else if (null == enum2) {
+ // Right one is null
+ return 1;
+ }
+
+ // Compare both enumeration's names
+ return enum1.name().compareTo(enum2.name());
+ }
+
+ /**
+ * No instances from utility classes
+ */
+ private EnumUtils () {
+ }
+
+}
};
}
+ @DataProvider (name = "left-null-boolean-provider")
+ public Object[][] createLeftNullBooleans () {
+ return new Object[][]{
+ {
+ null,
+ BOOLEAN1
+ }, {
+ null,
+ BOOLEAN2
+ }
+ };
+ }
+
@DataProvider (name = "right-null-boolean-provider")
public Object[][] createRightNullBooleans () {
return new Object[][]{
};
}
- @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 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);
}
@Test (description = "Tests method BooleanUtils.compare() when left-null boolean is provided", dataProvider = "left-null-boolean-provider")
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);
+ @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);
}
}
--- /dev/null
+/*
+ * 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.enums;
+
+import org.testng.Assert;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+/**
+ * A test case for utilities class EnumUtils
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+public class EnumUtilsTest {
+
+ /**
+ * Default constructor
+ */
+ public EnumUtilsTest () {
+ }
+
+ @DataProvider (name = "different-null-enum-provider")
+ public Object[][] createDifferentNullEnum () {
+ return new Object[][]{
+ {
+ TestEnum.TEST_VALUE1,
+ TestEnum.TEST_VALUE2
+ }, {
+ TestEnum.TEST_VALUE2,
+ TestEnum.TEST_VALUE1
+ }
+ };
+ }
+
+ @DataProvider (name = "left-null-enum-provider")
+ public Object[][] createLeftNullEnum () {
+ return new Object[][]{
+ {
+ null,
+ TestEnum.TEST_VALUE1}, {
+ null,
+ TestEnum.TEST_VALUE2
+ }
+ };
+ }
+
+ @DataProvider (name = "right-null-enum-provider")
+ public Object[][] createRightNullEnum () {
+ return new Object[][]{
+ {
+ TestEnum.TEST_VALUE1,
+ null
+ }, {
+ TestEnum.TEST_VALUE2,
+ null
+ }
+ };
+ }
+
+ @DataProvider (name = "same-null-enum-provider")
+ public Object[][] createSameNullEnum () {
+ return new Object[][]{
+ {
+ TestEnum.TEST_VALUE1,
+ TestEnum.TEST_VALUE1
+ }, {
+ TestEnum.TEST_VALUE2,
+ TestEnum.TEST_VALUE2
+ }
+ };
+ }
+
+ @Test (description = "Tests method EnumUtils.compare() with different enum", dataProvider = "different-null-enum-provider")
+ public void testEnumCompareDifferentEnum (final TestEnum enum1, final TestEnum enum2) {
+ // Should never return zero
+ Assert.assertNotEquals(EnumUtils.compare(enum1, enum2), 0);
+ }
+
+ @Test (description = "Tests method EnumUtils.compare() with left-null enum", dataProvider = "left-null-enum-provider")
+ public void testEnumCompareLeftNullEnum (final TestEnum enum1, final TestEnum enum2) {
+ // Should always return -1
+ Assert.assertEquals(EnumUtils.compare(enum1, enum2), -1);
+ }
+
+ @Test (description = "Tests method EnumUtils.compare() with right-null enum", dataProvider = "right-null-enum-provider")
+ public void testEnumCompareRightNullEnum (final TestEnum enum1, final TestEnum enum2) {
+ // Should always return 1
+ Assert.assertEquals(EnumUtils.compare(enum1, enum2), 1);
+ }
+
+ @Test (description = "Tests method EnumUtils.compare() with same enum", dataProvider = "same-null-enum-provider")
+ public void testEnumCompareSameEnum (final TestEnum enum1, final TestEnum enum2) {
+ // Should always return zero
+ Assert.assertEquals(EnumUtils.compare(enum1, enum2), 0);
+ }
+
+ /**
+ * Enumeration with a parameter for testing purposes
+ */
+ private enum TestEnum {
+ // First test value
+ TEST_VALUE1("SOME_FOO"),
+ // Second test value
+ TEST_VALUE2("SOME_BAR");
+
+ /**
+ * Test string
+ */
+ private final String testString;
+
+ /**
+ * Constructor
+ * <p>
+ * @param testString Test string
+ */
+ private TestEnum (final String testString) {
+ this.testString = testString;
+ }
+
+ /**
+ * Getter for test string
+ * <p>
+ * @return Test string
+ */
+ public String getTestString () {
+ return this.testString;
+ }
+ }
+
+}