| 1 | package org.jtoolkit.essence.data; |
| 2 | |
| 3 | import org.jetbrains.annotations.NotNull; |
| 4 | import org.jtoolkit.essence.app.pojo.DataValue; |
| 5 | import org.jtoolkit.essence.app.pojo.Datable; |
| 6 | import org.jtoolkit.essence.concurrency.Immutable; |
| 7 | import org.jtoolkit.essence.concurrency.CallbackSet; |
| 8 | |
| 9 | import java.io.DataInput; |
| 10 | import java.io.DataOutput; |
| 11 | import java.io.IOException; |
| 12 | |
| 13 | /* |
| 14 | Copyright 2006 Peter Lawrey |
| 15 | |
| 16 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 17 | you may not use this file except in compliance with the License. |
| 18 | You may obtain a copy of the License at |
| 19 | |
| 20 | http://www.apache.org/licenses/LICENSE-2.0 |
| 21 | |
| 22 | Unless required by applicable law or agreed to in writing, software |
| 23 | distributed under the License is distributed on an "AS IS" BASIS, |
| 24 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 25 | See the License for the specific language governing permissions and |
| 26 | limitations under the License. |
| 27 | */ |
| 28 | |
| 29 | /** |
| 30 | * An Event which can be process by an Event Handler or Event Listener. |
| 31 | * <p/>A handler takes all Events as Event objects. A handler is not interested in the contents of the event only in routering it. |
| 32 | * <p/>A listener is notified of events of interest in a decoded way.. |
| 33 | * |
| 34 | * @author Peter Lawrey |
| 35 | */ |
| 36 | @Immutable |
| 37 | public interface Event { |
| 38 | /** |
| 39 | * Notify this listener of an event. |
| 40 | */ |
| 41 | public void notifyListener(@NotNull Listener l); |
| 42 | |
| 43 | /** |
| 44 | * A Listenable can have event handlers and listeners. |
| 45 | */ |
| 46 | public interface Listenable { |
| 47 | /** |
| 48 | * @return This object can have a set of listeners. |
| 49 | */ |
| 50 | @NotNull public ListenerSet getListenerSet(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Process an event based on its contents. |
| 55 | */ |
| 56 | public interface Listener { |
| 57 | public void onStatusEvent(boolean online) throws IllegalStateException; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Notify that a Listeneable is online/offline. This event is triggers when a Listener is added for the first time. |
| 62 | */ |
| 63 | public class StatusEvent extends DataValue implements Datable, Event { |
| 64 | public final boolean online; |
| 65 | |
| 66 | public StatusEvent(boolean online) { |
| 67 | this.online = online; |
| 68 | } |
| 69 | |
| 70 | public void notifyListener(@NotNull Listener l) { |
| 71 | l.onStatusEvent(online); |
| 72 | } |
| 73 | |
| 74 | @SuppressWarnings("UnusedDeclaration") |
| 75 | public StatusEvent(@NotNull DataInput in) throws IOException { |
| 76 | online = in.readBoolean(); |
| 77 | } |
| 78 | |
| 79 | public void writeData(@NotNull DataOutput out) throws IOException { |
| 80 | out.writeBoolean(online); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * A set of handler and listeners. |
| 86 | */ |
| 87 | public interface ListenerSet extends CallbackSet<Event> { |
| 88 | public void add(@NotNull Listener l); |
| 89 | |
| 90 | public void notifyReset(boolean online); |
| 91 | |
| 92 | public void remove(@NotNull Listener l); |
| 93 | } |
| 94 | } |