Best android open-source packages and libraries.

Filter Library

Android library to filter any object in a list using a simple annotation.
Updated 2 years ago

Filter Library Download

Android library to filter any object in a list using a simple annotation.

The library comes up with various handy methods to filter your objects. All you have to do is add an annotation !

@Filterable

Create a class, generate the getters, and set the annotation @Filterable.

@Filterable
public class User {
    private int age;
    private String firstName;
    private String lastName;

    public int getAge() { return age; }
    public String getFirstName() { return firstName; }
    public String getLastName() { return lastName; }
}

That's all you need to do !

Rebuild your project, and the annotation processor should have generated a class called UserFilter which has methods with names that matches the class attributes (i.e. age(), firstName(), lastName()) :

List<User> users = getUserList();

List<User> result = UserFilter.builder()
        .age().greaterThan(10)
        .firstName().startsWith("A")
        .on(users);

Double filter

If your class contains an object as attribute, for instance :

@Filterable
public class User {
    private int age;
    private String name;
    private Shape shape;

    public int getAge() { return age; }
    public String getName() { return name; }
    public Shape getShape() { return shape; }

Simply annotate the class Shape with @Filterable :

@Filterable
public class Shape {
    private float size;
    private float mass;

    public float getSize() { return size; }
    public float getMass() { return mass; }
}

Then filter the field shape with the function matches() :

List<User> result = UserFilter.builder()
        .age().smallerThan(50)
        .shape().matches(ShapeFilter.builder()
            .size().greaterThan(1.80)
            .build(), ShapeFilter.class)
        .on(users);

Crazy condition

The library provides several methods to compare objects, including a regex() method. Still, if you can't formulate your condition, you can write it manually as follows :

List<User> result = UserFilter.builder()
        .extraCondition(new Condition<User>() {
            @Override
            public boolean verify(User object) {
                return someCrazyCondition(object);
            }
        })
        .on(users);

With lambdas :

List<User> result = UserFilter.builder()
        .extraCondition(this::someCrazyCondition)
        .on(users);

Difference with Java 8 Stream

Unlike Stream from Java 8, this library will return a list of references and will not waste memory. If you need a copy, just add the method copy() to the filter.

The library also includes a method called forEach which executes commands on the user(s) that respect your filter. For example, if you want to search the user which id is 42 and change its name to "answer" you would do it like this :

UserFilter.builder()
        .id().equalsTo(42)
        .forEach(new Operation<User>() {
            @Override
            public void execute(User object) {
                object.setName("Answer");
            }
        })
        .on(users);

With lambdas :

UserFilter.builder()
        .id().equalsTo(42)
        .forEach(user -> user.setName("Answer"))
        .on(users);

Note that you don't even have to store the result of the function.

Gradle

implementation 'me.aflak.libraries:filter-annotation:1.2'
annotationProcessor 'me.aflak.libraries:filter-processor:1.2'

Any feedback would be greatly appreciated !

Tags filter