复杂对象的常规闭包

发布于 2024-12-05 09:37:47 字数 330 浏览 2 评论 0原文

我如何为下面的复杂场景编写闭包,

def empList=[];
EmployeeData empData = null;
empData=new EmployeeDataImpl("anish","nath");
empList.add(empData );
empData=new EmployeeDataImpl("JOHN","SMITH");
empList.add(empData );

Employee employee= new Employee(empList);

如何编写闭包以便使用 emplyee 对象获得 empData 详细信息?有什么提示想法吗?

How can i write closure for below complex scenario

def empList=[];
EmployeeData empData = null;
empData=new EmployeeDataImpl("anish","nath");
empList.add(empData );
empData=new EmployeeDataImpl("JOHN","SMITH");
empList.add(empData );

Employee employee= new Employee(empList);

how can i write closure so that using emplyee object i will get empData details ? any hint Idea ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北恋 2024-12-12 09:37:47

我不确定你的意思...

要迭代 EmployeeData,你可以使用 each

empList.each { println it }

要查找特定条目,你可以使用 find

// Assume EmployeeData has a firstName property, you don't show its structure
EmployeeData anish = empList.find { it.firstName == 'anish' }

或者你可以使用 findAll 找到所有具有给定姓氏的人:

def smiths = empList.findAll { it.surname == 'Smith' }

这取决于你想要“闭包”做什么...

编辑

对,在你解释了你的 DSL 之后你想要的,我想出了这个(这将解决给定的问题):

@groovy.transform.Canonical
class EmployeeData {
  String firstName
  String lastName
}

class Employee {
  List<EmployeeData> empList = []

  Employee( List<EmployeeData> list ) {
    empList = list
  }
}

class EmployeeDSL {
  Employee root

  List propchain = []

  EmployeeDSL( Employee root ) {
    this.root = root
  }

  def propertyMissing( String name ) {
    // if name is 'add' and we have a chain of names
    if( name == 'add' && propchain ) {
      // add a new employee
      root.empList << new EmployeeData( firstName:propchain.take( 1 ).join( ' ' ), lastName:propchain.drop( 1 ).join( ' ' ) )
      // and reset the chain of names
      propchain = []
    }
    else {
      // add this name to the chain of names
      propchain << name
      this
    }
  }
}

Employee emp = new Employee( [] )

new EmployeeDSL( emp ).with {
  anish.nath.add
  tim.yates.add
}

emp.empList.each {
  println it
}

它使用 take()drop(),所以你需要 groovy 1.8.1+

希望它有意义!然而,这有点奇怪的语法(使用关键字 add 将字符串添加为 Employee),与其这样,最好提出类似于 MarkupBuilder 的东西。代码>通过实施BuilderSupport

I'm not sure what you mean...

To iterate over the EmployeeData, you could just use each:

empList.each { println it }

To find a particular entry, you can use find:

// Assume EmployeeData has a firstName property, you don't show its structure
EmployeeData anish = empList.find { it.firstName == 'anish' }

Or you can find all people with a given surname by using findAll:

def smiths = empList.findAll { it.surname == 'Smith' }

It depends what you want "the closure" to do...

Edit

Right, after you have explained your DSL that you want, I have come up with this (which will solve the given problem):

@groovy.transform.Canonical
class EmployeeData {
  String firstName
  String lastName
}

class Employee {
  List<EmployeeData> empList = []

  Employee( List<EmployeeData> list ) {
    empList = list
  }
}

class EmployeeDSL {
  Employee root

  List propchain = []

  EmployeeDSL( Employee root ) {
    this.root = root
  }

  def propertyMissing( String name ) {
    // if name is 'add' and we have a chain of names
    if( name == 'add' && propchain ) {
      // add a new employee
      root.empList << new EmployeeData( firstName:propchain.take( 1 ).join( ' ' ), lastName:propchain.drop( 1 ).join( ' ' ) )
      // and reset the chain of names
      propchain = []
    }
    else {
      // add this name to the chain of names
      propchain << name
      this
    }
  }
}

Employee emp = new Employee( [] )

new EmployeeDSL( emp ).with {
  anish.nath.add
  tim.yates.add
}

emp.empList.each {
  println it
}

It uses take() and drop(), so you will need groovy 1.8.1+

Hope it makes sense! It's a bit of an odd syntax however (using the keyword add to add the strings as an Employee), and instead of this it might be better to come up with something similar to MarkupBuilder by implementing BuilderSupport

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文