Check site inclusion and exclusion I have two string variables site_inclusion and site_exclusion.  If site_inclusion has some values, then I don't care what values site_exclusion contains.  This means site_inclusion overrides site_exclusion if site_inclusion has some values. But if site_inclusion is null and site_exclusion has some values, then go into the site_exclusion loop.

Requirements:


If site_inclusion and site_exclusion both are null then set useTheSynthesizer as true;
If site_inclusion is not null and it matches with the regexPattern then set useTheSynthesizer as true. And I don't care what values are there in site_exclusion.
If site_inclusion is null and site_exclusion is not null and site_exclusion does not match the regexPattern, then set useTheSynthesizer to true.


I wrote the below code but somehow I think I am repeating some stuff here in the if/else loop. Any code improvements that fulfill my conditions will be appreciated.

String site_inclusion = metadata.getSiteInclusion();
String site_exclusion = metadata.getSiteExclusion();

// fix for redundant data per site issue
if(site_inclusion != null &amp;&amp; site_inclusion.matches(regexPattern)) {
    useTheSynthesizer = true;
} else if(site_exclusion != null &amp;&amp; !(site_exclusion.matches(regexPattern))) {
    useTheSynthesizer = true;
} else if(site_inclusion == null &amp;&amp; site_exclusion == null ) {
    useTheSynthesizer = true;
}
Please spinning it to a separate function. Here is what I think is a little more simpler implementation.

public boolean checkInclusion(String site_i, String site_e, Regex re) {
    if (site_i != null)
        return site_e.matches(re);
    else if(site_e != null)
        return !site_e.matches(re)))
    else
        return true;
}


In your implementation, what happens when the site inclusion has values but does not match, and site_exclusion is null? It seems to not match any of the conditions, which is not in sync with the requirements from my reading.

Also what happens when the site inclusion has values but does not match, and site_exclusion has values that does not match? In your implementation, it seems like site exclusion takes precedence. But that does not seem to be the requirement.

Perhaps I am reading either requirement wrongly or your implementation conditions wrongly?

A rather terse implementation that is same as above is,

public boolean checkInclusion(String site_i, String site_e, Regex re) {
    if (site_i != null) return site_i.matches(re);
    return site_e == null || !site_e.matches(re)
}