Code Monkey home page Code Monkey logo

filter-library's Introduction

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 !

filter-library's People

Contributors

omaraflak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.