59fad8fe8d91b2488685af554fa435eadc2c398b
[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 import java.util.Collections;
21 import java.util.List;
22
23 import com.google.common.collect.Lists;
24
25 /**
26  * A {@code Reaction} binds together {@link Query}s, {@link Trigger}s, and
27  * {@link Action}s, and it stores the intermediary {@link State}s.
28  *
29  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30  */
31 public class Reaction {
32
33         /** The query to run. */
34         private final Query query;
35
36         /** The filters to run. */
37         private final List<Filter> filters = Lists.newArrayList();
38
39         /** The trigger to detect changes. */
40         private final Trigger trigger;
41
42         /** The action to perform. */
43         private final Action action;
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 query, Trigger trigger, Action action) {
59                 this(query, Collections.<Filter> emptyList(), trigger, action);
60         }
61
62         /**
63          * Creates a new reaction.
64          *
65          * @param query
66          *            The query to run
67          * @param filters
68          *            The filters to run
69          * @param trigger
70          *            The trigger to detect changes
71          * @param action
72          *            The action to perform
73          */
74         public Reaction(Query query, List<Filter> filters, Trigger trigger, Action action) {
75                 this.query = query;
76                 this.filters.addAll(filters);
77                 this.trigger = trigger;
78                 this.action = action;
79         }
80
81         //
82         // ACCESSORS
83         //
84
85         /**
86          * Returns the query to run.
87          *
88          * @return The query to run
89          */
90         public Query query() {
91                 return query;
92         }
93
94         /**
95          * Returns the filters to run.
96          *
97          * @return The filters to run
98          */
99         public Iterable<Filter> filters() {
100                 return filters;
101         }
102
103         /**
104          * Returns the trigger to detect changes.
105          *
106          * @return The trigger to detect changes
107          */
108         public Trigger trigger() {
109                 return trigger;
110         }
111
112         /**
113          * Returns the action to perform.
114          *
115          * @return The action to perform
116          */
117         public Action action() {
118                 return action;
119         }
120
121         /**
122          * Returns the update interval of this reaction.
123          *
124          * @return The update interval of this reaction (in milliseconds)
125          */
126         public long updateInterval() {
127                 return updateInterval;
128         }
129
130         /**
131          * Sets the update interval of this reaction.
132          *
133          * @param updateInterval
134          *            The update interval of this reaction (in milliseconds)
135          * @return This reaction
136          */
137         public Reaction setUpdateInterval(long updateInterval) {
138                 this.updateInterval = updateInterval;
139                 return this;
140         }
141
142 }