🔀 Merge branch 'website/epic-games' into next
[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                         return value.equals(parameter.value);
89                 }
90
91         }
92
93         /**
94          * Defines a part of a chain.
95          *
96          * @author <a href="mailto:bombe@pterodactylus.net">David â€˜Bombe’ Roden</a>
97          */
98         public static class Part {
99
100                 /** The class name of the part. */
101                 @JsonProperty(value = "class")
102                 private String name;
103
104                 /** The parameters of the part. */
105                 @JsonProperty
106                 private List<Parameter> parameters = new ArrayList<Parameter>();
107
108                 /**
109                  * Returns the name of the part’s class.
110                  *
111                  * @return The name of the part’s class
112                  */
113                 public String name() {
114                         return name;
115                 }
116
117                 /**
118                  * Returns the parameters of the part.
119                  *
120                  * @return The parameters of the part
121                  */
122                 public List<Parameter> parameters() {
123                         return parameters;
124                 }
125
126                 /**
127                  * {@inheritDoc}
128                  */
129                 @Override
130                 public int hashCode() {
131                         int hashCode = 0;
132                         hashCode ^= name.hashCode();
133                         for (Parameter parameter : parameters) {
134                                 hashCode ^= parameter.hashCode();
135                         }
136                         return hashCode;
137                 }
138
139                 /**
140                  * {@inheritDoc}
141                  */
142                 @Override
143                 public boolean equals(Object object) {
144                         if (!(object instanceof Part)) {
145                                 return false;
146                         }
147                         Part part = (Part) object;
148                         if (!name.equals(part.name)) {
149                                 return false;
150                         }
151                         if (parameters.size() != part.parameters.size()) {
152                                 return false;
153                         }
154                         for (int parameterIndex = 0; parameterIndex < parameters.size(); ++parameterIndex) {
155                                 if (!parameters.get(parameterIndex).equals(part.parameters.get(parameterIndex))) {
156                                         return false;
157                                 }
158                         }
159                         return true;
160                 }
161
162         }
163
164         /** Whether this chain is enabled. */
165         @JsonProperty
166         private boolean enabled;
167
168         /** The name of the chain. */
169         @JsonProperty
170         private String name;
171
172         /** The query of the chain. */
173         @JsonProperty
174         private Part query;
175
176         /** The filters of the chain. */
177         @JsonProperty
178         private List<Part> filters = new ArrayList<Part>();
179
180         /** The merger of the chain. */
181         @JsonProperty
182         private Part merger;
183
184         /** A combination of query, filters, and a merger. */
185         @JsonProperty
186         private Part watcher;
187
188         /** The action of the chain. */
189         @JsonProperty
190         private Part action;
191
192         /** Interval between updates (in seconds). */
193         @JsonProperty
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 name of the chain.
207          *
208          * @return The name of the chain
209          */
210         public String name() {
211                 return name;
212         }
213
214         /**
215          * Returns the query of this chain.
216          *
217          * @return The query of this chain
218          */
219         public Part query() {
220                 return query;
221         }
222
223         /**
224          * Returns the filters of this chain.
225          *
226          * @return The filters of this chain
227          */
228         public List<Part> filters() {
229                 return filters;
230         }
231
232         /**
233          * Returns the merger of this chain.
234          *
235          * @return The merger of this chain
236          */
237         public Part merger() {
238                 return merger;
239         }
240
241         /**
242          * Returns an optional watcher.
243          *
244          * @return The watcher of this chain
245          */
246         public Part watcher() {
247                 return watcher;
248         }
249
250         /**
251          * Returns the action of this chain.
252          *
253          * @return The action of this chain
254          */
255         public Part action() {
256                 return action;
257         }
258
259         /**
260          * Returns the update interval of the chain.
261          *
262          * @return The update interval (in seconds)
263          */
264         public int updateInterval() {
265                 return updateInterval;
266         }
267
268         //
269         // OBJECT METHODS
270         //
271
272         /**
273          * {@inheritDoc}
274          */
275         @Override
276         public int hashCode() {
277                 int hashCode = 0;
278                 hashCode ^= name.hashCode();
279                 if (watcher != null) {
280                         hashCode ^= watcher.hashCode();
281                 } else {
282                         hashCode ^= query.hashCode();
283                         for (Part filter : filters) {
284                                 hashCode ^= filter.hashCode();
285                         }
286                         hashCode ^= merger.hashCode();
287                 }
288                 hashCode ^= action.hashCode();
289                 hashCode ^= updateInterval;
290                 return hashCode;
291         }
292
293         /**
294          * {@inheritDoc}
295          */
296         @Override
297         public boolean equals(Object object) {
298                 if (!(object instanceof Chain)) {
299                         return false;
300                 }
301                 Chain chain = (Chain) object;
302                 if (!name.equals(chain.name)) {
303                         return false;
304                 }
305                 if (watcher != null) {
306                         if (!watcher.equals(chain.watcher)) {
307                                 return false;
308                         }
309                 } else {
310                         if (!query.equals(chain.query)) {
311                                 return false;
312                         }
313                         if (filters.size() != chain.filters.size()) {
314                                 return false;
315                         }
316                         for (int filterIndex = 0; filterIndex < filters.size(); ++filterIndex) {
317                                 if (!filters.get(filterIndex).equals(chain.filters.get(filterIndex))) {
318                                         return false;
319                                 }
320                         }
321                         if (!merger.equals(chain.merger)) {
322                                 return false;
323                         }
324                 }
325                 if (!action.equals(chain.action)) {
326                         return false;
327                 }
328                 return updateInterval == chain.updateInterval;
329         }
330
331 }