I have a piece of code where two objects (incoming request
object and a profile
object) are to be checked for matching conditions.
So the first method is as below where I check whether the profile
object will be applied to incoming request
:
public Response apply(Request request) {
..// some logic
Profile profile = getProfile();
if( isProfileApplicableOnRequest(profile, request) ) {
}
return null;
}
private void isProfileApplicableOnRequest(Profile profile, Request request) {
List<BigInteger> applicability = profile.getApplicability();
return (applicability != null && applicability.contains(BigInteger.ONE))
|| profileMatchesFrom(profile.getIDList(), request.getFrom())
|| profileMatchesResourceType(profile.getResourceTypes(), request.getResourceType())
|| profileMatchesOperation(profile.getOperations(), request.getOperation())
|| profileMatchesToParameter(profile.getResourceIDs(), request.getTo())
|| profileMatchesReleaseVersion(profile.getReleaseVersions(), request.getReleaseVersionIndicator());
}
So here we take values set in request
object and match whether its present in the profile
object. The profile object can have a Lists in which a parameter from request
is checked for.
The functions above in conditional are like:
private boolean profileMatchesResourceType(List<BigInteger> primitiveProfileResourceTypes, BigInteger requestResourceType){
return (primitiveProfileResourceTypes != null && requestResourceType != null
&& primitiveProfileResourceTypes.contains(requestResourceType));
}
private boolean profileMatchesFrom(List<String> idList, String from) {
String regex = null;
for (String id : idList) {
regex = ("\\Q" + id + "\\E").replace("*", "\\E.*\\Q"); // Replaces wildcard "*" inside ID pattern with Java regex ".*"
if(from.matches(regex))
return true;
}
return false;
}
private boolean profileMatchesToParameter(List<String> primitiveProfileResourceIds, String toParameter){
return (primitiveProfileResourceIds != null && toParameter != null
&& primitiveProfileResourceIds.contains(toParameter));
}
private boolean profileMatchesOperation(List<BigInteger> primitiveProfileOPerations, BigInteger requestOperation){
return (primitiveProfileOPerations != null && requestOperation != null
&& primitiveProfileOPerations.contains(requestOperation));
}
So here I feel I could simplify my isProfileApplicableOnRequest
method with multiple OR
conditions in some way.
Any comments ?