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.io.File;
20 import java.time.LocalDate;
21 import java.util.Iterator;
22
23 import javafx.beans.property.ObjectProperty;
24 import javafx.beans.property.SimpleObjectProperty;
25 import net.sourceforge.jhunters.pool.PoolStatus;
26
27 /**
28 * Data Set.
29 */
30 public class DataSet {
31 /**
32 * Object name.
33 */
34 protected static final String OBJECT_NAME = "HuntersPool";
35
36 /**
37 * Hash multiplier.
38 */
39 protected static final int HASH_PRIME = 47;
40
41 /**
42 * Field Updated.
43 */
44 private static final String FIELD_UPDATED = "Updated";
45
46 /**
47 * Field LastChange.
48 */
49 private static final String FIELD_CHANGED = "LastChange";
50
51 /**
52 * Field FileName.
53 */
54 private static final String FIELD_FILENAME = "FileName";
55
56 /**
57 * DataSet updated.
58 */
59 private final ObjectProperty<Boolean> theUpdated;
60
61 /**
62 * DataSet last Change.
63 */
64 private final ObjectProperty<LocalDate> theLastChange;
65
66 /**
67 * DataSet fileName.
68 */
69 private final ObjectProperty<File> theFileName;
70
71 /**
72 * List of players.
73 */
74 private final PlayerList thePlayers;
75
76 /**
77 * List of competitions.
78 */
79 private final CompetitionList theCompetitions;
80
81 /**
82 * The status of the list.
83 */
84 private PoolStatus theStatus;
85
86 /**
87 * Constructor.
88 */
89 public DataSet() {
90 theUpdated = new SimpleObjectProperty<Boolean>(this, FIELD_UPDATED, Boolean.FALSE);
91 theLastChange = new SimpleObjectProperty<LocalDate>(this, FIELD_CHANGED);
92 theFileName = new SimpleObjectProperty<File>(this, FIELD_FILENAME);
93 thePlayers = new PlayerList();
94 theCompetitions = new CompetitionList();
95 }
96
97 /**
98 * Obtain the updated property.
99 * @return the updated property.
100 */
101 public ObjectProperty<Boolean> updatedProperty() {
102 return theUpdated;
103 }
104
105 /**
106 * Obtain the last change property.
107 * @return the last change property.
108 */
109 public ObjectProperty<LocalDate> lastChangeProperty() {
110 return theLastChange;
111 }
112
113 /**
114 * Obtain the fileName property.
115 * @return the fileName property.
116 */
117 public ObjectProperty<File> fileNameProperty() {
118 return theFileName;
119 }
120
121 /**
122 * Is the dataSet updated?
123 * @return true/false.
124 */
125 public Boolean isUpdated() {
126 return theUpdated.getValue();
127 }
128
129 /**
130 * Obtain last change date?
131 * @return the change date.
132 */
133 public LocalDate getLastChange() {
134 return theLastChange.getValue();
135 }
136
137 /**
138 * Obtain fileName.
139 * @return the fileName.
140 */
141 public File getFileName() {
142 return theFileName.getValue();
143 }
144
145 /**
146 * Set the dataSet as updated.
147 * @param pUpdated true/false.
148 */
149 public void setUpdated(final Boolean pUpdated) {
150 theUpdated.setValue(pUpdated);
151 }
152
153 /**
154 * Set the last change for the dataSet.
155 * @param pDate the date.
156 */
157 public void setLastChange(final LocalDate pDate) {
158 theLastChange.setValue(pDate);
159 }
160
161 /**
162 * Set the fileName for the dataSet.
163 * @param pFile the file.
164 */
165 public void setFileName(final File pFile) {
166 theFileName.setValue(pFile);
167 }
168
169 /**
170 * Obtain player list.
171 * @return the players
172 */
173 public PlayerList getPlayers() {
174 return thePlayers;
175 }
176
177 /**
178 * Obtain competition list.
179 * @return the competitions
180 */
181 public CompetitionList getCompetitions() {
182 return theCompetitions;
183 }
184
185 /**
186 * Obtain the status.
187 * @return the status.
188 */
189 public PoolStatus getStatus() {
190 return theStatus;
191 }
192
193 /**
194 * Obtain player iterator.
195 * @return the iterator
196 */
197 public Iterator<Player> playerIterator() {
198 return thePlayers.playerIterator();
199 }
200
201 /**
202 * Obtain competition iterator.
203 * @return the iterator
204 */
205 public Iterator<Competition> competitionIterator() {
206 return theCompetitions.competitionIterator();
207 }
208
209 /**
210 * Set players.
211 * @param pSource the source players
212 */
213 protected void setPlayers(final PlayerList pSource) {
214 /* Set the players */
215 thePlayers.setPlayers(pSource.playerIterator());
216 }
217
218 /**
219 * Set competitions.
220 * @param pSource the source competitions
221 */
222 protected void setCompetitions(final CompetitionList pSource) {
223 /* Set the competitions */
224 theCompetitions.setCompetitions(pSource.competitionIterator());
225 }
226
227 /**
228 * adjust the players.
229 */
230 public void adjustPlayers() {
231 /* adjust players in the competitions */
232 theCompetitions.adjustPlayers();
233 }
234
235 @Override
236 public boolean equals(final Object pThat) {
237 /* Handle trivial cases */
238 if (this == pThat) {
239 return true;
240 }
241 if (pThat == null) {
242 return false;
243 }
244
245 /* Handle wrong class */
246 if (!(pThat instanceof DataSet)) {
247 return false;
248 }
249
250 /* Access as dataSet */
251 DataSet myThat = (DataSet) pThat;
252
253 /* Test on players */
254 if (!thePlayers.equals(myThat.getPlayers())) {
255 return false;
256 }
257
258 /* Test competition list */
259 return theCompetitions.equals(myThat.getCompetitions());
260 }
261
262 @Override
263 public int hashCode() {
264 return (thePlayers.hashCode() * HASH_PRIME)
265 + theCompetitions.hashCode();
266 }
267
268 /**
269 * validate the data.
270 * @return the status
271 */
272 protected PoolStatus validate() {
273 /* Validate the players */
274 theStatus = thePlayers.validate();
275
276 /* If we are valid */
277 if (theStatus.isValid()) {
278 /* Clear active status on players */
279 thePlayers.setActive(Boolean.FALSE);
280
281 /* validate the competition list */
282 theStatus = theCompetitions.validate();
283 }
284
285 /* return the status */
286 return theStatus;
287 }
288
289 /**
290 * Check for equality allowing for null.
291 * @param <T> the object type
292 * @param pFirst the first object
293 * @param pSecond the second object
294 * @return true/false
295 */
296 protected static <T> boolean isEqual(final T pFirst,
297 final T pSecond) {
298 if (pFirst == null) {
299 return pSecond == null;
300 }
301 return (pSecond == null)
302 ? false
303 : pFirst.equals(pSecond);
304 }
305 }