この項では、条件文および複数割当値を使用した変換の例を示します。
例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メソッドは、マスクされた文字列を受け取り、先頭と末尾のすべての空白を削除します。