1 /*
2 * ============================================================================
3 * Project betoffice-storage
4 * Copyright (c) 2000-2017 by Andre Winkler. All rights reserved.
5 * ============================================================================
6 * GNU GENERAL PUBLIC LICENSE
7 * TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 */
24
25 package de.winkler.betoffice.storage;
26
27 import org.apache.commons.lang.Validate;
28
29 /**
30 * TeamResult verwaltet die errechneten Daten einer Mannschaft für eine Saison
31 * und Gruppe. Zwei TeamResults, die im Sinne von <code>compareTo()</code>
32 * gleich sind, sind ebenfalls im Sinne der Methode <code>equals()</code>
33 * gleich. Das Attribut tabPos wird berechnet und wird für die Auswertung von
34 * <code>equals()</code> und <code>hashCode()</code> nicht verwendet.
35 *
36 * @author Andre Winkler
37 */
38 public class TeamResult implements Comparable<TeamResult> {
39
40 /** serial version id */
41 private static final long serialVersionUID = -6690928605407554417L;
42
43 private final Season season;
44
45 private final GroupType groupType;
46
47 private final Team team;
48
49 private int posGoals;
50
51 private int negGoals;
52
53 private int win;
54
55 private int lost;
56
57 private int remis;
58
59 private int tablePosition;
60
61 public TeamResult(Season _season, GroupType _groupType, Team _team) {
62 season = _season;
63 groupType = _groupType;
64 team = _team;
65 }
66
67 public Season getSeason() {
68 return season;
69 }
70
71 public GroupType getGroupType() {
72 return groupType;
73 }
74
75 public Team getTeam() {
76 return team;
77 }
78
79 /**
80 * @return the posGoals
81 */
82 public int getPosGoals() {
83 return posGoals;
84 }
85
86 /**
87 * @param posGoals
88 * the posGoals to set
89 */
90 public void setPosGoals(int posGoals) {
91 this.posGoals = posGoals;
92 }
93
94 /**
95 * @return the negGoals
96 */
97 public int getNegGoals() {
98 return negGoals;
99 }
100
101 /**
102 * @param negGoals
103 * the negGoals to set
104 */
105 public void setNegGoals(int negGoals) {
106 this.negGoals = negGoals;
107 }
108
109 /**
110 * @return Berechnet die Tordifferenz.
111 */
112 private int getGoalDiff() {
113 return posGoals - negGoals;
114 }
115
116 /**
117 * @return the win
118 */
119 public int getWin() {
120 return win;
121 }
122
123 /**
124 * @param win
125 * the win to set
126 */
127 public void setWin(int win) {
128 this.win = win;
129 }
130
131 /**
132 * @return the lost
133 */
134 public int getLost() {
135 return lost;
136 }
137
138 /**
139 * @param lost
140 * the lost to set
141 */
142 public void setLost(int lost) {
143 this.lost = lost;
144 }
145
146 /**
147 * @return the remis
148 */
149 public int getRemis() {
150 return remis;
151 }
152
153 /**
154 * @param remis
155 * the remis to set
156 */
157 public void setRemis(int remis) {
158 this.remis = remis;
159 }
160
161 public int getPoints() {
162 return getWin() * Game.WIN + getRemis() * Game.REMIS;
163 }
164
165 public int getGames() {
166 return getWin() + getRemis() + getLost();
167 }
168
169 public void setTabPos(final int _tablePosition) {
170 tablePosition = _tablePosition;
171 }
172
173 public int getTabPos() {
174 return tablePosition;
175 }
176
177 private String getGoalsToString() {
178 StringBuilder buf = new StringBuilder();
179 buf.append(getPosGoals());
180 buf.append(':');
181 buf.append(getNegGoals());
182 return buf.toString();
183 }
184
185 //
186 // Comparator Methoden
187 //
188
189 /**
190 * Vergleicht zwei Objekte team1 und team2 auf deren Rangfolge. Rückgabe von
191 * -1, wenn team1 schlechter als team2; +1, wenn team1 besser als team2; 0,
192 * wenn beide gleich stark.
193 *
194 * @param team1
195 * Ein TeamResult.
196 * @param team2
197 * Ein TeamResult.
198 * @return -1,0,+1
199 * @throws IllegalArgumentException
200 * Null Parameter übergeben.
201 */
202 public static int isBetterAs(TeamResult team1, TeamResult team2) {
203 Validate.notNull(team1, "team1 ist null");
204 Validate.notNull(team2, "team2 ist null");
205
206 //
207 // Einfacher Punktevergleich
208 //
209 if (team1.getPoints() > team2.getPoints()) {
210 // Team 1 hat mehr Punkte als Team 2.
211 return 1;
212 } else if (team1.getPoints() < team2.getPoints()) {
213 // Team 1 hat weniger Punkte als Team 1.
214 return -1;
215 }
216
217 //
218 // Punktevergleich führte zu Gleichstand, also nun Vergleich des
219 // Torverhältnisses.
220 //
221 if (team1.getGoalDiff() > team2.getGoalDiff()) {
222 // Team 1 hat eine besseres Torverhältnis als Team 2.
223 return 1;
224 } else if (team1.getGoalDiff() < team2.getGoalDiff()) {
225 // Team 1 hat ein schlechteres Torverhältnis als Team 1.
226 return -1;
227 }
228
229 //
230 // Vergleich der Tordifferenz führte zu Gleichstand, also nun Vergleich
231 // wer die meisten Tore geschossen hat.
232 //
233 if (team1.getPosGoals() > team2.getPosGoals()) {
234 // Team 1 hat mehr Tore geschossen als Team 1.
235 return 1;
236 } else if (team1.getPosGoals() < team2.getPosGoals()) {
237 // Team 1 hat weniger Tore geschossen als Team 2.
238 return -1;
239 }
240
241 //
242 // Beide Mannschaften gleich erfolgreich.
243 //
244 return 0;
245 }
246
247 //
248 // Methoden von Comparable
249 //
250
251 /**
252 * Vergleicht diesen TeamResult mit einem anderen.
253 *
254 * @param obj
255 * Ein TeamResult.
256 * @return Wenn '0': Objekte sind gleich.
257 */
258 public int compareTo(final TeamResult obj) {
259 return isBetterAs(obj, this);
260 }
261
262 //
263 // Methoden von Object
264 //
265
266 /**
267 * @see Object#toString()
268 */
269 @Override
270 public String toString() {
271 StringBuffer buf = new StringBuffer();
272 if (getTeam() == null) {
273 buf.append("<null>");
274 } else {
275 buf.append(getTeam().getName());
276 }
277 buf.append(" GroupType: ");
278 buf.append(getGroupType().getName());
279 buf.append(" Goals:");
280 buf.append(getGoalsToString());
281 buf.append(" Points:");
282 buf.append(getPoints());
283 return buf.toString();
284 }
285
286 /**
287 * @see Object#equals(java.lang.Object)
288 */
289 @Override
290 public boolean equals(Object object) {
291 if (object == null) {
292 return false;
293 } else if (!(object instanceof TeamResult)) {
294 return false;
295 } else {
296 TeamResult result = (TeamResult) object;
297 if ((result.getTeam().equals(getTeam()))
298 && (result.getSeason().equals(getSeason()))
299 && (result.getGroupType().equals(getGroupType()))
300 && (result.getPosGoals() == getPosGoals())
301 && (result.getNegGoals() == getNegGoals())
302 && (result.getWin() == getWin())
303 && (result.getRemis() == getRemis())
304 && (result.getLost() == getLost())) {
305 return true;
306 } else {
307 return false;
308 }
309 }
310 }
311
312 /**
313 * @see Object#hashCode()
314 */
315 @Override
316 public int hashCode() {
317 int result = 17;
318 result = 37 * result + getTeam().hashCode();
319 result = 37 * result + getSeason().hashCode();
320 result = 37 * result + getGroupType().hashCode();
321 result = 37 * result + posGoals;
322 result = 37 * result + negGoals;
323 result = 37 * result + win;
324 result = 37 * result + lost;
325 result = 37 * result + remis;
326 return result;
327 }
328
329 }