Skip to content

bekmeh/fr-filter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

fr-filter

This is a Filter API which determines whether a resource matches certain criteria.

Example Usage

Method 1

Filter filter = new Filter("age", Comparator.LT, 20).and("height", Comparator.GT, 5.5);
Resource user = new Resource();
user.put("age", 15);
user.put("height", 6);
filter.matches(user); // Should evaluate to true

In this case, the methods can be chained indefinitely.

Method 2

Filter filter = new Filter(
                          new OrNode(
                                      new NotNode(new LessThanNode("age", 20)), 
                                      new GreaterThanNode("height", 5.5)));
Resource user = new Resource();
user.put("age", 25);
user.put("height", 4);
filter.matches(user); // Should evaluate to true

Approach

My initial approach was a simplistic one (see first commit). However, the code became overly complex and it did not take into account order of precedence.

I decided to model the problem using a binary tree instead, because it better reflects the structure of boolean logic. Here, the evaluate() method determines the boolean values all the way from the bottom branch back to the top. The Filter class provides a nicer interface for creating and evaluating FilterNodes, and allows the use of chained and()/or()/not() methods. These are used to place nodes in the tree based on the order of precedence of the operator.

References

https://en.wikipedia.org/wiki/Logical_connective

https://en.wikipedia.org/wiki/Binary_tree

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages