Add an update interval to a reaction.
[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         /** The interval in which to run queries (in milliseconds). */
46         private long updateInterval;
47
48         /**
49          * Creates a new reaction.
50          *
51          * @param query
52          *            The query to run
53          * @param trigger
54          *            The trigger to detect changes
55          * @param action
56          *            The action to perform
57          */
58         public Reaction(Query<S> query, Trigger<S> trigger, Action<S> action) {
59                 this.query = query;
60                 this.trigger = trigger;
61                 this.action = action;
62         }
63
64         //
65         // ACCESSORS
66         //
67
68         /**
69          * Returns the query to run.
70          *
71          * @return The query to run
72          */
73         public Query<S> query() {
74                 return query;
75         }
76
77         /**
78          * Returns the trigger to detect changes.
79          *
80          * @return The trigger to detect changes
81          */
82         public Trigger<S> trigger() {
83                 return trigger;
84         }
85
86         /**
87          * Returns the action to perform.
88          *
89          * @return The action to perform
90          */
91         public Action<S> action() {
92                 return action;
93         }
94
95         /**
96          * Returns the current state of the query.
97          *
98          * @return The current state of the query
99          */
100         public S currentState() {
101                 return currentState;
102         }
103
104         /**
105          * Returns the previous state of the query.
106          *
107          * @return The previous state of the query
108          */
109         public S previousState() {
110                 return previousState;
111         }
112
113         /**
114          * Returns the update interval of this reaction.
115          *
116          * @return The update interval of this reaction (in milliseconds)
117          */
118         public long updateInterval() {
119                 return updateInterval;
120         }
121
122         /**
123          * Sets the update interval of this reaction.
124          *
125          * @param updateInterval
126          *            The update interval of this reaction (in milliseconds)
127          * @return This reaction
128          */
129         public Reaction<?> setUpdateInterval(long updateInterval) {
130                 this.updateInterval = updateInterval;
131                 return this;
132         }
133
134 }