Groovy 生成器支持

发布于 2024-12-06 14:44:04 字数 357 浏览 3 评论 0原文

我如何使用 groovy 构建器支持构建上述模式

emp = empFileFactory.root()
{
  emp(id: '3', value: '1')

  emp(id:'24')
  {
    emp(id: '1', value: '2')
    emp(id: '6', value: '7')
    emp(id: '7', value: '1')
  }

  emp(id: '25')
  {
    emp(id: '1', value: '1')
    emp(id: '6', value: '7')
  }

}

我正在尝试在 groovy 中构建上述结构有人可以解释一下我如何实现这一点

How can i build the above pattern using groovy builder support

emp = empFileFactory.root()
{
  emp(id: '3', value: '1')

  emp(id:'24')
  {
    emp(id: '1', value: '2')
    emp(id: '6', value: '7')
    emp(id: '7', value: '1')
  }

  emp(id: '25')
  {
    emp(id: '1', value: '1')
    emp(id: '6', value: '7')
  }

}

i'm trying to build the above strucutre in groovy can some one explain how could i acieve this

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

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

发布评论

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

评论(2

等往事风中吹 2024-12-13 14:44:04

你可以做这样的事情(这没有错误处理,只是为我不希望被调用的方法返回 null):

// First define our class to hold our created 'Emp' objects
@groovy.transform.Canonical
class Emp {
  String id
  String value
  List<Emp> children = []
}

class EmpBuilder extends BuilderSupport{
  def children = []
  protected void setParent(Object parent, Object child){
    parent.children << child
  }
  protected Object createNode(Object name){
    if( name == 'root' ) {
      this
    }
    else {
      null
    }
  }
  protected Object createNode(Object name, Object value){
    null
  }
  protected Object createNode(Object name, Map attributes){
    if( name == 'emp' ) {
      new Emp( attributes )
    }
    else {
      null
    }
  }
  protected Object createNode(Object name, Map attributes, Object value){
    null
  }
  protected void nodeCompleted(Object parent, Object node) {
  }
  Iterator iterator() { children.iterator() }
}

然后,如果我们用你所需的构建器代码调用它,如下所示:

b = new EmpBuilder().root() {
  emp(id: '3', value: '1')

  emp(id:'24') {
    emp(id: '1', value: '2')
    emp(id: '6', value: '7')
    emp(id: '7', value: '1')
  }

  emp(id: '25') {
    emp(id: '1', value: '1')
    emp(id: '6', value: '7')
  }
}

我们可以打印出 '像这样的树

b.each { println it }

,看看我们得到了我们要求的结构:

Emp(3, 1, [])
Emp(24, null, [Emp(1, 2, []), Emp(6, 7, []), Emp(7, 1, [])])
Emp(25, null, [Emp(1, 1, []), Emp(6, 7, [])])

You can do something like this (this has no error handling, and just returns null for methods that I don't expect to be called):

// First define our class to hold our created 'Emp' objects
@groovy.transform.Canonical
class Emp {
  String id
  String value
  List<Emp> children = []
}

class EmpBuilder extends BuilderSupport{
  def children = []
  protected void setParent(Object parent, Object child){
    parent.children << child
  }
  protected Object createNode(Object name){
    if( name == 'root' ) {
      this
    }
    else {
      null
    }
  }
  protected Object createNode(Object name, Object value){
    null
  }
  protected Object createNode(Object name, Map attributes){
    if( name == 'emp' ) {
      new Emp( attributes )
    }
    else {
      null
    }
  }
  protected Object createNode(Object name, Map attributes, Object value){
    null
  }
  protected void nodeCompleted(Object parent, Object node) {
  }
  Iterator iterator() { children.iterator() }
}

Then, if we call this with your required builder code like so:

b = new EmpBuilder().root() {
  emp(id: '3', value: '1')

  emp(id:'24') {
    emp(id: '1', value: '2')
    emp(id: '6', value: '7')
    emp(id: '7', value: '1')
  }

  emp(id: '25') {
    emp(id: '1', value: '1')
    emp(id: '6', value: '7')
  }
}

We can print out the 'tree' like so

b.each { println it }

and see we get the structure we asked for:

Emp(3, 1, [])
Emp(24, null, [Emp(1, 2, []), Emp(6, 7, []), Emp(7, 1, [])])
Emp(25, null, [Emp(1, 1, []), Emp(6, 7, [])])
世态炎凉 2024-12-13 14:44:04

您想要实现扩展 BuilderSupport类,这很容易做到。这里有一个非常好的教程

您需要实现一些方法,但命名应该非常不言自明:

  • createNode 创建一个节点(每个节点都有一个名称和可选属性和/或一个值)
  • setParent code> 将一个节点指定为另一个节点的父节点

就这样。

You want to implement extend the BuilderSupport class, which is pretty easy to do. There's a pretty nice tutorial here.

You need to implement a few methods, but the naming should be pretty self-explanatory:

  • createNode creates a node (each node has a name and optional attributes and/or a value)
  • setParent assigns a node as another nodes parent

That's pretty much it.

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