Saturday, July 30, 2011

Fudging the truth with Groovy

In Groovy, any expression can be treated as a Boolean test. Groovy decides whether the expression should evaluate to true or false based on rules, often referred to collectively as the Groovy truth. The rules apply to different types as follows:

  • Collection: any non-empty collection is considered true
  • String: any non-empty string is considered true
  • Number/Character: any non-zero is treated as true
  • Matcher: true only if a match was found
  • Other objects: true if the object reference is not null

We can put these rules to use to write idiomatic Groovy as follows:

def user = User.findByUserName("henry")
if(user) {
  println "Welcome ${user.fullName}"
else {
  println "No user found."
}

This works as expected as long as the findByUserName returns null if no user is found. It is, however, also possible to customize the rules that define the Groovy Truth. Consider the following GroovyBean:

class Inventory {
  def books = []
}

Using the standard rules for Groovy Truth we can see that the following code

Inventory inventory = new Inventory()
printTruth(inventory)

def printTruth(obj) {
  if(obj) {
    println "true"
  } else {
    println "false"
  }
}

will result in true being printed to the console (since the object is not null). However, for this class it may make more sense for the truth of the Inventory class to be determined by the number of books in the inventory.

In Groovy, we can accomplish this by simply implementing the boolean asBoolean method. In this case, we may decide on something as follows:

class Inventory {
  def books = []
 
  boolean asBoolean() {
    books.size() > 0
  }

}

Repeating the check from above, using the same inventory instance will now produce false as output. We can now use this functionality anywhere a Boolean expression is required:

if(inventory) {
  inventory.books.each { book ->
    println book
  }
} else {
  println "No books in inventory"
}

Of course, for such a simple scenario, it is probably better to use the Safe Navigation operator. Nevertheless, being able to customize the rules for Groovy Truth is a handy feature in situations where you have business logic that dictates a common test be performed before an operation is performed with the same object.

12 comments:

  1. Nice post. I learned some new information. Thanks for sharing.

    Guest posting sites
    Education

    ReplyDelete
  2. Really Thanks for Sharing Such an informative stuff...Guest Post Free Technology

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. The best Casino Sites for real money with Instant Withdrawals
    Casino Withdrawal Methods: · Interac · Credit Card · CreditCard · E-Wallet · Gambling · Credit Card luckyclub.live · Mastercard · PayPal · Betfred · PlayBingo · NetEnt.

    ReplyDelete
  5. It is an ideal video editing agenda for home movies. It become rough to create modifies vast videos. Completely well-matched with the nearly Cracked Editing Software

    ReplyDelete