0

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 ?

Siddharth Trikha
  • 593
  • 1
  • 7
  • 14
  • 1
    Shouldn't the return type for "isProfileApplicableOnRequest" be boolean, not void? – Doc Brown May 18 '22 at 12:19
  • 1
    Other than the return type, the code for `isProfileApplicableOnRequest()` looks just fine to me. Don't change a thing. – Robert Harvey May 18 '22 at 14:16
  • Why would `profile.getApplicability()` ever return a `null` value? Worse case scenario it should return a list with zero items in it. That would remove the null-checks. – Greg Burghardt May 18 '22 at 15:02

0 Answers0