如何将此Java Lambda代码转换为Groovy等效项

时间:2019-01-26 14:21:44

标签: groovy

我有一个可行的方法,但需要将其转换为使用Groovy,因为代码库都是groovy。我也相信使用Groovy可以更简洁地完成此操作,但我更擅长使用lambda,因此我首先尝试了这一点,但我仍在努力实现Groovy的相同效果。如果有人可以帮助我,我将非常感激。

代码如下:

 public ArrayList<GameCredits> getCreditsList(CreditGained 
                                           creditsAccumulated, 
     Players playerList) {
    List<GameCredits> credits = creditsAccumulated.credits
    List<GameCredit> newCredits = []
    boolean playerMatched
    //Match Players to their credits and create new credits for unmatched
    playerList.players.stream()
            .filter { Player player -> player.playerKey != null }
            .each { Player player ->
                 playerMatched = false
                 credits.stream()
                .filter { GameCredit credit -> (credit?.creditIdentifier && 

                      credit?.creditIdentifier.equals(player.playerKey)) }

            .each { GameCredit credit ->
               credit.rank = player.rank
               credit.playerEmail = player.emailAddress
               credit.firstName = player.firstName
               credit.lastName = player.lastName
               playerMatched = true
        }
        if(!playerMatched){
              GameCredit credit=new GameCredit()
              credit.rank = player.rank
              credit.playerEmail = player.emailAddress
              credit.firstName = player.firstName
              credit.lastName = player.lastName
              newcredits.push(newGameCredits)
        }
    }
    credits.addAll(newCredits)
    credits
}

这是我的尝试,但我知道会有更好的方法。

public ArrayList<GameCredits> getCreditsList(CreditGained creditsAccumulated, 
     Players playerList) {
    List<GameCredits> credits = creditsAccumulated.credits
    List<GameCredit> newCredits = []
    boolean playerMatched
    //Match Players to their credits and create new credits for unmatched
    playerList.players.findAll
             { Player player -> player.playerKey != null }
            .each { Player player ->
                 playerMatched = false
                 credits
                .findAll { GameCredit credit -> (credit?.creditIdentifier && 
                 credit?.creditIdentifier.equals(player.playerKey)) }

            .each { GameCredit credit ->
               credit.rank = player.rank
               credit.playerEmail = player.emailAddress
               credit.firstName = player.firstName
               credit.lastName = player.lastName
               playerMatched = true
        }
        if(!playerMatched){
              GameCredit newGameCredits=new GameCredit()
              newGameCredits.rank = player.rank
              newGameCredits.playerEmail = player.emailAddress
              newGameCredits.firstName = player.firstName
              newGameCredits.lastName = player.lastName
              newcredits.push(newGameCredits)
        }
    }
    credits.addAll(newCredits)
    credits
}

谢谢

1 个答案:

答案 0 :(得分:0)

这是我的看法(我删除了CreditGained的副作用):

List<GameCredits> collectCredits(CreditGained credits, Players players) {
  credits.credits + players.players.collectMany { Player player ->
    if (!player.playerKey) {
      Collections.emptyList() // quick exit for player without key
    } else {
      List<GameCredit> matches = player.credits.findAll {
        it?.creditIdentifier == player.playerKey
      }

      if (matches) {
        // return credit(s) matched to player (with updated metadata)
        matches.each { GameCredit credit ->
          credit.with {
            rank = player.rank
            lastName = player.lastName
            firstName = player.firstName
            playerEmail = player.emailAddress
          }
        }
      } else {
        // return 1 new credit for player
        new GameCredit(
          rank: player.rank,
          lastName: player.lastName,
          firstName: player.firstName,
          playerEmail: player.emailAddress,
          creditIdentifier: player.playerKey // guessing you want this too...
        )
      }
    }
  }
}