将nisnetgrouptriple字符串转换为对象

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

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

(host,user,domain)
    (,user,)
    (host,,)
    (host,user,)
    (,,domain)

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

public class NetgroupTriple {
    private String hostname; 
    private String username;
    private String domainName; 

    public String getHostname() {
        return hostname;
    }

    public void setHostname(String hostname) {
        this.hostname = hostname;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getDomainName() {
        return domainName;
    }

    public void setDomainName(String domainName) {
        this.domainName = domainName;
    }
}

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

public static NetgroupTriple fromString(String member) {

    NetgroupTriple triple = new NetgroupTriple();

    String[] split = member.split(",");
    if(split.length != 3)
        throw new Exception("bad");

    if(!split[0].equals("("))
        triple.setHostname(split[0].replace("(",""));
    if(!split[1].isEmpty())
        triple.setUsername(split[1]);
    if(!split[2].equals(")"))
        triple.setDomainName(split[2].replace(")",""));
    return triple;
}

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

col17t5w

col17t5w1#

我想这是可行的:

public NetgroupTriple(String hostname, String username, String domainName){
    this.hostname = hostname;
    this.username = username;
    this.domainName = domainName;
}

然后解析:

public static NetgroupTriple fromString(String member) {
    String[] split = member.split(",");
    if(split.length != 3)
        throw new Exception(String.format("Could not convert member to NetgroupTriple: %s", member));

    return new NetgroupTriple(
            split[0].equals("(") ? null : split[0].replace("(",""),
            split[1].equals("") ? null : split[1],
            split[2].equals(")") ? null: split[2].replace(")",""));

}

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

jaxagkaj

jaxagkaj2#

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

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

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

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

所以你的 fromString() 看起来像

public static NetgroupTriple fromString(String member) {
    String[] split = member.substring(1, member.length() - 1).split(",");
    if(split.length != 3)
        throw new Exception("bad");

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

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

public NetgroupTriple(String host, String user, String domain) {
    hostname = host;
    username = user;
    domainName = domain;        
}

public String getHostname() {
    return hostname;
}

public String getUsername() {
    return username;
}

public String getDomainName() {
    return domainName;
}

}

相关问题