[SalesForce] how to pass operators dynamically to if condition

enter image description here

I am trying to implement multiple filters on visual force page to generate custom reports by using external system data from rest call.I am not going to store anything in salesforce objects.So i have to do search by looping throught data

like

if(searchField1=searchVal1 && searchField2=searchVal2 || searchField3=searchVal3)){
    system.debug('***'+criteria matched);
}

but the problem is i have to change the && and || operators according to user selection from drop-down like if user selects OR and And in dropdown it has to chnage as

if(searchField1=searchVal1 || searchField2=searchVal2 && searchField3=searchVal3)){
    system.debug('***'+criteria matched);
}

so i was wondering is there any way to achieve this dynamically…

like

if(searchField1=searchVal1 'operator1' searchField2=searchVal2 'operator2' searchField3=searchVal3)){
        system.debug('***'+criteria matched);
    }

happy to hear any changes in the code…

Best Answer

Ignoring the issue mention in the comment, you can create methods that allow this sort of code:

if (op(searchField1 == searchVal1, operator1, searchField2 == searchVal2) ...

where the method would be:

private Boolean op(Boolean b1, String operator, Boolean b2) {
    return operator == 'and' ? (b1 && b2) : (b1 || b2);
}