Giter Site home page Giter Site logo

eventhandler's Introduction

EventHandler

license made-with-java last-commit

A simple and lightweight event handler system made in Java.

Features

  • Scalable and fast
  • Custom event support
  • Cancellable events
  • Event priorities
  • Event inheritence support (ex: TestEvent extends TestSuperEvent, TestEvent gets called from the handler and then TestSuperEvent will also be called)

Event Implementation

public class TestEvent extends Event implements EventCancellable {
    private boolean isCancelled;
    private String data;

    // can be whatever
    public TestEvent(String data) {
        this.data = data;
    }

    public String getData() {
        return data;
    }

    @Override
    public boolean isCancelled() {
        return this.isCancelled;
    }

    @Override
    public void setCancelled(boolean cancelled) {
        this.isCancelled = cancelled;
    }
}

Listener Class Example

public class SomeListener implements EventListener {
    // will be invoked
    @HandleEvent
    public void onTestEvent(TestEvent event) {
        System.out.println("Data: " + event.getData());
    }
    
    // will not be invoked
    public void onNotInvokedTestEvent(TestEvent event) {
        System.out.println("Data: " + event.getData());
    }
}

Usage

// create the handler and listener instances
EventHandler handler = new EventHandler();
ListenerTest testListener = new ListenerTest();

// register the listener
handler.registerEvents(testListener);

// make sure data is not overridden yet
Assert.assertEquals("None", testListener.getData());
Assert.assertEquals(0, testListener.getTimesRan());

// call the test event
handler.callEvent(new TestEvent("New data"));

// data should now be what was passed into the TestEvent constructor
Assert.assertNotEquals("None", testListener.getData());
Assert.assertEquals("New data", testListener.getData());
Assert.assertEquals(1, testListener.getTimesRan());

// unregister the listener
handler.unregisterEvents(testListener);

// call the test event, this should not change the data now
handler.callEvent(new TestEvent("Different data"));
Assert.assertNotEquals("Different data", testListener.getData());
Assert.assertEquals(1, testListener.getTimesRan());

See tests for more.

eventhandler's People

Contributors

oscaz avatar quantiom avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 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.