将nisnetgrouptriple字符串转换为对象

wztqucjr  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(429)

我正在尝试转换nisnetgrouptriple字符串,其格式如下:

  1. (host,user,domain)
  2. (,user,)
  3. (host,,)
  4. (host,user,)
  5. (,,domain)

变成一个netgrouptriple对象,如下所示:

  1. public class NetgroupTriple {
  2. private String hostname;
  3. private String username;
  4. private String domainName;
  5. public String getHostname() {
  6. return hostname;
  7. }
  8. public void setHostname(String hostname) {
  9. this.hostname = hostname;
  10. }
  11. public String getUsername() {
  12. return username;
  13. }
  14. public void setUsername(String username) {
  15. this.username = username;
  16. }
  17. public String getDomainName() {
  18. return domainName;
  19. }
  20. public void setDomainName(String domainName) {
  21. this.domainName = domainName;
  22. }
  23. }

我有一个粗略的函数来完成它,但我希望有一个更干净的方式使用流。

  1. public static NetgroupTriple fromString(String member) {
  2. NetgroupTriple triple = new NetgroupTriple();
  3. String[] split = member.split(",");
  4. if(split.length != 3)
  5. throw new Exception("bad");
  6. if(!split[0].equals("("))
  7. triple.setHostname(split[0].replace("(",""));
  8. if(!split[1].isEmpty())
  9. triple.setUsername(split[1]);
  10. if(!split[2].equals(")"))
  11. triple.setDomainName(split[2].replace(")",""));
  12. return triple;
  13. }

有没有人知道一个更干净的方法来实现这一点?

col17t5w

col17t5w1#

我想这是可行的:

  1. public NetgroupTriple(String hostname, String username, String domainName){
  2. this.hostname = hostname;
  3. this.username = username;
  4. this.domainName = domainName;
  5. }

然后解析:

  1. public static NetgroupTriple fromString(String member) {
  2. String[] split = member.split(",");
  3. if(split.length != 3)
  4. throw new Exception(String.format("Could not convert member to NetgroupTriple: %s", member));
  5. return new NetgroupTriple(
  6. split[0].equals("(") ? null : split[0].replace("(",""),
  7. split[1].equals("") ? null : split[1],
  8. split[2].equals(")") ? null: split[2].replace(")",""));
  9. }

还是没有我希望的那么优雅。

展开查看全部
jaxagkaj

jaxagkaj2#

如果你知道总是有封装括号,你可以从一开始就删除它们

  1. String[] split = member.substring(1, member.length() - 1).split(",");

那么,既然它出现的顺序 member 总是(“主机”、“用户”、“域”)那么你可以做什么

  1. NetgroupTriple triple = new NetgroupTriple(split[0], split[1], split[2]);

所以你的 fromString() 看起来像

  1. public static NetgroupTriple fromString(String member) {
  2. String[] split = member.substring(1, member.length() - 1).split(",");
  3. if(split.length != 3)
  4. throw new Exception("bad");
  5. NetgroupTriple triple = new NetgroupTriple(split[0], split[1], split[2]);
  6. return triple;
  7. }

这会让你 NetgroupTriple 成为 immutable ```
public class NetgroupTriple {
private String hostname;
private String username;
private String domainName;

  1. public NetgroupTriple(String host, String user, String domain) {
  2. hostname = host;
  3. username = user;
  4. domainName = domain;
  5. }
  6. public String getHostname() {
  7. return hostname;
  8. }
  9. public String getUsername() {
  10. return username;
  11. }
  12. public String getDomainName() {
  13. return domainName;
  14. }

}

展开查看全部

相关问题