Git Product home page Git Product logo

easy-java-events's Introduction

Easy-Java-Events

EJE provides accessible methods for handling events/actions/listeners. Add this as dependency to your project via Maven/Gradle/Sbt/Leinigen (requires Java 8 or higher).

Learn the basics in 10 seconds:

Event<Integer> onValueChanged = new Event<>();

onValueChanged.addAction(value -> { 
    System.out.println("New value: "+value); 
}); 

onValueChanged.execute(10); // Prints out "New value: 10"
onValueChanged.execute(5); // Prints out "New value: 5"

Learn everything in 5 minutes:

class EventTest {
    @Test
    void example() {
        Event<Integer> onValueChanged = new Event<>();
        Action<Integer> myAction = onValueChanged.addAction(value -> { // Stays in memory and gets executed every time.
            System.out.println("New value: "+value);
            // You can throw exceptions in here
        }, Exception::printStackTrace); // and catch them here.

        onValueChanged.execute(10); // Prints out "New value: 10"
        onValueChanged.execute(5); // Prints out "New value: 5"

        // One time actions that only get executed once, are also supported:
        onValueChanged.addOneTimeAction(value -> {
            System.out.println("New value: "+value+" bye!");
        }, Exception::printStackTrace);

        onValueChanged.execute(7); // Prints out "New value: 7" and "New value: 7 bye!"

        // If more and more actions get added over time
        // you must remove unused actions. There are some util methods for this case.
        // First we create a new action with additional params: isOneTime=false and object=null.
        Action<Integer> actionToRemove = onValueChanged.addAction((action, value) -> {
            System.out.println("New value: "+value+", but I will be gone soon :/");
        }, Exception::printStackTrace, false, null);

        // Then we initialise the cleaner thread for this event, which checks
        // its actions list every 100ms for actions that
        // fulfill the condition "object != null" and removes those.
        onValueChanged.initCleaner(100, object -> object != null, Exception::printStackTrace);

        // Once we want to remove the action, we simply give it an object that is not null.
        // The cleaner then removes it in the next check.
        actionToRemove.object = new Object(); // Note that you can store any type of object here.
    }
}

easy-java-events's People

Contributors

osiris-team avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

anoop-qasolve

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.