Rename project to “Rhynodge.”
[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         /** The action of the chain. */
188         @JsonProperty
189         private Part action;
190
191         /** Interval between updates (in seconds). */
192         @JsonProperty
193         private int updateInterval;
194
195         /**
196          * Returns whether this chain is enabled.
197          *
198          * @return {@code true} if this chain is enabled, {@code false} otherwise
199          */
200         public boolean enabled() {
201                 return enabled;
202         }
203
204         /**
205          * Returns the name of the chain.
206          *
207          * @return The name of the chain
208          */
209         public String name() {
210                 return name;
211         }
212
213         /**
214          * Returns the query of this chain.
215          *
216          * @return The query of this chain
217          */
218         public Part query() {
219                 return query;
220         }
221
222         /**
223          * Returns the filters of this chain.
224          *
225          * @return The filters of this chain
226          */
227         public List<Part> filters() {
228                 return filters;
229         }
230
231         /**
232          * Returns the trigger of this chain.
233          *
234          * @return The trigger of this chain
235          */
236         public Part trigger() {
237                 return trigger;
238         }
239
240         /**
241          * Returns the action of this chain.
242          *
243          * @return The action of this chain
244          */
245         public Part action() {
246                 return action;
247         }
248
249         /**
250          * Returns the update interval of the chain.
251          *
252          * @return The update interval (in seconds)
253          */
254         public int updateInterval() {
255                 return updateInterval;
256         }
257
258         //
259         // OBJECT METHODS
260         //
261
262         /**
263          * {@inheritDoc}
264          */
265         @Override
266         public int hashCode() {
267                 int hashCode = 0;
268                 hashCode ^= name.hashCode();
269                 hashCode ^= query.hashCode();
270                 for (Part filter : filters) {
271                         hashCode ^= filter.hashCode();
272                 }
273                 hashCode ^= trigger.hashCode();
274                 hashCode ^= action.hashCode();
275                 hashCode ^= updateInterval;
276                 return hashCode;
277         }
278
279         /**
280          * {@inheritDoc}
281          */
282         @Override
283         public boolean equals(Object object) {
284                 if (!(object instanceof Chain)) {
285                         return false;
286                 }
287                 Chain chain = (Chain) object;
288                 if (!name.equals(chain.name)) {
289                         return false;
290                 }
291                 if (!query.equals(chain.query)) {
292                         return false;
293                 }
294                 if (filters.size() != chain.filters.size()) {
295                         return false;
296                 }
297                 for (int filterIndex = 0; filterIndex < filters.size(); ++filterIndex) {
298                         if (!filters.get(filterIndex).equals(chain.filters.get(filterIndex))) {
299                                 return false;
300                         }
301                 }
302                 if (!trigger.equals(chain.trigger)) {
303                         return false;
304                 }
305                 if (!action.equals(chain.action)) {
306                         return false;
307                 }
308                 if (updateInterval != chain.updateInterval) {
309                         return false;
310                 }
311                 return true;
312         }
313
314 }