2017-04-19 105 views
0

我知道Java,但对Groovy来说是全新的。 Groovy中有一些遗留代码可供使用。Groovy每个关闭错误

我有以下方法在Groovy:

def notificationPrefsByFruit = mapMyNotificationsByFruits(prefs, fruits) 

当我mapMyNotificationsByFruits方法在第一线调试,我得到prefs

def mapMyNotificationsByFruits(prefs,fruits) { 
    def map = [:] 
    prefs.each { MainNotification cn -> 
     cn.fruits.each { 
      MyNotification an -> 
      def ex = map[(an.fruitId)] 
      if (!ex) ex = [] 
      ex.add(an) 
      map[(an.fruitId)] = ex 
     } 
    } 
    log.info("map is: $map") 
    return map 
} 

以上方法会从如下的另一种方法叫做

MainNotification [someId=ABC123, [email protected], fruits=[{fruitId=XYZ123, someField=0}]] 

在运行这段代码时,我得到以下错误:

groovy.lang.MissingMethodException: No signature of method: com.somepackage.SomeClass$_mapMyNotificationsByFruits_closure5$_closure10.doCall() is applicable for argument types: (groovy.json.internal.LazyMap) values: [[{fruitId=XYZ123, someField=0}]] 

这里有什么问题?

是什么,这些线做:

MyNotification an -> 
     def ex = map[(an.fruitId)] 
     if (!ex) ex = [] 
     ex.add(an) 
     map[(an.fruitId)] = ex 

它是一个铸造的问题?

更换上面下面的代码行解决问题:

MyNotification an = it 
def ex = map[(an.fruitId)] 
if (!ex) ex = [] 
ex.add(an) 
map[(an.fruitId)] = ex 

但我不知道,如果这两个代码块是相同的,我是正确的修复它。

在此先感谢!

+0

什么是输入和预期输出? – Rao

+0

您没有提供足够的信息,但试图解决您的问题。 – dsharew

回答

0

是什么算法呢?
鉴于MainNotification实例的列表。但它fruitId分组的MyNotification实例。

鉴于

[ 
    [ 
     someId: "ABC123", 
     email: "[email protected]", 
     fruits: [ 
      [fruitId : "XYZ123", someField: 0], 
      [fruitId : "XYZ124", someField: 6], 
     ] 
    ], 

    [ 
     someId: "XYSK", 
     email: "[email protected]", 
     fruits: [ 
      [fruitId : "XYZ123", someField: 5], 
      [fruitId : "XYZ124", someField: 2], 
      [fruitId : "XYZ144", someField: 9], 
     ] 
    ] 
] 

预期输出:

[ 
    XYZ123 : [ 
      [fruitId : "XYZ123", someField: 0], 
      [fruitId : "XYZ123", someField: 5] 
     ], 
    XYZ124 : [ 
      [fruitId : "XYZ124", someField: 6], 
      [fruitId : "XYZ124", someField: 2] 
     ], 
    XYZ144 : [ 
      [fruitId : "XYZ144", someField: 9] 
     ] 
] 

更正:

  • 水果可变决不会在该方法中使用,以便请删除它。
  • 该方法正在调用prefs变量上的每个方法,因此它是 期望前缀为List<MainNotification>而不是 MainNotification
  • 在嵌套关闭此代码:cn.fruits.each期待每个 水果是MyNotification实例(这是主要的修复您 问题顺便说一句)。

所以每个输入数据,你给你的问题,你没有张贴MainNotification和源代码MyNotification我模仿他们是这样的:

class MainNotification{ 
    String someId; 
    String email; 
    List<MyNotification> fruits; 
} 

class MyNotification{ 
    String fruitId; 
    int someField; 
} 

随着更正和假设这里是工作的代码:

def mapMyNotificationsByFruits(prefs,fruits) { 
    def map = [:] 
    prefs.each { MainNotification cn -> 
     cn.fruits.each { MyNotification an -> 
      def ex = map[(an.fruitId)] 
      if (!ex) ex = [] 
      ex.add(an) 
      map[(an.fruitId)] = ex 
     } 
    } 
    println ("map is: $map") 
    return map 
} 

MainNotification mainNotification = new MainNotification(someId: "ABC123", 
     email: "[email protected]", 
     fruits: [new MyNotification(fruitId : "XYZ123", someField: 0)] 
); 

MyNotification fruits = null; //never used on the method 

List<MainNotification> prefs = [mainNotification]; 

def notificationPrefsByFruit = mapMyNotificationsByFruits(prefs, fruits) 
+1

因此,我的问题结束时的替代代码与以前的代码相同..对吗?因为我将它作为MyNotification进行投射。这是对的吗 ? – Nik

+0

是的抱歉忘了提及他们应该导致相同的效果。 – dsharew

+0

感谢您的帮助! – Nik