Home > Groovy > Multi-level ternary operator

Multi-level ternary operator

Love one. Can’t be easily debugged, probably .. But reads very nicely:

String getScmClass()
{
    ( this.@scmClass == 'svn' ) ? 'hudson.scm.SubversionSCM'  :
    ( this.@scmClass == 'git' ) ? 'hudson.plugins.git.GitSCM' :
                                  null
}

Still, standard mapping is usually better – it allows to dump all known options:

Map    scmClasses = [ svn : 'hudson.scm.SubversionSCM',
                      git : 'hudson.plugins.git.GitSCM' ]
String getScmClass()
{
  def    scmClass = scmClasses[ this.@scmClass ]
  assert scmClass, "Unknown [${this.@scmClass}]. Known classes are ${scmClasses.keySet()}"
         scmClass
}
Advertisements
Categories: Groovy Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: