This is a question that I'm not sure how to solve the problem in Java. I want to make triple statements based on three data types, URI, String or Literal, each type is encoded differently. I wrote coding methods that accept these types.
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(); }
But since I can accept any combination of these types, this will require 9 makeStatement functions that basically do the same thing, and this seems like a bad idea, especially since I might want to add another type later.
Normally I would answer such a question with a proposal to create a superclass, but I canβt edit String, URI and Literal. Another option would be to define
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"; }
and then check the classes for each argument, but I find it not very elegant. Another suggestion is to define something like makeStatement (URI subjectURI, String subjectString, Literal subjectLiteral, URI predicateURI .. etc.), and then check which arguments are null and go from there, but that would mean typing a lot of zeros. when i call the function. The third option would be https://stackoverflow.com/a/412960/2129 , but again this will require quite some additional input when calling the makeStatement function.
Any suggestions?