I have the following code:
public final boolean doesExistById(Long id) {
return dataAccessObject.findById(id) != null;
}
public final boolean doesExistByName(String name) {
return dataAccessObject.findByName(name) != null;
}
public final boolean doesExistByDisplayName(String displayName) {
return dataAccessObject.findByDisplayName(displayName) != null;
}
public final boolean doesExistByWebId(String webId) {
return dataAccessObject.findByWebId(webId) != null;
}
My Product
class has properties id, name, displayName, wedId
.
dataAccessObject.findBy____()
returns an object of type Product
, if it can be found in the data store, or null
if it cannot.
I would like to reduce this chunk of code, if possible, because I have many objects that require the doesExist()
pattern as above. The client code will only know one of these properties.
A possible solution I thought of would be to do this:
public final boolean doesExist(Long id, String name, String displayName, String webId) {..}
and then call it with null
for unknown fields while using if
statements to determine which field has a value. But is there another way that is more elegant?