Conditional statements and multi-assign values

This section contains examples of transformations using conditional statements and multi-assign values.

Example 20-1 Simple conditional statement

The following code uses an if...else statement to assign a flag to a record based on the value of the tip_amount attribute.

// if the value of tip_amount is greater than 0, assign the record the 'Tip' flag:
if(tip_amount>0){
    'Tip'
    }
// if the above statement is false, assign the record the 'No Tip' flag: 
else{
    'No Tip'
    }

Example 20-2 Advanced conditional statement

The following code assigns each record a flag based on the tip_amount attribute. However, this one uses a series of if...else statements to assign different flags to each percentage range:

// if the tip was more than 25% of the total fare, assign the record the Large Tip flag:
if(tip_amount/fare_amount>.25){
    'Large Tip'
}

// if the tip was less than or euqal to 25% and higher than 18%, assign the record the Standard Tip flag:
else if(tip_amount/fare_amount<=.25 || tip_amount/fare_amount>.18){
    'Standard Tip'
}

// if the tip was less than or equal to 18%, assign the record the Small Tip flag:  
else if(tip_amount/fare_amount<=.18 || tip_amount/fare_amount>0){
    'Small Tip'
}

// if all of the above statements failed assign the record the No Tip flag:
else{
    'No Tip'
}

Example 20-3 Advanced conditional statement using a variable

This example performs the same operation as the previous example, but uses a variable to store the tip percentage rather than calculating it in each statement.

// calculate the tip percentage and assign it to PercentageTip
def PercentageTip = tip_amount/fare_amount

// if the value of PercentTip is greater than .25 (25%),
// assign the record the Large Tip flag
if(PercentTip>.25){
    'Large Tip'
}

// if the value of PercentTip is less than or equal to .25 and higher than .18
// assign the record the Standard Tip flag
else if(PercentTip<=.25 && PercentTip>.18){
     'Standard Tip'
}

// if the value of PercentTip is less than or equal to .18 and higher than 0
// assign the record the Small Tip flag
else if(PercentTip<=.18 && PercentTip>0){
     'Small Tip'
}

// if all of the above statements failed assign the record the No Tip flag
else{
    'No Tip'
}

Example 20-4 Advanced conditional statement using date logic

The following code uses a series of else...if statements to create a multi-assign value. It uses the diffDatesWithPrecision function to calculate the amount of time between the current date and the attribute dropoff_datetime in days. It then assigns the record a list of String values that specify the different ranges of time it falls into.

if(diffDates(dropoff_datetime,today(),'days',true)<=30){
    toSet('Last 30 Days','Last 90 Days','Last 180 Days')
}
else if(diffDates(dropoff_datetime,today(),'days',true)<=90){
    toSet('Last 90 Days','Last 180 Days')
}
else if(diffDates(dropoff_datetime,today(),'days',true)<=180){
   toSet('Last 180 Days')
}
else{
    toSet('Greater than 180 Days')
}

Example 20-5 Examples with multi-assign values

The following examples demonstrate how to work with multi-assign values.

This example takes a multi-assign attribute named ItemColor and uses toUpperCase function to convert its values to upper case:
ItemColor.collect
{toUpperCase(it)}

The following code iterates through the values in a multi-assign attribute called MedalsAwarded and adds a specific number of points for each type of medal it finds to the variable MedalValue. It then returns MedalValue, which contains the total number of points awarded for each medal in the attribute.

def MedalValue=0

for (int i in 0..cardinality(MedalsAwarded)-1) {
     if(indexOf(MedalsAwarded[i],'Gold')>=0){
          MedalValue=MedalValue+3;
     }
     else if(indexOf(MedalsAwarded[i],'Silver')>=0){
          MedalValue=MedalValue+2;
     }
     else if(indexOf(MedalsAwarded[i],'Bronze')>=0){
          MedalValue=MedalValue+1;
     }
}
MedalValue

For example, if MedalsAwarded['Gold','Silver','Gold'], the final value of MedalValue would be 8.

Here is another option for iterating through the values in a multi-assign attribute:
def MedalValue=0

for (x in MedalsAwarded) {
     if(indexOf(x,'Gold')>=0){
          MedalValue=MedalValue+3;
     }
     else if(indexOf(x,'Silver')>=0){
          MedalValue=MedalValue+2;
     }
     else if(indexOf(x,'Bronze')>=0){
          MedalValue=MedalValue+1;
     }
}
MedalValue

Example 20-6 Multi-assign value operations using method chaining

The following code iterates through a multi-assign attribute called PartIdentifier and replaces its identification numbers with X to mask them.

PartIdentifier.{collect(trim(replace(substring(it,1,10),'[0-9]','X')))}

The above example uses method chaining to perform a number of operations on the values of PartIdentifier in a single statement. The first part of the statement, PartIdentifier.collect calls the collect method on the PartIdentifier attribute. collect runs the code in the outermost set of parentheses on each of the values in the PartIdentifier multi-assign attribute.

collect first calls the substring method, which returns the substring of the String it, defined by the character in position 1 through position 10, where it is the implicit Groovy variable ranging over the numbers of PartIdentifier.

This substring is passed to the replace method, which replaces all numeric characters ('[0-9]') with X. The trim method then takes the masked String and removes all leading and trailing whitespace from it.