Chain defintion model.
[rhynodge.git] / src / main / java / net / pterodactylus / reactor / loader / Chain.java
1 /*
2  * Reactor - Chain.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.loader;
19
20 import java.util.List;
21
22 import org.simpleframework.xml.Element;
23 import org.simpleframework.xml.ElementList;
24 import org.simpleframework.xml.Root;
25
26 /**
27  * Model for chain definitions.
28  *
29  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30  */
31 @Root
32 public class Chain {
33
34         /**
35          * Parameter model.
36          *
37          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38          */
39         public static class Parameter {
40
41                 /** The name of the parameter. */
42                 @Element
43                 private String name;
44
45                 /** The value of the parameter. */
46                 @Element
47                 private String value;
48
49                 /**
50                  * Returns the name of the parameter.
51                  *
52                  * @return The name of the parameter
53                  */
54                 public String name() {
55                         return name;
56                 }
57
58                 /**
59                  * Returns the value of the parameter.
60                  *
61                  * @return The value of the parameter
62                  */
63                 public String value() {
64                         return value;
65                 }
66
67                 /**
68                  * {@inheritDoc}
69                  */
70                 @Override
71                 public int hashCode() {
72                         int hashCode = 0;
73                         hashCode ^= name.hashCode();
74                         hashCode ^= value.hashCode();
75                         return hashCode;
76                 }
77
78                 /**
79                  * {@inheritDoc}
80                  */
81                 @Override
82                 public boolean equals(Object object) {
83                         if (!(object instanceof Parameter)) {
84                                 return false;
85                         }
86                         Parameter parameter = (Parameter) object;
87                         if (!name.equals(parameter.name)) {
88                                 return false;
89                         }
90                         if (!value.equals(parameter.value)) {
91                                 return false;
92                         }
93                         return true;
94                 }
95
96         }
97
98         /**
99          * Defines a part of a chain.
100          *
101          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
102          */
103         public static class Part {
104
105                 /** The class name of the part. */
106                 @Element(name = "class")
107                 private String name;
108
109                 /** The parameters of the part. */
110                 @ElementList(required = false, empty = false)
111                 private List<Parameter> parameters;
112
113                 /**
114                  * Returns the name of the part’s class.
115                  *
116                  * @return The name of the part’s class
117                  */
118                 public String name() {
119                         return name;
120                 }
121
122                 /**
123                  * Returns the parameters of the part.
124                  *
125                  * @return The parameters of the part
126                  */
127                 public List<Parameter> parameters() {
128                         return parameters;
129                 }
130
131                 /**
132                  * {@inheritDoc}
133                  */
134                 @Override
135                 public int hashCode() {
136                         int hashCode = 0;
137                         hashCode ^= name.hashCode();
138                         for (Parameter parameter : parameters) {
139                                 hashCode ^= parameter.hashCode();
140                         }
141                         return hashCode;
142                 }
143
144                 /**
145                  * {@inheritDoc}
146                  */
147                 @Override
148                 public boolean equals(Object object) {
149                         if (!(object instanceof Part)) {
150                                 return false;
151                         }
152                         Part part = (Part) object;
153                         if (!name.equals(part.name)) {
154                                 return false;
155                         }
156                         if (parameters.size() != part.parameters.size()) {
157                                 return false;
158                         }
159                         for (int parameterIndex = 0; parameterIndex < parameters.size(); ++parameterIndex) {
160                                 if (!parameters.get(parameterIndex).equals(part.parameters.get(parameterIndex))) {
161                                         return false;
162                                 }
163                         }
164                         return true;
165                 }
166
167         }
168
169         /** Whether this chain is enabled. */
170         @Element
171         private boolean enabled;
172
173         /** The query of the chain. */
174         @Element
175         private Part query;
176
177         /** The filters of the chain. */
178         @ElementList(required = false, empty = false)
179         private List<Part> filters;
180
181         /** The trigger of the chain. */
182         @Element
183         private Part trigger;
184
185         /** The action of the chain. */
186         @Element
187         private Part action;
188
189         /** Interval between updates (in seconds). */
190         @Element
191         private int updateInterval;
192
193         /**
194          * Returns whether this chain is enabled.
195          *
196          * @return {@code true} if this chain is enabled, {@code false} otherwise
197          */
198         public boolean enabled() {
199                 return enabled;
200         }
201
202         /**
203          * Returns the query of this chain.
204          *
205          * @return The query of this chain
206          */
207         public Part query() {
208                 return query;
209         }
210
211         /**
212          * Returns the filters of this chain.
213          *
214          * @return The filters of this chain
215          */
216         public List<Part> filters() {
217                 return filters;
218         }
219
220         /**
221          * Returns the trigger of this chain.
222          *
223          * @return The trigger of this chain
224          */
225         public Part trigger() {
226                 return trigger;
227         }
228
229         /**
230          * Returns the action of this chain.
231          *
232          * @return The action of this chain
233          */
234         public Part action() {
235                 return action;
236         }
237
238         /**
239          * Returns the update interval of the chain.
240          *
241          * @return The update interval (in seconds)
242          */
243         public int updateInterval() {
244                 return updateInterval;
245         }
246
247         //
248         // OBJECT METHODS
249         //
250
251         /**
252          * {@inheritDoc}
253          */
254         @Override
255         public int hashCode() {
256                 int hashCode = 0;
257                 hashCode ^= query.hashCode();
258                 for (Part filter : filters) {
259                         hashCode ^= filter.hashCode();
260                 }
261                 hashCode ^= trigger.hashCode();
262                 hashCode ^= action.hashCode();
263                 hashCode ^= updateInterval;
264                 return hashCode;
265         }
266
267         /**
268          * {@inheritDoc}
269          */
270         @Override
271         public boolean equals(Object object) {
272                 if (!(object instanceof Chain)) {
273                         return false;
274                 }
275                 Chain chain = (Chain) object;
276                 if (!query.equals(chain.query)) {
277                         return false;
278                 }
279                 if (filters.size() != chain.filters.size()) {
280                         return false;
281                 }
282                 for (int filterIndex = 0; filterIndex < filters.size(); ++filterIndex) {
283                         if (!filters.get(filterIndex).equals(chain.filters.get(filterIndex))) {
284                                 return false;
285                         }
286                 }
287                 if (!trigger.equals(chain.trigger)) {
288                         return false;
289                 }
290                 if (!action.equals(chain.action)) {
291                         return false;
292                 }
293                 if (updateInterval != chain.updateInterval) {
294                         return false;
295                 }
296                 return true;
297         }
298
299 }