Giter VIP home page Giter VIP logo

alpine's Introduction

Alpine

The lightweight event system framework

Tutorial

For starters, we must create an EventBus to handle event flow. The Alpine Event Framework provides a default implementation of EventBus, so we'll be using that.

To create a new event bus, instantiate the type me.zero.alpine.EventManager in some sort of handler file.

public class Core {
    
    public static final EventBus EVENT_BUS = new EventManager();
}

We will make references to this event bus whenever we are in need of carrying out event related tasks.

Now to registering objects to listen to the event bus, we'll need to create a Listener object, and give it some sort of generic type parameter. This generic type is the type of event we'll be listening for. I will be using java.lang.String for this example.

public class EventProcessor implements Listenable {
    
    @EventHandler
    private Listener<String> stringListener = new Listener<>(str -> {
        System.out.println(str);
    });
}

In order to use our newly created "EventProcessor" class, we need to instantiate it, and then subscribe it to the EventBus. Active EventBus subscribers get their Listeners invoked whenever a post call is made with the same type as the listener. Classes containing static listeners may not be subscribed to the EventBus.

public class Main {
    
    public static void main(String[] args) {
        EventProcessor processor = new EventProcessor();
        Core.EVENT_BUS.subscribe(processor);
        Core.EVENT_BUS.post("Test");
    }
}

The code above should give a single line console output of "Test".

Listeners can have filters applied, so that only certain events may be passed to them. Below is an example of a String Filter.

public class LengthOf3Filter implements Predicate<String> {
    
    @Override
    public boolean test(String t) {
        return t.length() == 3;
    }
}

Here, we only accept String input if the length is 3. To add our filter to our listener, we just create a new instance of it and add it onto the listener parameters.

public class EventProcessor {
    
    @EventHandler
    private Listener<String> stringListener = new Listener<>(str -> {
        System.out.println(str);
    }, new LengthOf3Filter());
}

Now if we run our code, we shouldn't get any console output because the length of "Test" is not equal to 3. Filters that don't have a type matching that of its parent Listener aren't valid.

alpine's People

Contributors

zeromemes avatar madakai avatar leafhacker avatar

Stargazers

TechnoThilo avatar

Watchers

James Cloos 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.