0

我是 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

4 回答 4

2

你可以使用闭包和柯里化:

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)
于 2012-08-06T13:25:02.313 回答
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)}
于 2012-08-06T08:51:20.890 回答
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
}
于 2012-08-06T09:02:16.393 回答
0

我同意加山。我可以补充一下,这
def publishedStatusArray = functionCall(s2PublicLifecycleInstance, propertyName) 在 groovy 中是可能的。功能:

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

将返回您想要的属性。

于 2012-08-06T09:06:43.063 回答