简体   繁体   中英

Converting an nisNetgroupTriple String to an Object

I'm trying to convert nisNetgroupTriple Strings which can be of this format:

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

Into a NetgroupTriple object which looks like this:

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;
    }
}

I've got this gross function to do it, but I'm hoping there is a cleaner way using streams.

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;
}

Does anyone know a cleaner way to accomplish this?

If you know that there are always encapsulating parentheses, you can remove those right from the start

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

Then, since it appears the order of the incoming member is always ("host", "user", "domain") then you can do

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

So your fromString() looks like

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;
}

Which will allow your NetgroupTriple to be 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;
    }
}

I guess this works:

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

Then the parsing:

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(")",""));

}

Still not as elegant as I'm hoping for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM