Skip to content

Commit e89b8d8

Browse files
author
Deepak Malik
committed
Boolean Utils
1 parent d7f8bc5 commit e89b8d8

File tree

3 files changed

+169
-1
lines changed

3 files changed

+169
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ Below topics/problems are covered as of now.
154154
- [ ] NumberUtils
155155
- [X] [ArrayUtils](../master/src/com/deepak/data/structures/Utils/ArrayUtils.java)
156156
- [ ] ListUtils
157-
- [ ] BooleanUtils
157+
- [X] [BooleanUtils](../master/src/com/deepak/data/structures/Utils/BooleanUtils.java)
158158
- [ ] CollectionUtils
159159
- [ ] MapUtils
160160
- [ ] DateUtils
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* Data-Structures-In-Java
3+
* BooleanUtils.java
4+
*/
5+
package com.deepak.data.structures.Utils;
6+
7+
/**
8+
* Utilities for Boolean
9+
*
10+
* @author Deepak
11+
*/
12+
public class BooleanUtils {
13+
14+
/**
15+
* Method to negate a Boolean
16+
*
17+
* @param bool
18+
* @return {@link Boolean}
19+
*/
20+
public static Boolean negate(final Boolean bool) {
21+
if (bool == null) {
22+
return null;
23+
}
24+
return bool.booleanValue() ? Boolean.FALSE : Boolean.TRUE;
25+
}
26+
27+
/**
28+
* Method to check if boolean is True
29+
*
30+
* @param bool
31+
* @return {@link boolean}
32+
*/
33+
public static boolean isTrue(final boolean bool) {
34+
return Boolean.TRUE.equals(bool);
35+
}
36+
37+
/**
38+
* Method to check if boolean is false
39+
*
40+
* @param bool
41+
* @return {@link boolean}
42+
*/
43+
public static boolean isFalse(final boolean bool) {
44+
return Boolean.FALSE.equals(bool);
45+
}
46+
47+
/**
48+
* Method to convert Boolean object to boolean primitive
49+
*
50+
* @param bool
51+
* @return {@link boolean}
52+
*/
53+
public static boolean toBoolean(final Boolean bool) {
54+
return bool != null && bool.booleanValue();
55+
}
56+
57+
/**
58+
* Method to convert int to boolean
59+
*
60+
* @param val
61+
* @return {@link boolean}
62+
*/
63+
public static boolean toBoolean(final int val) {
64+
return val != 0;
65+
}
66+
67+
/**
68+
* Method to convert int to Boolean object
69+
*
70+
* @param val
71+
* @return {@link Boolean}
72+
*/
73+
public static Boolean toBooleanObject(final int val) {
74+
return val != 0 ? Boolean.TRUE : Boolean.FALSE;
75+
}
76+
77+
/**
78+
* Method to compare two boolean values
79+
* If both the same, it returns 0, if x is true, it returns 1
80+
* else it returns -1
81+
*
82+
* @param x
83+
* @param y
84+
* @return {@link int}
85+
*/
86+
public static int compare(final boolean x, final boolean y) {
87+
if (x == y) {
88+
return 0;
89+
}
90+
return x ? 1 : -1;
91+
}
92+
93+
}
94+
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Data-Structures-In-Java
3+
* BooleanUtilsTest.java
4+
*/
5+
package com.deepak.data.structures.Utils;
6+
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
/**
11+
* Test cases for Boolean Utility functions
12+
*
13+
* @author Deepak
14+
*/
15+
public class BooleanUtilsTest {
16+
17+
/**
18+
* Test case to check negation
19+
*/
20+
@Test
21+
public void testNegateBoolean() {
22+
Assert.assertEquals(BooleanUtils.negate(Boolean.TRUE), Boolean.FALSE);
23+
Assert.assertEquals(BooleanUtils.negate(Boolean.FALSE), Boolean.TRUE);
24+
Assert.assertEquals(BooleanUtils.negate(null), null);
25+
}
26+
27+
/**
28+
* Test case to check is True
29+
*/
30+
@Test
31+
public void testIsTrue() {
32+
Assert.assertEquals(BooleanUtils.isTrue(Boolean.TRUE), true);
33+
Assert.assertEquals(BooleanUtils.isTrue(Boolean.FALSE), false);
34+
}
35+
36+
/**
37+
* Test case to check is False
38+
*/
39+
@Test
40+
public void testIsFalse() {
41+
Assert.assertEquals(BooleanUtils.isFalse(Boolean.TRUE), false);
42+
Assert.assertEquals(BooleanUtils.isFalse(Boolean.FALSE), true);
43+
}
44+
45+
/**
46+
* Test case to convert Boolean object to primitive boolean
47+
*/
48+
@Test
49+
public void testToBoolean() {
50+
Assert.assertEquals(BooleanUtils.toBoolean(Boolean.TRUE), true);
51+
Assert.assertEquals(BooleanUtils.toBoolean(Boolean.FALSE), false);
52+
}
53+
54+
/**
55+
* Test case to convert int to Boolean object
56+
*/
57+
@Test
58+
public void testToBooleanObject() {
59+
Assert.assertEquals(BooleanUtils.toBooleanObject(0), Boolean.FALSE);
60+
Assert.assertEquals(BooleanUtils.toBooleanObject(1), Boolean.TRUE);
61+
Assert.assertEquals(BooleanUtils.toBooleanObject(2), Boolean.TRUE);
62+
}
63+
64+
/**
65+
* Test case to compare two boolean
66+
*/
67+
@Test
68+
public void testCompare() {
69+
Assert.assertEquals(BooleanUtils.compare(true, true), 0);
70+
Assert.assertEquals(BooleanUtils.compare(true, false), 1);
71+
Assert.assertEquals(BooleanUtils.compare(false, true), -1);
72+
}
73+
74+
}

0 commit comments

Comments
 (0)