この項では、条件文および複数割当値を使用した変換の例を示します。
例20-1 単純な条件文
次のコードはif...else文を使用して、tip_amount
属性の値に基づいてレコードにフラグを割り当てます。
// 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' }
例20-2 高度な条件文
次のコードでは、tip_amount
属性に基づいて各レコードにフラグを割り当てます。 ただし、これは一連のif...else文を使用して、パーセンテージの範囲ごとに異なるフラグを割り当てます:
// 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' }
例20-3 変数を使用する詳細な条件文
この例では、前述の例と同じ操作を実行しますが、変数を使用してヒントのパーセントを格納し、文ごとに計算します。
// 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' }
例20-4 日付ロジックを使用した詳細な条件文
次のコードでは、一連のelse...if文を使用して、複数割当値を作成します。 現在の日付とdropoff_datetime
属性の間の時間を日数で計算するために、diffDatesWithPrecision
関数が使用されます。 次に、そのレコードに含まれる様々な時間範囲を指定する文字列値のリストを割り当てます。
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') }
例20-5 複数割当値を含む例
次の例は、複数割当の値を使用する方法を示しています。
ItemColor
という名前の複数割当て属性を取得し、toUpperCase
関数を使用してその値を大文字に変換します:
ItemColor.collect {toUpperCase(it)}
次のコードは、MedalsAwarded
というマルチ割当て属性の値を反復処理し、変数MedalValue
に見つけた各タイプのmedalに特定のポイント数を追加します。 次に、属性内の各メダルに対して付与されたポイントの合計数を格納するMedalValue
が返されます。
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
たとえば、MedalsAwarded['Gold','Silver','Gold']
の場合、MedalValue
の最終的な値は8
です。
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
例20-6 メソッド・チェーンを使用した値の割当て操作
次のコードは、PartIdentifier
というマルチ割当属性を反復処理し、識別番号をX
に置き換えてマスクします。
PartIdentifier.{collect(trim(replace(substring(it,1,10),'[0-9]','X')))}
前述の例では、メソッド・チェーンを使用して、1つの文のPartIdentifier
の値に対して多数の操作を実行しています。 PartIdentifier.collect
は、文の最初の部分で、PartIdentifier
属性のcollect
メソッドをコールします。collect
では、PartIdentifier
複数割当属性の各値に対して最も外側のカッコでコードを実行します。
collect
では、最初にsubstring
メソッドがコールされます。これは、位置1から位置10の文字で定義された文字列it
の部分文字列を戻します。it
は、PartIdentifier
の番号の間の暗黙的なGroovy変数です。
この部分文字列はreplace
メソッドに渡され、すべての数値文字('[0-9]'
)をX
で置き換えます。 次にtrim
メソッドは、マスクされた文字列を受け取り、先頭と末尾のすべての空白を削除します。