First, I do echo the points raised by @rolfl@rolfl and @maaartinus@maaartinus, and I also feel that there are probably other parts of your codebase that you need to review given the usage highlighted in your question. With that said, given the limited context that you have presented, I can think of a third alternative.
Can you create 'lite' instances of your Product
class that only contain 'one of these properties' for your existing class to use?
For example, if one part of your client code only knows the web ID:
Product lookupByWebId = Product.ofWebId("WebID");
That will create a Product
object where only the webId
field is populated and the other fields null
. Your existing class can then have this method:
public final boolean isExisting(Product product) {
// ignoring getters for illustration purposes
if (product.id != null) {
return dataAccessObject.findById(product.id) != null;
}
if (product.name != null) {
return dataAccessObject.findByName(product.name) != null;
}
if (product.displayName != null) {
return dataAccessObject.findByDisplayName(product.displayName) != null;
}
if (product.webId != null) {
return dataAccessObject.findByWebId(product.webId) != null;
}
throw new IllegalArgumentException("No queryable fields.");
}
The benefit here is that when you enhance your Product
class with newer fields, you do not have to change method signatures to accommodate them.
If you do however find a way to even workaround the need to keep this kind of 'check' methods as suggested in @rolfl@rolfl and @maaartinus@maaartinus answers, then please do that instead. :)