在Java中接受不同类型的参数

Rha*_*and 7 java overloading

这是一个关于我不确定如何在Java中解决的问题.我想根据三种类型的数据(URI,String或Literal)制作三重语句,每种类型的编码方式不同.我编写了接受这些类型的编码方法.

public static String makeStatement(URI subject, URI predicate, String object) {
    return " " + encode(subject) + " " + encode(predicate) + " " + encode(object) + ".\n";
}

public static String makeStatement(String subject, URI predicate, String object) {
    return " " + encode(subject) + " " + encode(predicate) + " " + encode(object) + ".\n";
}

public static String makeStatement(URI subject, URI predicate, Literal object) {
    return " " + encode(subject) + " " + encode(predicate) + " " + encode(object) + ".\n";
}

private static String encode(String binding) {
    return "?" + binding;
}

private static String encode(URI uri) {
    return "<" + uri.stringValue() + ">";
}

private static String encode(Literal literal) {
    return "\"" + literal.stringValue() + "\"" + literal.getDatatype();
}
Run Code Online (Sandbox Code Playgroud)

但是因为我可以接受这些类型的任何组合,这将需要9个makeStatement函数,它们基本上做同样的事情,这似乎是一个坏主意,特别是因为我可能有可能我想稍后添加另一种类型.

通常我会回答这个问题,建议创建一个superClass,但我不能编辑String,URI和Literal.另一种选择是定义

public static String makeStatement(Object subject, Object predicate, Object object) {
    String encodedSubject = "", encodedPredicate = "", encodedObject = "";
    if (subject.getClass().equals(URI.class)) {
        encodedSubject = encode((URI) subject);
}
    return " " + encode(encodedSubject) + " " + encode(encodedPredicate) + " " + encode(encodedObject) + ".\n";
}
Run Code Online (Sandbox Code Playgroud)

然后检查每个参数的类,但我认为这不是很优雅.另一个建议是定义类似makeStatement(URI subjectURI,String subjectString,Literal subjectLiteral,URI predicateURI等等),然后检查哪些参数为null并从那里开始,但这意味着当我调用时输入很多空值功能.第三种选择是/sf/answers/870561471/,但在调用makeStatement函数时,这又需要一些额外的输入.

有什么建议?

Rok*_*okL 3

您可以使用构建器模式:

    public class StatementMaker {
    private static String encode(String binding) {
        return "?" + binding;
    }

    private static String encode(URI uri) {
        return "<" + uri.stringValue() + ">";
    }

    private static String encode(Literal literal) {
        return "\"" + literal.stringValue() + "\"" + literal.getDatatype();
    }

    public static Statement from(String b) {
        return new Statement(encode(b));
    }

    public static Statement from(URI b) {
        return new Statement(encode(b));
    }

    public static Statement from(Literal b) {
        return new Statement(encode(b));
    }

    public static class Statement {

        private StringBuilder buf;
        private Statement(String s) {
            buf = new StringBuilder(" ");
            buf.append(s);
        }

        public Statement with(String s) {
            buf.append(" ").append(encode(b));
            return this;
        }

        public Statement with(URI s) {
            buf.append(" ").append(encode(b));
            return this;
        }

        public Statement with(Literal s) {
            buf.append(" ").append(encode(b));
            return this;
        }

        public String toString() {
            return buf.toString() + ".\n";
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

您现在可以这样构造语句:

StatementMaker.from(subject).with(predicate).with(object).toString()

在需要语句的代码中,您可以使用静态导入进一步缩短代码:

import static my.package.StatementMaker.from;

然后语句简化为:

from(subject).with(predicate).with(object).toString()

您可以向内部类添加另外 3 个方法:

public static class Statement {

    private StringBuilder buf;
    private Statement(String s) {
        buf = new StringBuilder(" ");
        buf.append(s);
    }

    public Statement with(String s) {
        buf.append(" ").append(encode(b));
        return this;
    }

    public Statement with(URI s) {
        buf.append(" ").append(encode(b));
        return this;
    }

    public Statement with(Literal s) {
        buf.append(" ").append(encode(b));
        return this;
    }

    public String and(String s) {
        buf.append(" ").append(encode(b));
        return buf.toString() + ".\n";
    }

    public String and(URI s) {
        buf.append(" ").append(encode(b));
        return buf.toString() + ".\n";
    }

    public String and(Literal s) {
        buf.append(" ").append(encode(b));
        return buf.toString() + ".\n";
    }


    public String toString() {
        return buf.toString() + ".\n";
    }

}
Run Code Online (Sandbox Code Playgroud)

然后你可以使用避免toString()这样的调用:

String statement = from(subject).with(predicate).and(object);