Groovy : What is Groovy Truth and define Custom Groovy Truth

What is Groovy Truth

Groovy has its own way of defining what is true and what is false, this is called the Groovy Truth. In Java, we have to write statements like if(obj ==null), in groovy you can simply write if(obj) and you can define in what conditions this object will evaluate to true and when it will evaluate to false.
 Info
To define your custom logic for groovy Truth you need to define method
boolean asBoolean(){
 // your logic here
}

What is True in Groovy

Following evaluated to true in groovy
  1. boolean true
  2. Not null objects
  3. Non 0 numbers
  4. Not null and not empty Strings
  5. Not null and not empty lists, maps, collections

What is False in Groovy

Following things evaluates to false in groovy
  1. boolean false
  2. Numeric 0 number
  3. Null objects
  4. Null Strings
  5. Null or empty lists, maps, collections
def groovyTruth = {val->
  if(val)
     println "$val is true"
  else 
    println "$val is false"
}

boolean b1 = true
boolean b2 = false

groovyTruth(b1)  // true  
groovyTruth(b2) // false

int n1 = 0  
int n2 = 12
int n3 = -321

groovyTruth(n1) // false
groovyTruth(n2) // true
groovyTruth(n3) // true

String s1 = null
String s2 = ""
String s3 = "some string"

groovyTruth(s1)  // false
groovyTruth(s2)  // false
groovyTruth(s3)  // true

Object o1 = null
Object o2 = new Object()

groovyTruth(o1)  // false
groovyTruth(o2)  // true

List l1 = null
List l2 = []
List l3 = [1,2,3]

groovyTruth(l1) // false
groovyTruth(l2) // false
groovyTruth(l3) // true


Map m1 = null 
Map m2 = [:]
Map m3 = ['key': 'val']

groovyTruth(m1)  // false
groovyTruth(m2)  // false
groovyTruth(m3)  // true

When we run this code, it will give the following output
Output

true is true
false is false
0 is false
12 is true
-321 is true
null is false
is false
some string is true
null is false
java.lang.Object@37074b2c is true
null is false
[] is false
[1, 2, 3] is true
null is false
[:] is false
[key:val] is true

Custom Groovy Truth

You can also define your custom Groovy Truth, that is how object will evaluate to true and false . For this you need to override the method Boolean asBoolean() to return a boolean value which will be used to evaluate truth of the object.
Let's take a class Student and it should give true if the status of student is enrolled
class Student{  
  String name 
  String status

 boolean asBoolean(){
    return status == 'enrolled'
 }
 String toString(){
   return name
 }
}

Student s1 = new Student(name : 's1', status: 'enrolled')
Student s2 = new Student(name : 's2', status: 'applied')

def groovyTruth = {val->
  if(val)
     println "$val is true"
  else 
    println "$val is false"
}

groovyTruth(s1)
groovyTruth(s2)
​
So the above code will give the following output
Output

s1 is true
s2 is false

How to Customise groovy Truth for the existing Classes in Groovy

Let's say we want to make the string "null" to evaluate to false. Then we can override the asBoolean() method of the String class.
So, the following will evaluate to false in this scenario.
  1. null - null object
  2. "" - blank string
  3. "null" - string will null as a string
String.metaClass.asBoolean = {
  if(delegate == null || delegate == "" || delegate == "null")
     return false
  return true
}

String s1 = null
String s2 = ""
String s3 = "null"
String s4 = "some string"

def groovyTruth = {val->
  if(val)
     println "$val is true"
  else 
    println "$val is false"
}

groovyTruth(s1)
groovyTruth(s2)
groovyTruth(s3)
groovyTruth(s4)​
This will give the following output
Output

null is false
is false
null is false
some string is true

No comments:

Powered by Blogger.