Try to bind together queries, triggers, and actions.
[rhynodge.git] / src / main / java / net / pterodactylus / reactor / Reaction.java
1 /*
2  * Reactor - Reaction.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.reactor;
19
20 /**
21  * A {@code Reaction} binds together {@link Query}s, {@link Trigger}s, and
22  * {@link Action}s, and it stores the intermediary {@link State}s.
23  *
24  * @param <S>
25  *            The type of the state
26  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
27  */
28 public class Reaction<S extends State> {
29
30         /** The query to run. */
31         private final Query<S> query;
32
33         /** The trigger to detect changes. */
34         private final Trigger<S> trigger;
35
36         /** The action to perform. */
37         private final Action<S> action;
38
39         /** The current state of the query. */
40         private S currentState;
41
42         /** The previous state of the query. */
43         private S previousState;
44
45         /**
46          * Creates a new reaction.
47          *
48          * @param query
49          *            The query to run
50          * @param trigger
51          *            The trigger to detect changes
52          * @param action
53          *            The action to perform
54          */
55         public Reaction(Query<S> query, Trigger<S> trigger, Action<S> action) {
56                 this.query = query;
57                 this.trigger = trigger;
58                 this.action = action;
59         }
60
61         //
62         // ACCESSORS
63         //
64
65         /**
66          * Returns the query to run.
67          *
68          * @return The query to run
69          */
70         public Query<S> query() {
71                 return query;
72         }
73
74         /**
75          * Returns the trigger to detect changes.
76          *
77          * @return The trigger to detect changes
78          */
79         public Trigger<S> trigger() {
80                 return trigger;
81         }
82
83         /**
84          * Returns the action to perform.
85          *
86          * @return The action to perform
87          */
88         public Action<S> action() {
89                 return action;
90         }
91
92         /**
93          * Returns the current state of the query.
94          *
95          * @return The current state of the query
96          */
97         public S currentState() {
98                 return currentState;
99         }
100
101         /**
102          * Returns the previous state of the query.
103          *
104          * @return The previous state of the query
105          */
106         public S previousState() {
107                 return previousState;
108         }
109
110 }