vendredi 21 août 2015

How can shorten this logic

Consider below JSON:

"collection1" :
{
    "list1" : ["val1","val2","val3"]
    "list2" : ["val1","val2","val3"] 
},
"collection2" :
{
    "list1" : ["val1","val2","val3"]
    "list2" : ["val1","val2","val3"] 
},

I want to return true if there is no value for both collection1 and collection2. Now whether there is "no value" can be determined by many ways:

  • both collection1 and collection2 are null
  • all collection1, collection2.list1 and collection2.list2 are null
  • collection1 is null and
    • collection2.list1 and collection2.list2 contains no elements or
    • collection2.list1 is null and collection2.list2 contains no elements or
    • collection2.list2 is null and collection2.list1 contains no elements

You can realise this can lead to many combinations. I can obiviously translate this directly to if-else blocks, but am finding it boring, or may be I am just bored, my head isnt working and really need some coffee because I am not able to guess anything simpler, smaller (possibly few liner if one liner is not possible) and concise on such a simple/dull thing. Or am I just wasting my time and over thinking? Ans simply convert that to if-else block and leave?

I am writing code in groovy, so Java solution will also work.

Aucun commentaire:

Enregistrer un commentaire