Groovy编写单个方法而不是每个函数两个

时间:2012-08-06 08:41:05

标签: groovy

我是一名Groovy初学者

这两个each调用都执行类似的任务,但具有s2PublicLifecycleInstance的不同属性。

如何编写一个单独的函数来执行以下操作,以便我只能通过方法调用来执行任务?

def scheduledStatusArray =[]
def publishedStatusArray =[]

s2PublicLifecycleInstance.each {
  if(it.scheduledInMyLearnStatus == "No"){
    scheduledStatusArray.add(1)
  } else if(it.scheduledInMyLearnStatus == "Yes"){
    scheduledStatusArray.add(2)
  }else{
    scheduledStatusArray.add(3)
  }
}

s2PublicLifecycleInstance.each {
  if(it.publishedOnTrainingClassesStatus == "No"){
    publishedStatusArray.add(1)
  }else if(it.publishedOnTrainingClassesStatus == "Yes"){
    publishedStatusArray.add(2)
  }else{
    publishedStatusArray.add(3)
  }
}

我想要这样的事情:

def publishedStatusArray = functionCall(s2PublicLifecycleInstance, propertyName)

在Groovy中有可能吗?

4 个答案:

答案 0 :(得分:2)

你可以使用闭包和currying:

def checkStatus = { prop,instances ->
   def statuses = instances.collect {
      switch( it[prop] ) {
      case "Yes": 1
         break
      case "No": 2
         break
      default: 3
      }
   }
   return statuses
}

def checkScheduledStatus = checkStatus.curry("scheduledInMyLearnStatus")
def checkPublishedStatus = checkStatus.curry("publishedOnTrainingClassesStatus")

def scheduledStatusArray = checkScheduledStatus(s2PublicLifecycleInstance)
def publishedStatusArray = checkPublishedStatus(s2PublicLifecycleInstance)

答案 1 :(得分:1)

def myMethod(param, arrayToAdd) {
  if(param == "No"){
                arrayToAdd.add(1)
            }else if(param == "Yes"){
                arrayToAdd.add(2)
            }else{
                arrayToAdd.add(3)
            }
}


s2PublicLifecycleInstance.each {myMethod(it.scheduledInMyLearnStatus, scheduledStatusArray)}

s2PublicLifecycleInstance.each {myMethod(it.publishedOnTrainingClassesStatus, publishedStatusArray)}

答案 2 :(得分:1)

您可以使用以下方法执行此操作:

List check( List lifecycle, String param ) {
  lifecycle.collect { it ->
    switch( it."$param" ) {
      case "No":
        return 1
      case "Yes":
        return 2
      default:
        return 3
    }
  }
}

def scheduledStatusArray = check( s2PublicLifecycleInstance, 'scheduledInMyLearnStatus' )
def publishedStatusArray = check( s2PublicLifecycleInstance, 'publishedOnTrainingClassesStatus' )

或者,您可以这样做(代码较少):

List check( List lifecycle, String param ) {
  def retvals = [ 'No':1, 'Yes':2 ].withDefault { 3 }
  lifecycle.collect { it ->
    retvals[ it."$param" ]
  }
}

另一种选择是使用带多个赋值的注入:

def (scheduledStatusArray, publishedStatusArray) = s2PublicLifecycleInstance.inject( [[],[]] ) { lists, it ->
  lists[ 0 ] << ( it.scheduledInMyLearnStatus == 'No' ? 1 :
                  it.scheduledInMyLearnStatus == 'Yes' ? 2 : 3 )
  lists[ 1 ] << ( it.publishedOnTrainingClassesStatus == 'No' ? 1 :
                  it.publishedOnTrainingClassesStatus == 'Yes' ? 2 : 3 )
  lists
}

答案 3 :(得分:0)

我同意加桑。我可以补充一点,那个 def publishedStatusArray = functionCall(s2PublicLifecycleInstance, propertyName) 在groovy中是可能的。功能:

  

functionCall(def s2PublicLifecycleInstance, String propertyName){ s2PublicLifecycleInstance."$propertyName" }

将返回您想要的属性。