Java Grails字符串的简单处理

Java Grails字符串的简单处理,java,arrays,sorting,grails,groovy,Java,Arrays,Sorting,Grails,Groovy,具有如下类消息: public class Message { private String text; public Message(String text) { this.text = text; } public int getLength() { return text.length() } } 我想创建另一个具有以下函数的类: 功能1:为每个用户对文件夹和子文件夹中的邮件进行排序 功

具有如下类消息:

public class Message { 
     private String text; 
     public Message(String text) { 
          this.text = text;
     } 
     public int getLength() { 
          return text.length()
     } 
}
我想创建另一个具有以下函数的类:

功能1:为每个用户对文件夹和子文件夹中的邮件进行排序

功能2:对于按文件夹显示的每个用户:

  • 文件夹及其所有子文件夹中的邮件总数

  • 文件夹及其所有子文件夹中邮件的平均长度

注意:
子文件夹可以包含子文件夹,不需要对某个深度进行限制。

首先为文件夹实现类。一旦你做完了。实现Comparable并重写compareTo()函数。它比较消息。您可以访问此博客以了解如何使用compareTo()


我强烈建议的第一件事是更新问题的标题,使其反映您的要求。您的挑战与字符串处理几乎没有关系,也不简单

幸运的是,Groovy有一些整洁的实用程序可以处理这种挑战

节点生成器 使用Groovy可以创建对象树。能够表示消息和文件夹结构的树。这就是它的作用:

import groovy.util.NodeBuilder
import groovy.util.Node

def root = new NodeBuilder().root {
    message 'world'
    message 'hello'

    'John' {
        'Work' {
            message 'TPS report'
        }

        'Secret Stuff' {
            message 'plea to mistress'
            message 'rejection from ex mistress'
        }
    }    

   'Jane' {
       message 'John, I know about her!'
   }
}
通过此树的实例,您可以使用它执行所请求的功能:

职能1:排序 排序不是递归完成的。相反,它是按需完成的

// Sort the messages in the root of the tree, on the fly.
assert root.message.toSorted { it.value() }.toString() == '[message[attributes={}; value=hello], message[attributes={}; value=world]]'
功能2a:消息总数 函数2b:平均消息长度 好处:访问特定消息 在所有这些情况下,我没有使用您的消息类。但是使用NodeBuilder的子类来代替消息类是很容易的

// Sort the messages in the root of the tree, on the fly.
assert root.message.toSorted { it.value() }.toString() == '[message[attributes={}; value=Message(hello)], message[attributes={}; value=Message(world)]]'

// Count the number of messages within the 'John' folder; including 'Work' and 'Secret Stuff' folders.
assert root.John.'**'.message.size() == 3

// Calculate the average length of the messages in the 'John' folder: 52 / 3 = 17.3333333333
assert root.John.'**'.message.sum { it.value().size() } / root.John.'**'.message.size() == 17.3333333333


// Access the first message in the 'Jane' folder.
assert root.Jane.message[0].value() instanceof Message 
assert root.Jane.message[0].value().text == 'John, I know about her!'
MessageTreeBuilder 可以使用自定义生成器使用消息实例填充树。但首先,让我们改进消息类

@groovy.transform.Canonical
public class Message { 
     String text; 

     int size() { 
          return text.size()
     } 
}
这是MessageTreeBuilder

class MessageTreeBuilder extends NodeBuilder {
    Object createNode(Object name, Object value) {
        def payload = name == 'message' ? new Message(value) : value

        [currentNode, name, payload] as Node
    }
}
是的,这就是整个班级

然后,您可以使用相同的精确树,但使用MessageTreeBuilder实例而不是NodeBuilder

// Sort the messages in the root of the tree, on the fly.
assert root.message.toSorted { it.value() }.toString() == '[message[attributes={}; value=Message(hello)], message[attributes={}; value=Message(world)]]'

// Count the number of messages within the 'John' folder; including 'Work' and 'Secret Stuff' folders.
assert root.John.'**'.message.size() == 3

// Calculate the average length of the messages in the 'John' folder: 52 / 3 = 17.3333333333
assert root.John.'**'.message.sum { it.value().size() } / root.John.'**'.message.size() == 17.3333333333


// Access the first message in the 'Jane' folder.
assert root.Jane.message[0].value() instanceof Message 
assert root.Jane.message[0].value().text == 'John, I know about her!'
class MessageTreeBuilder extends NodeBuilder {
    Object createNode(Object name, Object value) {
        def payload = name == 'message' ? new Message(value) : value

        [currentNode, name, payload] as Node
    }
}
// Sort the messages in the root of the tree, on the fly.
assert root.message.toSorted { it.value() }.toString() == '[message[attributes={}; value=Message(hello)], message[attributes={}; value=Message(world)]]'

// Count the number of messages within the 'John' folder; including 'Work' and 'Secret Stuff' folders.
assert root.John.'**'.message.size() == 3

// Calculate the average length of the messages in the 'John' folder: 52 / 3 = 17.3333333333
assert root.John.'**'.message.sum { it.value().size() } / root.John.'**'.message.size() == 17.3333333333


// Access the first message in the 'Jane' folder.
assert root.Jane.message[0].value() instanceof Message 
assert root.Jane.message[0].value().text == 'John, I know about her!'