summaryrefslogtreecommitdiffstats
blob: 4c236bf5998e97cddda4cc2c70d7e2f13662632f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
 * Copyright (c) 2010 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */
/*
 * Copyright (c) 2010-2012, Isode Limited, London, England.
 * All rights reserved.
 */
package com.isode.stroke.eventloop;

/**
 * An Event object represents an external event that requires processing. 
 * A concrete {@link EventLoop} implementation must ensure that Events are 
 * processed in the application's main event loop. 
 * 
 * <p>An application (and any concrete class of EventLoop) should treat Events
 * as opaque objects to be passed to {@link EventLoop#handleEvent(Event)}.
 *  
 * <p>Event processing is designed to happen in the main event loop, so for
 * example a GUI application might handle Events in the following manner:
 * <pre>
 *  EventLoop eventLoop = new EventLoop() {
 *     protected void post(final Event event) {
 *       // Invoke the callback in the AWT display thread     
 *       SwingUtilities.invokeLater(new Runnable() {
 *           public void run() {
 *              handleEvent(event);
 *          }
 *       });
 *     }
 *  };
 *  .
 *  .
 *  .
 *  client.onConnected.connect(new Slot() {
 *     public void call() {
 *        // This will always be called inside the event dispatching thread
 *        updateSomeTextFieldInTheGUI("Connected");
 *     }
 *  });
 * </pre>
 * 
 */
public class Event {

    public interface Callback {
        void run();
    }

    Event(EventOwner owner, Callback callback, int id) {
        this.owner = owner;
        this.callback = callback;
        this.id = id;
    }

    @Override
    public boolean equals(Object other) {
        if (other instanceof Event) {
            return id == ((Event) other).id;
        }
        return false;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 23 * hash + this.id;
        return hash;
    }
    final int id;
    final EventOwner owner;
    public final Callback callback;
    
    @Override
    public String toString() {
        return "Event with id=" + id + 
        " callback=" + callback +
        (owner == null ? " (no owner information)" : " owner=" + owner);
        
    }
}