groovy closure for complex object prblm stateme2

[亡魂溺海] 提交于 2020-01-17 05:12:09

问题


I have an java interface

    public interface IPerson {
      Person  addPerson(String name );
      Person  addPerson(String name1,String name2);
      Person  addPerson(String name,String[] details);
      Person  addPerson(String name,String name1,String[] details);
      Person  addPerson(String name,List<String> details);
    }

With PersonImpl .java being:

class PersonImpl implemets Iperson {
       ..
  // and interface methods implemtation

}

and my person.java looks like

class Person {
    def firstName;
    def lastName;
}

And my PersonTest.groovy looks like

def PersonImpl   person  = new PersonImpl();

person.addPerson("anish")
person.addPerson("anish","nath")
person.addPerson("john","smith")
person.addPerson("tim","yates")
def list=[];
list.add("abc")
list.add("qpr")
person.addPerson("anish",list)
person.addPerson("nath","11", [".docsDevNmAccessStatus.1", "Integer", "4"])
person.addPerson("nath","11", [".docsDevNmAccessStatus.1", "String", "4"])

Is there any way to define the DSL for this interface so that i easily called addOperation easily?

The problem is that the IPerson interface cannot be changed.

how can i write dsl something like

addPerson "anihs" "nath" //call to person.addPerson("anish","nath")
addPerson "tim" "kates" 

//simlary of other interface method any suggestion

回答1:


In Groovy something like addPerson "anihs" "nath" is not really allowed. If you wanted two arguments you would have to use a comma. So the best you can get is addPerson "anihs", "nath". But this method calls is just hanging in the air, the context is missing. One quite easy version would be of course:

def PersonImpl person = new PersonImpl();
person.with {
  addPerson "anish"
  addPerson "anish","nath"
  addPerson "john","smith"
  addPerson "tim","yates"
  def list = ["abc", "qpr"]
  addPerson "anish",list
  addPerson "nath","11", [".docsDevNmAccessStatus.1", "Integer", "4"]
  addPerson "nath","11", [".docsDevNmAccessStatus.1", "String", "4"]
}

though I am not sure this is enough for you.



来源:https://stackoverflow.com/questions/7482933/groovy-closure-for-complex-object-prblm-stateme2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!