What's the utility method that parses a boolean String properly? By properly I mean
"true" => true
"false" => false
"foo" => error
The parse methods in java.lang.Boolean are dodgy - they don't distinguish "false" from "foo". Anything else in Java libraries (or Guava, or Commons Lang) that does it properly?
Yes it's just be a couple lines, I just rather not write any line that I shouldn't have to. :-)
Check out Boolean Utils form apache commons :
Boolean Utils API
Converts a String to a Boolean
throwing an exception if no match
found.
null is returned if there is no match.
BooleanUtils.toBoolean("true",
"true", "false") = true
BooleanUtils.toBoolean("false",
"true", "false") = false
if ( "true".equalsIgnoreCase(yourString) )
return true;
else if ( "false".equalsIgnoreCase(yourString) )
return false;
else
throw new Exception();
There's not one.
Honestly, this question is ridiculous. Yes, there are ways to do it built in (the Boolean utils API Apache Fan mentioned). But you're going out of your way to do something in a fancy way at the cost of A) productivity (stop wasting your time, write the three lines of code), and B) readability. What's easier to read:
if( "true".equals(myString) )
or
if( BooleanUtils.toBoolean(myString, "true", "false") )
I'd go for the first one every time. Even better, use the IgnoreCase option for the string comparison. The toBoolean is case sensitive, so "True" would actually throw an exception. Awesome! That's really useful!
How about the very simple:
EDIT: or am I missing the point...
boolean isTrue (String s) {
return theString.toLowerCase().equals("true");
}
Related
I have a code snippet as below. Here there is a nested if else loop as well as multiple conditions [all different parameters]. What is the best way to optimize this.
if(request!=null && !StringUtils.isBlank(request)) {
if(request.getFirstName()!=null && !StringUtils.isBlank(request.getFirstName())
&& request.getLastName()!=null && !StringUtils.isBlank(request.getLastName())
&& request.getAge()!=null && !StringUtils.isBlank(request.getAge())
&& request.getAddress()!=null && !StringUtils.isBlank(request.getAddress())
&& request.getPhoneNumber()!=null && !StringUtils.isBlank(request.getPhoneNumber())) {
return true;
}else {
return false;
}
}else {
return false;
}
I had thought of using switch case and for loop as well but all the conditions are based on different variables, I didn't see it as compatible.
StringUtils from commons-lang already has a method which accepts an array of Strings. It will check for null or empty or blank strings. So all your checks boil down to:
return !(request == null || StringUtils.isAnyBlank(
request.getFirstName, request.getLastName,
request.getAge, request.getPhoneNumber));
You can try StringUtils.isAnyBlank(). Please refer attached link.
isAnyBlank : https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#isAnyBlank-java.lang.CharSequence
If you don't use commons-lang dependency you can simply use Stream API (Java 8+)
Boolean allNonBlank = Stream.of(
request.getFirstName(),
request.getLastName(),
request.getAge(),
request.getPhoneNumber())
.allMatch(it -> it != null && !String.isBlank(it));
You have a few syntax errors
You are passing request to StringUtils but it doesn't appear to implement CharSequence
You are using !! instead of !
You invocation of the get methods does not include the () to mark it as methods.
Although not an error, you do not need nested if-statements here. Using unnecessary if-else-blocks can make it harder to decipher what the code is doing. It can, however, allow for comments to describe why certain conditions are being checked or whatever. None of that seems relevant here. In fact, you can pass the result of the boolean operation without any if-statement. Using if-statements that return true or false looks like this.
if (<condition-is-true?>) return true
else return false;
Which can be simplified to...
return <condition-is-true?>;
Further, assuming you are using using Apache StringUtils, you do not need to check for null first - the isEmpty(CharSequence) method does that. Additionally, StringUtils includes the isAnyEmpty(CharSequence...) method so you can pass all of the Strings at once.
return request != null && !StringUtils.isAnyEmpty(
request.getFirstName(),
request.getLastName(),
request.getAge(),
request.getAddress(),
request.getPhoneNumber());
I want to get all the individual conditions which resulted in the execution of a rule.
For example, if I have the following rule:
package app1;
rule 'rule1'
when
MyObjectType1( booleanPredicate1() )
or
(
MyObjectType2( booleanPredicate2() )
and
MyObjectType3( booleanPredicate3() )
)
or
MyObjectType4( booleanPredicate4() )
then
System.out.println("In rule - " + drools.getRule().getName());
end
and booleanPredicate1(), booleanPredicate2() and booleanPredicate4() are true, then I want to get the following output:
booleanPredicate1() resulted in rule execution.
booleanPredicate4() resulted in rule execution.
What I've tried so far is inside the implementation of all such predicate methods, I've added a logging statement which gets executed only when that method is going to return true:
boolean booleanPredicate1()
{
boolean ret = false;
...
...
if (<business-logic-defined-predicate>)
{
ret = true;
}
if(ret)
{
addToLog("booleanPredicate1 became true.");
}
return ret;
}
but with this solution, I'll also get the output booleanPredicate2() resulted in rule execution. which is wrong.
Is there any way with which I can get the correct logging results?
Consult my paper on rule design patterns it has a section answering your question.
To summarize it here: you need rules for the individual truth values to register what is true for some fact or some combination of facts. The rule as you have it now will then combine the boolean values from the registry, and registry contains the answer to your problem.
Here is the Java code:
public static boolean anyEqual(Object needle, Object... haystack) {
if(needle == null || haystack == null) {
return false;
}
if(haystack.length == 0) {
return false;
}
for(Object match : haystack) {
if(match != null && needle.getClass() == match.getClass() && needle.equals(match)) {
return true; // warning from IntelliJ here, 'contract clause !null, null -> false is violated'
}
}
return false;
}
Does anyone have any idea why this is being shown? contract clause !null, null -> false is violated? Thanks!
IntelliJ 14.0.2 build: 139.659
Screenshot:
IntelliJ is inferring the formal contract of your method to be this:
null, _ -> false; !null, null -> false
What this actually means:
The first contract specifies that, so long as the first parameter is null, it will return false. This is observed by your first if statement:
if(needle == null || haystack == null) {
return false;
}
The second contract specifies that, if the second parameter is null, then it will return false. This is also specified by the same if statement above.
My gut is telling me that IntelliJ is having some trouble discerning what the loop's formal contract is in addition to all of the above, although it'd be as simple as another condition in the contract expression.
for(Object match : haystack) {
if(match != null && needle.getClass() == match.getClass() && needle.equals(match)) {
return true;
}
}
Let's briefly go through this.
The enhanced-for statement won't fire if haystack is of length 0, so that's something to take into consideration.
The elements inside of the array could be null, and I'm not entirely sure that IntelliJ's static analysis covers that piece yet.
We've established already that needle must be non-null, so there's nothing violating the contract at that line.
If we have a scenario in which match != null && needle.getClass() == match.getClass() && needle.equals(match) is true, we return true. Otherwise, we return false.
There's nothing that I can see in the formal documentation that gives us the expression we require to say, "hey - we're checking elements of an array!"; it may be the case that the analysis is tripping up on the fact that we're returning true in spite of what we stated above (since haystack is non-null).
Allow me to stress this point:
haystack has to be non-null in order for you to enter into the enhanced-for. Your code will not work otherwise.
All in all, I wouldn't worry about it. Better yet, file a bug against it so that this sort of thing could be fixed or expanded upon.
This looks like an IntelliJ bug to me, since by removing the static keyword from the method the warning disappears.
Something must be confusing the static analysis here. One can always submit this to youtrack so jetbrains devs can look at it.
Someone already reported this issue Here
(tested on v14.0.3)
This message is being shown because IntelliJ checks for method contract violations. It's a relatively new feature, read more at https://www.jetbrains.com/idea/features/annotation_java.html
What is recommended in java :
if(!var){
}
or
if(var==null){
}
and why?
It is the same thing with Groovy?
In java, if var is null,
if( !var ) {
// var is not null
}
Won't work as ! is for booleans.
In Groovy, it will work as it applies Groovy Truth.
HOWEVER, if you are testing for null, then you should use the explicit test
if( var != null ) {
// var is not null
}
Even in Groovy, as if var was anything Groovy considers false, the first comparison would pass, ie:
assert !0
assert !''
assert ![]
assert !null
Of course you can also use the null safe operator:
def map = [ person:[ tim:[ login:'tim_yates' ] ] ]
assert map?.person?.tim?.login == 'tim_yates'
assert map?.person?.alice?.login == null
The first one in Java would apply only to booleans
The unary operator ! does the negation operation. For example,
!false will yield true.
And == does comparison operation.
They both are completely different to each other. In java, you can use the negation operator only with boolean values, whereas the comparison operator can be used to compare values.
Also, in Java, if var is Boolean and is null, then
if(!var){ // would throw a NullPointerException
}
In Java, you would usually say that
if(someBool != false)
is the same as
if(someBool)
But what if someBool is not of type boolean but Boolean, and its value is null?
If you want to handle Boolean instances as well as primitives and be null-safe, you can use this:
if(Boolean.TRUE.equals(someBool))
It will throw a NullPointerException (autounboxing of null throws NPE).
But that only means that you must not allow a null value. Either use a default, or don't use autounboxing and make a non-null check. Because using a null value of a boolean means you have 3, not 2 values. (Better ways of handling it were proposed by Michael and Tobiask)
Use ApacheCommons BooleanUtils.isTrue() or .isFalse()
If someBool is Boolean
if (someBull != null && someBull) {
//Yeah, true.
}
Since Boolean can be null make sure you avoid NullPointerException by checking for not null.
I did a little test:
Boolean o = null;
try {
System.out.println(o ? "yes" : "no");
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println((o != false) ? "yes" : "no");
} catch (Exception e) {
e.printStackTrace();
}
The output is surprising:
java.lang.NullPointerException
at btest.main(btest.java:10)
java.lang.NullPointerException
at btest.main(btest.java:15)
The first NPE is to be expected, because o will be autounboxed (and that fails because it's null). The second happens for the same reason, but it doesn't feel natural. Anyway, the solution is to do:
System.out.println(!Boolean.FALSE.equals(o) ? "yes" : "no");
You can however compare a null Boolean with a Boolean instance. For example :
Boolean myBool = null;
System.out.println(myBool == Boolean.FALSE);
System.out.println(myBool == Boolean.TRUE);
prints :
false
false
Good illustrations of the difference between the primitive boolean & the object Boolean. The former can be only true or false. The latter can be true, false, or unknown/undefined. (i.e., null). Which you use depends on whether you want to deal with two use cases or three.
It's old, but Boolean.valueOf(null) is false, just like Boolean.valueOf(false) is false.
Actually the Boolean constructor accepts null, returns FALSE and doesn't throw a NullPointerTantrum.
new Boolean(null);
<false>
This has the added bonus of also giving a thruthy response to the string "true" which is not the case for Boolean.TRUE.equals but we are more restricted again having only constructors for Strings and Booleans.
Something you can overcome with string concatenation, which is also null-proof.
new Boolean(""+null);
<false>
new Boolean(""+false);
<false>
new Boolean(""+new Object());
<false>
new Boolean(""+6);
<false>
new Boolean(""+new Integer(9));
<false>
Ensuring that all the TRUE options, available in java, still remains.
new Boolean(""+true);
<true>
new Boolean(""+"true");
<true>
If it's Java 7+ you can use
import java.util.Objects;
And
if (Objects.equals(someBool, true))
As Boolean will give you an object, you must always check for NULL before working on the object
If its null then you'll get a NullPointerException