Use JSON for configuration files, remove all references to XML.
[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 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 query of the chain. */
172         @JsonProperty
173         private Part query;
174
175         /** The filters of the chain. */
176         @JsonProperty
177         private List<Part> filters = new ArrayList<Part>();
178
179         /** The trigger of the chain. */
180         @JsonProperty
181         private Part trigger;
182
183         /** The action of the chain. */
184         @JsonProperty
185         private Part action;
186
187         /** Interval between updates (in seconds). */
188         @JsonProperty
189         private int updateInterval;
190
191         /**
192          * Returns whether this chain is enabled.
193          *
194          * @return {@code true} if this chain is enabled, {@code false} otherwise
195          */
196         public boolean enabled() {
197                 return enabled;
198         }
199
200         /**
201          * Returns the query of this chain.
202          *
203          * @return The query of this chain
204          */
205         public Part query() {
206                 return query;
207         }
208
209         /**
210          * Returns the filters of this chain.
211          *
212          * @return The filters of this chain
213          */
214         public List<Part> filters() {
215                 return filters;
216         }
217
218         /**
219          * Returns the trigger of this chain.
220          *
221          * @return The trigger of this chain
222          */
223         public Part trigger() {
224                 return trigger;
225         }
226
227         /**
228          * Returns the action of this chain.
229          *
230          * @return The action of this chain
231          */
232         public Part action() {
233                 return action;
234         }
235
236         /**
237          * Returns the update interval of the chain.
238          *
239          * @return The update interval (in seconds)
240          */
241         public int updateInterval() {
242                 return updateInterval;
243         }
244
245         //
246         // OBJECT METHODS
247         //
248
249         /**
250          * {@inheritDoc}
251          */
252         @Override
253         public int hashCode() {
254                 int hashCode = 0;
255                 hashCode ^= query.hashCode();
256                 for (Part filter : filters) {
257                         hashCode ^= filter.hashCode();
258                 }
259                 hashCode ^= trigger.hashCode();
260                 hashCode ^= action.hashCode();
261                 hashCode ^= updateInterval;
262                 return hashCode;
263         }
264
265         /**
266          * {@inheritDoc}
267          */
268         @Override
269         public boolean equals(Object object) {
270                 if (!(object instanceof Chain)) {
271                         return false;
272                 }
273                 Chain chain = (Chain) object;
274                 if (!query.equals(chain.query)) {
275                         return false;
276                 }
277                 if (filters.size() != chain.filters.size()) {
278                         return false;
279                 }
280                 for (int filterIndex = 0; filterIndex < filters.size(); ++filterIndex) {
281                         if (!filters.get(filterIndex).equals(chain.filters.get(filterIndex))) {
282                                 return false;
283                         }
284                 }
285                 if (!trigger.equals(chain.trigger)) {
286                         return false;
287                 }
288                 if (!action.equals(chain.action)) {
289                         return false;
290                 }
291                 if (updateInterval != chain.updateInterval) {
292                         return false;
293                 }
294                 return true;
295         }
296
297 }