I work under the assumption that you have several constants like Group.GROUP_TYPE_NAME_HYDRO
, and all your methods are just like the one you show us, but with different constants (I believe you should add those other methods to your question, since without them there is no visible repeated code).
You have two possible scenarios (again, with more code would be easier):
1) You are only changing the product type constant, keeping the other two all the same in your other methods. If that’s the case, you can just extract a more generic method like:
public List<Product> getAllProductsOfType(YourConstantType productType) throws RepositoryException {
return getProductsByTypeAndScope(
getProductTypeId(productType, getAllProductTypes()),
getScopeTypeId(Group.SCOPE_TYPE_NAME_HTX, getAvailableScopes()) ,
getScopeTypeId(Group.SCOPE_TYPE_NAME_PROJECT, getAvailableScopes())
);
}
Note that I don’t know what your constants are, since you did’t put them on your question, that’s why YourConstantType
is there.
Then your existing methods would just call this new one with their parameter.
2) Another scenario: You also have different scopes on your methods. In this case, I might go with a builder pattern
Also… is that a checked exception what I see there? I hope not …
solved Refactor code java [closed]