-
Notifications
You must be signed in to change notification settings - Fork 65
/
BooleanUtils.java
94 lines (84 loc) · 1.69 KB
/
BooleanUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* Data-Structures-In-Java
* BooleanUtils.java
*/
package com.deepak.data.structures.Utils;
/**
* Utilities for Boolean
*
* @author Deepak
*/
public class BooleanUtils {
/**
* Method to negate a Boolean
*
* @param bool
* @return {@link Boolean}
*/
public static Boolean negate(final Boolean bool) {
if (bool == null) {
return null;
}
return bool.booleanValue() ? Boolean.FALSE : Boolean.TRUE;
}
/**
* Method to check if boolean is True
*
* @param bool
* @return {@link boolean}
*/
public static boolean isTrue(final boolean bool) {
return Boolean.TRUE.equals(bool);
}
/**
* Method to check if boolean is false
*
* @param bool
* @return {@link boolean}
*/
public static boolean isFalse(final boolean bool) {
return Boolean.FALSE.equals(bool);
}
/**
* Method to convert Boolean object to boolean primitive
*
* @param bool
* @return {@link boolean}
*/
public static boolean toBoolean(final Boolean bool) {
return bool != null && bool.booleanValue();
}
/**
* Method to convert int to boolean
*
* @param val
* @return {@link boolean}
*/
public static boolean toBoolean(final int val) {
return val != 0;
}
/**
* Method to convert int to Boolean object
*
* @param val
* @return {@link Boolean}
*/
public static Boolean toBooleanObject(final int val) {
return val != 0 ? Boolean.TRUE : Boolean.FALSE;
}
/**
* Method to compare two boolean values
* If both the same, it returns 0, if x is true, it returns 1
* else it returns -1
*
* @param x
* @param y
* @return {@link int}
*/
public static int compare(final boolean x, final boolean y) {
if (x == y) {
return 0;
}
return x ? 1 : -1;
}
}