RSS Feed for This PostCurrent Article

Java: Predicate in Commons Collection

Predicate in Commons Collection is very useful for logic evaluation. The available predicates should fulfills most of your logic requirements, or you can even write your own custom predicate.

You can combine predicates together to form a compound predicate and also write custom predicate. E.g

public class PredicateTest {
 
    static class Student {
        int age;
        String name;
 
        Student(int age, String name) {
            this.age = age;
            this.name = name;
        }
 
        public int getAge() {
            return age;
        }
 
        public void setAge(int age) {
            this.age = age;
        }
 
        public String getName() {
            return name;
        }
 
        public void setName(String name) {
            this.name = name;
        }
    }
 
    static class GreaterThanPredicate implements Predicate {
        private int value;
 
        public GreaterThanPredicate(int value) {
            this.value = value;
        }
 
        public boolean evaluate(Object obj) {
            boolean accept = false;
            if (obj instanceof Student) {
                accept = ((Student) obj).getAge() > value;
            }
            return (accept);
        }
    }
 
    static class NameEqualsPredicate implements Predicate {
        private String value;
 
        public NameEqualsPredicate(String value) {
            this.value = value;
        }
 
        public boolean evaluate(Object obj) {
            boolean accept = false;
            if (obj instanceof Student) {
                accept = ((Student) obj).getName().equals(value);
            }
            return (accept);
        }
    }
 
    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
 
        // Create Base Predicates
        Predicate isNameEqual = new NameEqualsPredicate("Jack");
 
        Predicate isGreater = new GreaterThanPredicate(12);
 
        // Create 2 argument logical predicate composites
        Predicate andPredicate = new AndPredicate(isNameEqual, isGreater);
        Predicate orPredicate = new OrPredicate(isNameEqual, isGreater);
 
 
        System.out.println("Result: " + andPredicate.evaluate(new Student(13, "Jack")));
 
 
}


Trackback URL


RSS Feed for This Post2 Comment(s)

  1. Nitin Nanivadekar | Jul 7, 2009 | Reply

    Hi.
    I am sorry to have placed a comment with a wrong subject here [This is not a spam though

    I am interested in the latest ATSMS library. I had used a long way back and it was an excellent software.
    Have you publishded a latest version(you had 2.0 in work, I believe)?
    If yes, can you please provide a download link?

  2. admin | Jul 7, 2009 | Reply

    The latest version is in C#, which I am still setting up the website and plan to release it as a free library in August 2009. The C# version should be able to do a lot of things than the existing version, which is very outdated already.

Sorry, comments for this entry are closed at this time.