I know that passing a an Object is not a good practise. But in this case it seems the best solution to me.
public void doSomething(final Object obj) {
// some code
...
if (obj instanceof InputStream) {
document = PDDocument.load((InputStream) obj);
} else if (obj instanceof File) {
document = PDDocument.load((File) obj);
} else {
throw new IllegalArgumentException("Argument must be an instance of " + InputStream.class.getName() + " or " + " " + File.class.getName() + ".");
// more code
...
}
}
An alternative solution would have more duplicated code (all the line before and after PDDocument.load(obj);)
public void doSomething(final InputStream obj) {
// some code
...
document = PDDocument.load(obj);
// more code
...
}
}
public void doSomething(final File obj) {
// some code
...
document = PDDocument.load(obj);
// more code
...
}
}
Due to the duplicated code I prefer the first solution.
Do you know any better solution to solve this problem?