Combining Predicates

This post explains how we can combine two or more predicates using logical operations as mentioned below
1) and
2) or
3) negate

Below is an example

Person


package Function;

public class Person {
    private String ssn;
    private String fname;
    private String lname;
    private String description;
    private int age;
    
    public String getSsn() {
        return ssn;
    }
    public void setSsn(String ssn) {
        this.ssn = ssn;
    }
    public String getFname() {
        return fname;
    }
    public void setFname(String fname) {
        this.fname = fname;
    }
    public String getLname() {
        return lname;
    }
    public void setLname(String lname) {
        this.lname = lname;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        sb.append(this.ssn).append(",");
        sb.append(this.fname).append(",");
        sb.append(this.lname).append(",");
        sb.append(this.age).append(",");
        sb.append(this.description);
        sb.append("}");
        return sb.toString();
    }
}

First Name Predicate


package Function;

import java.util.function.Predicate;

public class FNamePredicate implements Predicate {
    private String fName;
    
    @Override
    public boolean test(Person person) {
        if(fName.equals(person.getFname())) {
            return true;
        }
        return false;
    }

    public void setfName(String fName) {
        this.fName = fName;
    }
}

Last Name Predicate


package Function;

import java.util.function.Predicate;

public class LNamePredicate implements Predicate {
    private String lName;
    
    @Override
    public boolean test(Person person) {
        if(lName.equals(person.getLname())) {
            return true;
        }
        return false;
    }

    public void setlName(String lName) {
        this.lName = lName;
    }
}

Main Class


package Function;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

public class PredicateDemo2 {
    public static void main(String[] args) {
        List list = new ArrayList(10);
        for(int i = 0 ; i < 10; i++) {
            Person person = new Person();
            person.setSsn("ssn" + i);
            person.setFname("fname" + i);
            person.setLname("lname" + i);
            person.setDescription("description" + i);
            person.setAge(i);
            
            list.add(person);
        }
        
        display("Input List", list);
        
        System.out.println();
        
        FNamePredicate fNamePredicate = new FNamePredicate();
        fNamePredicate.setfName("fname5");
        
        LNamePredicate lNamePredicate = new LNamePredicate();
        lNamePredicate.setlName("lname5");
        
        Predicate namePredicate = fNamePredicate.and(lNamePredicate);
        
        List list1 = filter(namePredicate, list);
        display("Name and query", list1);
        
        System.out.println();
        
        lNamePredicate.setlName("lname6");
        namePredicate = fNamePredicate.or(lNamePredicate);
        list1 = filter(namePredicate, list);
        display("Name or query", list1);
        
        System.out.println();
        
        Predicate fNamePredicate1 = fNamePredicate.negate();
        list1 = filter(fNamePredicate1, list);
        display("Negate of First name query", list1);
    }
    
    public static List filter(Predicate predicate, List list) {
        List filteredList = new ArrayList();
        for(Person person : list) {
            if(predicate.test(person)) {
                filteredList.add(person);
            }
        }
        
        return filteredList;
    }
    
    public static void display(String queryName, List list) {
        System.out.println(queryName);
        for(Person person : list) {
            System.out.println(person);
        }
    }
}

Output

Input List
{ssn0,fname0,lname0,0,description0}
{ssn1,fname1,lname1,1,description1}
{ssn2,fname2,lname2,2,description2}
{ssn3,fname3,lname3,3,description3}
{ssn4,fname4,lname4,4,description4}
{ssn5,fname5,lname5,5,description5}
{ssn6,fname6,lname6,6,description6}
{ssn7,fname7,lname7,7,description7}
{ssn8,fname8,lname8,8,description8}
{ssn9,fname9,lname9,9,description9}

Name and query
{ssn5,fname5,lname5,5,description5}

Name or query
{ssn5,fname5,lname5,5,description5}
{ssn6,fname6,lname6,6,description6}

Negate of First name query
{ssn0,fname0,lname0,0,description0}
{ssn1,fname1,lname1,1,description1}
{ssn2,fname2,lname2,2,description2}
{ssn3,fname3,lname3,3,description3}
{ssn4,fname4,lname4,4,description4}
{ssn6,fname6,lname6,6,description6}
{ssn7,fname7,lname7,7,description7}
{ssn8,fname8,lname8,8,description8}
{ssn9,fname9,lname9,9,description9}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s