• Saves Valuable Time
  • Trusted Accuracy for 12 Years
  • 15-Day Money-Back Guarantee

Java Equivalent to C# Events

Java does not have any syntax which is specific to events. C# has the event keyword which allows you to declare an event to which you can easily subscribe or unsubscribe multiple listeners in addition to letting all the listeners know when the event occurs. To do this in Java, you need to replace the event's delegate type with a functional interface and replace the event declaration with a new field declaration using our generic 'Event' helper class. The 'Event' class takes care of two different listener collections and has methods for adding or removing an event subscription. Two different collections are necessary since C# allows subscribing to an event by specifying a method name or delegate instance(named listener) or subscribing by specifying a lambda.

Here's a simple example:

C#:

public class EventSource
{
    public delegate void FooDelegate();
    public event FooDelegate FooEvent;

    private void RaiseFooEvent()
    {
        FooEvent();
    }
}

public class EventClient
{
    public void SubscribeAndUnsubscribe()
    {
        EventSource t = new EventSource();
        //subscribe via method name:
        t.FooEvent += HandleFooEvent;
        //unsubscribe via method name:
        t.FooEvent -= HandleFooEvent;

        //subscribe via lambda:
        t.FooEvent += () =>
        {
            HandleFooEvent();
        };
    }

    private void HandleFooEvent()
    {
    }
}

Java:

public class EventSource
{
    @FunctionalInterface
    public interface FooDelegate
    {
        void invoke();
    }
    public Event<FooDelegate> FooEvent = new Event<FooDelegate>();

    private void RaiseFooEvent()
    {
        //invoke all listeners:
        for (FooDelegate listener : FooEvent.listeners())
        {
            listener.invoke();
        }
    }
}

public class EventClient
{
    public final void SubscribeAndUnsubscribe()
    {
        EventSource t = new EventSource();
        //subscribe via method name:
        t.FooEvent.addListener("HandleFooEvent", () -> HandleFooEvent());
        //unsubscribe via method name:
        t.FooEvent.removeListener("HandleFooEvent");

        //subscribe via lambda:
        t.FooEvent.addListener(() ->
        {
            HandleFooEvent();
        });
    }

    private void HandleFooEvent()
    {
    }
}

//----------------------------------------------------------------------------------------
// Copyright © 2007 - 2017 Tangible Software Solutions Inc.
// This class can be used by anyone provided that the copyright notice remains intact.
//
// This class is used to convert C# events to Java.
//----------------------------------------------------------------------------------------
public final class Event<T>
{
    private java.util.Map<String, T> namedListeners = new java.util.HashMap<String, T>();
    public void addListener(String methodName, T namedEventHandlerMethod)
    {
        if (!namedListeners.containsKey(methodName))
            namedListeners.put(methodName, namedEventHandlerMethod);
    }
    public void removeListener(String methodName)
    {
        if (namedListeners.containsKey(methodName))
            namedListeners.remove(methodName);
    }

    private java.util.List<T> anonymousListeners = new java.util.ArrayList<T>();
    public void addListener(T unnamedEventHandlerMethod)
    {
        anonymousListeners.add(unnamedEventHandlerMethod);
    }

    public java.util.List<T> listeners()
    {
        java.util.List<T> allListeners = new java.util.ArrayList<T>();
        allListeners.addAll(namedListeners.values());
        allListeners.addAll(anonymousListeners);
        return allListeners;
    }
}


Use C# to Java Converter to convert from C# to Java.     

Additional resource: Java and C# Equivalents


Copyright © 1997 – 2017 Tangible Software Solutions Inc.