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