View Javadoc
1   /*******************************************************************************
2    * jhunters: Pool League
3    * Copyright 2015 Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   ********************************************************************************/
17  package net.sourceforge.jhunters.pool.data;
18  
19  import java.time.LocalDate;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.Map;
23  
24  import javafx.beans.Observable;
25  import javafx.collections.FXCollections;
26  import javafx.collections.ObservableList;
27  import net.sourceforge.jhunters.pool.PoolStatus;
28  
29  /**
30   * Competition List.
31   */
32  public class CompetitionList {
33      /**
34       * Object name.
35       */
36      protected static final String OBJECT_NAME = DataResource.COMPETITION_LIST.getValue();
37  
38      /**
39       * List of competitions.
40       */
41      private final ObservableList<Competition> theCompetitions;
42  
43      /**
44       * The status of the list.
45       */
46      private PoolStatus theStatus;
47  
48      /**
49       * Constructor.
50       */
51      public CompetitionList() {
52          /* Create the player list with an extractor to allow SortedList to work */
53          theCompetitions = FXCollections.observableArrayList(p -> new Observable[] { p.nameProperty() });
54      }
55  
56      /**
57       * Obtain competitions.
58       * @return the competitions
59       */
60      public ObservableList<Competition> getCompetitions() {
61          return theCompetitions;
62      }
63  
64      /**
65       * Obtain competition iterator.
66       * @return the iterator
67       */
68      public Iterator<Competition> competitionIterator() {
69          return theCompetitions.iterator();
70      }
71  
72      /**
73       * Obtain the status.
74       * @return the status.
75       */
76      public PoolStatus getStatus() {
77          return theStatus;
78      }
79  
80      /**
81       * Reset fixtures.
82       */
83      protected void reset() {
84          theCompetitions.clear();
85      }
86  
87      /**
88       * Add competition.
89       * @param pComp the competition.
90       */
91      protected void addCompetition(final Competition pComp) {
92          /* Add to the competition list */
93          theCompetitions.add(pComp);
94      }
95  
96      /**
97       * Set competitions.
98       * @param pSource the source competitions.
99       */
100     protected void setCompetitions(final Iterator<Competition> pSource) {
101         /* Reset the fixtures */
102         reset();
103 
104         /* Loop through the source list */
105         while (pSource.hasNext()) {
106             Competition myComp = pSource.next();
107 
108             /* Add the competition */
109             addCompetition(myComp);
110         }
111     }
112 
113     /**
114      * Get named competitions.
115      * @param pName the name of the competition.
116      * @return the competition
117      */
118     public Competition getCompetition(final String pName) {
119         /* Loop through the competitions */
120         Iterator<Competition> myIterator = theCompetitions.iterator();
121         while (myIterator.hasNext()) {
122             Competition myComp = myIterator.next();
123 
124             /* If this is the competition */
125             if (pName.equals(myComp.getName())) {
126                 return myComp;
127             }
128         }
129 
130         /* Not found */
131         return null;
132     }
133 
134     /**
135      * Create unique player.
136      * @return the unique player
137      */
138     private String createUniqueName() {
139         /* Create the builder */
140         StringBuilder myBuilder = new StringBuilder();
141         myBuilder.append("Competition");
142         int myLen = myBuilder.length();
143 
144         /* Loop through the list */
145         int myIndex = 0;
146         Competition myComp;
147         String myName;
148 
149         /* Loop to find a unique name */
150         do {
151             /* Reset the buffer and adjust index */
152             myBuilder.setLength(myLen);
153             myIndex++;
154 
155             /* Create name */
156             myBuilder.append(myIndex);
157             myName = myBuilder.toString();
158             myComp = getCompetition(myName);
159 
160             /* ReLoop if already exists */
161         } while (myComp != null);
162 
163         /* Return the name */
164         return myName;
165     }
166 
167     /**
168      * Create competition.
169      * @param pPlayers the players
170      * @return the competition
171      */
172     public Competition createCompetition(final PlayerList pPlayers) {
173         /* Create unique name */
174         String myName = createUniqueName();
175 
176         /* Create start date a week from now */
177         LocalDate myDate = LocalDate.now().plusWeeks(1);
178 
179         /* Create the fixture generator */
180         FixtureGenerator myGenerator = new FixtureGenerator();
181         myGenerator.setPlayers(pPlayers);
182 
183         /* Generate the fixture list */
184         FixtureList myFixtures = myGenerator.createFixtures(myDate);
185 
186         /* Create the competition */
187         Competition myComp = new Competition();
188         myComp.setName(myName);
189         myComp.setParticipants(pPlayers.playerIterator());
190         myComp.setFixtures(myFixtures.fixtureIterator());
191         myComp.setStartDate(myDate);
192 
193         /* Add competition to the list */
194         addCompetition(myComp);
195 
196         /* return the competition */
197         return myComp;
198     }
199 
200     /**
201      * Remove a competition from the list.
202      * @param pCompetition the competition.
203      */
204     public void removeCompetition(final Competition pCompetition) {
205         theCompetitions.remove(pCompetition);
206     }
207 
208     /**
209      * adjust the players.
210      */
211     protected void adjustPlayers() {
212         /* Loop through the competitions */
213         Iterator<Competition> myIterator = competitionIterator();
214         while (myIterator.hasNext()) {
215             Competition myComp = myIterator.next();
216 
217             /* Adjust players for the competition */
218             myComp.adjustPlayers();
219         }
220     }
221 
222     @Override
223     public boolean equals(final Object pThat) {
224         /* Handle trivial cases */
225         if (this == pThat) {
226             return true;
227         }
228         if (pThat == null) {
229             return false;
230         }
231 
232         /* Handle wrong class */
233         if (!(pThat instanceof CompetitionList)) {
234             return false;
235         }
236 
237         /* Access as competition list */
238         CompetitionList myThat = (CompetitionList) pThat;
239 
240         /* Test only on competition list */
241         return theCompetitions.equals(myThat.getCompetitions());
242     }
243 
244     @Override
245     public int hashCode() {
246         return theCompetitions.hashCode();
247     }
248 
249     /**
250      * Validate competition list.
251      * @return the status
252      */
253     public PoolStatus validate() {
254         /* Create name map */
255         Map<String, PoolStatus> myMap = new HashMap<String, PoolStatus>();
256         theStatus = PoolStatus.VALID;
257 
258         /* Loop through the competitions */
259         Iterator<Competition> myIterator = competitionIterator();
260         while (myIterator.hasNext()) {
261             Competition myComp = myIterator.next();
262 
263             /* Validate the competition */
264             PoolStatus myStatus = myComp.validate();
265             if (!myStatus.isValid()) {
266                 theStatus = PoolStatus.INVALID;
267             } else {
268                 /* Check for preExistence */
269                 String myName = myComp.getName();
270                 PoolStatus myExisting = myMap.get(myName);
271                 myMap.put(myName, myExisting == null
272                                                     ? PoolStatus.VALID
273                                                     : PoolStatus.DUPLICATE);
274             }
275         }
276 
277         /* If we are valid */
278         if (theStatus.isValid()) {
279             /* Loop through the players */
280             myIterator = competitionIterator();
281             while (myIterator.hasNext()) {
282                 Competition myComp = myIterator.next();
283 
284                 /* Ignore invalid competitions */
285                 if (!myComp.getStatus().isValid()) {
286                     continue;
287                 }
288 
289                 /* If the name is duplicate */
290                 String myName = myComp.getName();
291                 PoolStatus myStatus = myMap.get(myName);
292                 if (!myStatus.isValid()) {
293                     /* Set as duplicate */
294                     theStatus = PoolStatus.DUPLICATE;
295                     myComp.setStatus(theStatus);
296                 }
297             }
298         }
299 
300         /* return the status */
301         return theStatus;
302     }
303 }