1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sourceforge.jhunters.pool.data;
18
19 import java.time.LocalDate;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Comparator;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Map;
27
28 import javafx.collections.FXCollections;
29 import javafx.collections.ObservableList;
30 import net.sourceforge.jhunters.pool.data.CompetitionFixtures.FixtureComparator;
31 import net.sourceforge.jhunters.pool.data.CompetitionFixtures.FixtureRecord;
32 import net.sourceforge.jhunters.pool.data.Player.PlayerComparator;
33
34
35
36
37 public class CompetitionMatrix {
38
39
40
41 private final ObservableList<CompetitionFixtures> theFixtures;
42
43
44
45
46 private final Map<Integer, CompetitionFixtures> theFixtureMap;
47
48
49
50
51 private final Comparator<CompetitionFixtures> theComparator;
52
53
54
55
56 protected CompetitionMatrix() {
57 theFixtures = FXCollections.observableArrayList();
58 theFixtureMap = new HashMap<Integer, CompetitionFixtures>();
59 theComparator = new FixtureComparator();
60 }
61
62
63
64
65
66 protected void setPlayers(final List<Player> pPlayers) {
67
68 List<Player> myList = new ArrayList<Player>(pPlayers);
69 Collections.sort(myList, new PlayerComparator());
70
71
72 int i = 0;
73 int mySize = pPlayers.size();
74
75
76 Iterator<Player> myIterator = myList.iterator();
77 while (myIterator.hasNext()) {
78 Player myPlayer = myIterator.next();
79
80
81 CompetitionFixtures myFixtures = new CompetitionFixtures(myPlayer, mySize);
82 myFixtures.setIndex(i++);
83 theFixtures.add(myFixtures);
84 theFixtureMap.put(myPlayer.getId(), myFixtures);
85 }
86 }
87
88
89
90
91 protected void adjustPlayers() {
92
93 RemapMatrix myMapping = new RemapMatrix();
94
95
96 if (myMapping.isReMapRequired()) {
97
98 myMapping.adjustFixtures();
99
100
101 myMapping.setNewIndices();
102 }
103 }
104
105
106
107
108
109 public ObservableList<CompetitionFixtures> getMatrix() {
110 return theFixtures;
111 }
112
113
114
115
116
117
118 private CompetitionFixtures getFixtures(final Player pPlayer) {
119 return theFixtureMap.get(pPlayer.getId());
120 }
121
122
123
124
125
126 protected void processFixtures(final FixtureList pFixtures) {
127
128 resetOutlook();
129
130
131 Iterator<FixtureSet> myIterator = pFixtures.fixtureIterator();
132 while (myIterator.hasNext()) {
133 FixtureSet mySet = myIterator.next();
134
135
136 processFixtureSet(mySet);
137 }
138
139
140 determineOutlook();
141 }
142
143
144
145
146
147 private void processFixtureSet(final FixtureSet pSet) {
148
149 Iterator<Fixture> myIterator = pSet.fixtureIterator();
150 while (myIterator.hasNext()) {
151 Fixture myFixture = myIterator.next();
152
153
154 if (myFixture.isBye()) {
155 continue;
156 }
157
158
159 processFixture(myFixture);
160 }
161 }
162
163
164
165
166
167 protected void processFixture(final Fixture pFixture) {
168
169 CompetitionFixtures myHome = getFixtures(pFixture.getHomePlayer());
170 CompetitionFixtures myAway = getFixtures(pFixture.getAwayPlayer());
171
172
173 Result myResult = pFixture.getResult();
174 if (myResult != null) {
175
176 myHome.setResult(myResult, myAway);
177 myAway.setResult(myResult.reverse(), myHome);
178
179
180 } else {
181 LocalDate myDate = pFixture.getPlannedDate();
182
183
184 myHome.setOutlook(myDate, myAway);
185 myAway.setOutlook(myDate, myHome);
186 }
187 }
188
189
190
191
192 private void resetOutlook() {
193
194 Iterator<CompetitionFixtures> myIterator = theFixtures.iterator();
195 while (myIterator.hasNext()) {
196 CompetitionFixtures myFixtures = myIterator.next();
197
198
199 myFixtures.resetOutlook();
200 }
201 }
202
203
204
205
206 private void determineOutlook() {
207
208 Iterator<CompetitionFixtures> myIterator = theFixtures.iterator();
209 while (myIterator.hasNext()) {
210 CompetitionFixtures myFixtures = myIterator.next();
211
212
213 myFixtures.determineOutlook();
214 }
215 }
216
217
218
219
220 private final class RemapMatrix {
221
222
223
224 private final List<Integer[]> theMap;
225
226
227
228
229 private RemapMatrix() {
230
231 theMap = new ArrayList<Integer[]>();
232
233
234 FXCollections.sort(theFixtures, theComparator);
235
236
237 int myIndex = 0;
238 Iterator<CompetitionFixtures> myIterator = theFixtures.iterator();
239 while (myIterator.hasNext()) {
240 CompetitionFixtures myFixtures = myIterator.next();
241
242
243 int myCurIndex = myFixtures.getIndex();
244 if (myIndex != myCurIndex) {
245
246 Integer[] myMap = new Integer[]
247 { myCurIndex, myIndex };
248 theMap.add(myMap);
249 }
250
251
252 myIndex++;
253 }
254 }
255
256
257
258
259
260 private boolean isReMapRequired() {
261 return !theMap.isEmpty();
262 }
263
264
265
266
267 private void adjustFixtures() {
268
269 Iterator<CompetitionFixtures> myIterator = theFixtures.iterator();
270 while (myIterator.hasNext()) {
271 CompetitionFixtures myFixtures = myIterator.next();
272
273
274 adjustFixtures(myFixtures);
275 }
276 }
277
278
279
280
281
282 private void adjustFixtures(final CompetitionFixtures pFixtures) {
283
284 FixtureRecord[] myCache = new FixtureRecord[theMap.size()];
285
286
287 int i = 0;
288 Iterator<Integer[]> myIterator = theMap.iterator();
289 while (myIterator.hasNext()) {
290 Integer[] myMapping = myIterator.next();
291
292
293 myCache[i++] = pFixtures.fixtureProperty(myMapping[0]).getValue();
294 }
295
296
297 i = 0;
298 myIterator = theMap.iterator();
299 while (myIterator.hasNext()) {
300 Integer[] myMapping = myIterator.next();
301
302
303 pFixtures.fixtureProperty(myMapping[1]).setValue(myCache[i++]);
304 }
305 }
306
307
308
309
310 private void setNewIndices() {
311
312 int myIndex = 0;
313 Iterator<CompetitionFixtures> myIterator = theFixtures.iterator();
314 while (myIterator.hasNext()) {
315 CompetitionFixtures myFixtures = myIterator.next();
316
317
318 myFixtures.setIndex(myIndex++);
319 }
320 }
321 }
322 }