Skip to content

Commit 267169e

Browse files
chrisjaimesChristian Jaimes
andauthored
BAEL-5521 Convert boolean to int in Java (eugenp#12017)
* added class for Article examples, created unit tests and modified pom in order to call Apache Commons outside test * changed parameter to prevent NPE Co-authored-by: Christian Jaimes <christian.jaimes@oracle>
1 parent 74b3c66 commit 267169e

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed

java-numbers-4/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
<groupId>org.apache.commons</groupId>
2424
<artifactId>commons-lang3</artifactId>
2525
<version>${commons-lang3.version}</version>
26-
<scope>test</scope>
2726
</dependency>
2827
<dependency>
2928
<groupId>com.google.guava</groupId>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.booleantoint;
2+
3+
import org.apache.commons.lang3.BooleanUtils;
4+
5+
public class BooleanToInt {
6+
public static int booleanPrimitiveToInt(boolean foo) {
7+
int bar = 0;
8+
if (foo) {
9+
bar = 1;
10+
}
11+
return bar;
12+
}
13+
14+
public static int booleanPrimitiveToIntTernary(boolean foo) {
15+
return (foo) ? 1 : 0;
16+
}
17+
18+
public static int booleanObjectToInt(boolean foo) {
19+
return Boolean.compare(foo, false);
20+
}
21+
22+
public static int booleanObjectToIntInverse(boolean foo) {
23+
return Boolean.compare(foo, true) + 1;
24+
}
25+
26+
public static int booleanObjectMethodToInt(Boolean foo) {
27+
return foo.compareTo(false);
28+
}
29+
30+
public static int booleanObjectMethodToIntInverse(Boolean foo) {
31+
return foo.compareTo(true) + 1;
32+
}
33+
34+
public static int booleanUtilsToInt(Boolean foo) {
35+
return BooleanUtils.toInteger(foo);
36+
}
37+
38+
public static int bitwiseBooleanToInt(Boolean foo) {
39+
return (Boolean.hashCode(foo) >> 1) & 1;
40+
}
41+
}
42+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baeldung.booleantoint;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
public class BooleanToIntUnitTest {
8+
@Test
9+
void givenBooleanPrimitiveValue_ThenReturnInt() {
10+
assertEquals(1, BooleanToInt.booleanPrimitiveToInt(true));
11+
assertEquals(0, BooleanToInt.booleanPrimitiveToInt(false));
12+
}
13+
14+
@Test
15+
void givenBooleanPrimitiveValue_ThenReturnIntTernary() {
16+
assertEquals(1, BooleanToInt.booleanPrimitiveToIntTernary(true));
17+
assertEquals(0, BooleanToInt.booleanPrimitiveToIntTernary(false));
18+
}
19+
20+
@Test
21+
void givenBooleanObject_ThenReturnInt() {
22+
assertEquals(0, BooleanToInt.booleanObjectToInt(false));
23+
assertEquals(1, BooleanToInt.booleanObjectToInt(true));
24+
}
25+
26+
@Test
27+
void givenBooleanObject_ThenReturnIntInverse() {
28+
assertEquals(0, BooleanToInt.booleanObjectToIntInverse(false));
29+
assertEquals(1, BooleanToInt.booleanObjectToIntInverse(true));
30+
}
31+
32+
@Test
33+
void givenBooleanObject_ThenReturnIntUsingClassMethod() {
34+
assertEquals(0, BooleanToInt.booleanObjectMethodToInt(false));
35+
assertEquals(1, BooleanToInt.booleanObjectMethodToInt(true));
36+
}
37+
38+
@Test
39+
void givenBooleanObject_ThenReturnIntUsingClassMethodInverse() {
40+
assertEquals(0, BooleanToInt.booleanObjectMethodToIntInverse(false));
41+
assertEquals(1, BooleanToInt.booleanObjectMethodToIntInverse(true));
42+
}
43+
44+
@Test
45+
void givenBoolean_ThenReturnIntUsingBooleanUtils() {
46+
assertEquals(0, BooleanToInt.booleanUtilsToInt(false));
47+
assertEquals(1, BooleanToInt.booleanUtilsToInt(true));
48+
}
49+
50+
@Test
51+
void givenBoolean_ThenReturnIntUsingBitwiseOperators() {
52+
assertEquals(0, BooleanToInt.bitwiseBooleanToInt(false));
53+
assertEquals(1, BooleanToInt.bitwiseBooleanToInt(true));
54+
}
55+
}

0 commit comments

Comments
 (0)