Android Criteria

I have done a lot of copy and paste of my code lately so I decided to start to create some small libraries. Mostly for my own personal re-use, to learn more about releasing open source libs and of course getting rid of the copy and paste.

The first lib I decided to put together was a criteria lib. It is a library for complex filtering of in memory data.

It is licensed under the Apache 2 License so it is free to use. It can be located at github.

Features

  • List filtering
  • Single item verification
  • “And”, “Or”, “Xor” and “Not” supported

Android 4.0.3 and above support.

Usages

Import into your gradle project:

dependencies {
...
compile 'com.rakangsoftware.criteria:criteria:0.1.2'
...
}

First you need to have your model object. To make this example clean and easy I will use String as model.

Data to use

Let’s setup a list with names.

List<String> data = new ArrayList<>();
data.add("Silas");
data.add("Carmen");
data.add("Joan");
data.add("Michele");
data.add("Kari");
data.add("Marjorie");
data.add("Johnette");
data.add("Jason");
data.add("Marya");
data.add("Ola");

Implement a Criterion

Since Criteria don’t implement any real concrete filter classes we need to create one our self, here is one for checking if a name contains a string.

public class NameCriterion implements Criterion<String> {

private String mQuery;

public NameCriterion(final String query) {
mQuery = query;
}

@Override
public List<String> meet(final List<String> objects) {
List<String> result = new ArrayList<>();
for (String name : objects) {
if (name.toLowerCase().contains(mQuery)) {
result.add(name);
}
}
return result;
}

@Override
public boolean meet(final String name) {
return name.toLowerCase().contains(mQuery);
}
}

How to use

This code snippet will return a list with all names containing the letter ‘a’.

Criterion contains  = new NameCriterion("a");
List<String> result = contains.meet(data);

How to AndCriteria

This code snippet will return a list with all names containing the letters ‘a’ and ‘r’.

Criterion<String> contains = new AndCriteria<>(
new NameCriterion("a"),
new NameCriterion("r")
);
List<String>      result   = contains.meet(getNames());

This code snippet will return a list with all names containing the letters ‘a’, ‘r’ and ‘p’.

Criterion<String> contains = new AndCriteria<>(
new NameCriterion("a"),
new NameCriterion("r"),
new NameCriterion("p")
);
List<String>      result   = contains.meet(getNames());

How to OrCriteria

This code snippet will return a list with all names containing the letter ‘a’ or ‘r’.

Criterion<String> contains = new OrCriteria<>(
new NameCriterion("a"),
new NameCriterion("r")
);
List<String>      result   = contains.meet(data);

This code snippet will return a list with all names containing the letter ‘a’, ‘r’ or ‘p’.

CriterionString> contains = new OrCriteria<>(
new NameCriterion("a"),
new NameCriterion("r"),
new NameCriterion("p")
);
List<String>     result   = contains.meet(data);

How to XorCriteria

This code snippet will return a list with all names containing the letter ‘a’ or ‘r’ but not both of them.

Criterion<String> contains = new XorCriteria<>(
new NameCriterion("a"),
new NameCriterion("r")
);
List<String>      result   = contains.meet(data);

This code snippet will return a list with all names containing the any of the letters ‘a’, ‘r’ or ‘p’ but only one of them.

Criterion<String> contains = new XorCriteria<>(
new NameCriterion("a"),
new NameCriterion("r"),
new NameCriterion("p")
);
List<String>      result   = contains.meet(data);

How to NotCriteria

This code snippet will return a list with all names not containing the letter ‘a’.

Criterion<String>    contains = new NotCriterion<>(
new NameCriterion("a")
);
List<String>         result   = contains.meet(getNames());

Nested criteria.

Let's say we want all name containing 'a' or 'e' but not 'c'.

AndCriterion
|-- NotCriterion
| `-- NameCriterion ('c')
`-- OrCriterion
|-- NameCriterion ('a')
`-- NameCriterion ('r')
Criterion<String> orCriterion = new OrCriteria<>(
new NameCriterion("a"),
new NameCriterion("r")
);
Criterion<String> notCriterion = new NotCriterion<>(
new NameCriterion("c")
);
Criterion<String> andCriterion = new AndCriteria<>(
orCriterion,
notCriterion
);
List<String> result = andCriterion.meet(data);

Single item verification

To just verify one single item. Just call on the meet function with a single item and you will get a boolean back.

boolean result = anyCriterion.meet("adam");