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 notnull
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.
Nice post. I learned some new information. Thanks for sharing.
ReplyDeleteGuest posting sites
Education
It taught me a lot and information are valuable. Thank you.
ReplyDeleteCloud Computing Courses in Coimbatore
Cloud Computing Training in Coimbatore
Cloud Computing Courses
Cloud Training
Cloud Certification
Cloud Computing Certification
I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.
ReplyDeleteWeb Designing Course Training in Chennai | Web Designing Course Training in annanagar | Web Designing Course Training in omr | Web Designing Course Training in porur | Web Designing Course Training in tambaram | Web Designing Course Training in velachery
Really Thanks for Sharing Such an informative stuff...Guest Post Free Technology
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThe best Casino Sites for real money with Instant Withdrawals
ReplyDeleteCasino Withdrawal Methods: · Interac · Credit Card · CreditCard · E-Wallet · Gambling · Credit Card luckyclub.live · Mastercard · PayPal · Betfred · PlayBingo · NetEnt.
this site is really an amazing site like colourist logo
ReplyDeleteYENİ PERDE MODELLERİ
ReplyDeletesms onay
vodafone mobil ödeme bozdurma
nft nasıl alınır
Ankara evden eve nakliyat
trafik sigortası
dedektör
web sitesi kurma
aşk kitapları
kartal bosch klima servisi
ReplyDeletebeykoz toshiba klima servisi
beykoz beko klima servisi
üsküdar beko klima servisi
pendik lg klima servisi
pendik alarko carrier klima servisi
tuzla toshiba klima servisi
tuzla beko klima servisi
çekmeköy lg klima servisi
visit this site Fitness Mania
ReplyDeleteIt 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
ReplyDeleteExcellent post thanks for sharing
ReplyDeletegold rate in chennai
gold rate today namakkal
gold price today salem
gold rate today madurai
gold rate today at grt