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