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.util.Comparator;
20
21 import javafx.beans.property.IntegerProperty;
22 import javafx.beans.property.ObjectProperty;
23 import javafx.beans.property.SimpleIntegerProperty;
24 import javafx.beans.property.SimpleObjectProperty;
25 import net.sourceforge.jhunters.pool.PoolStatus;
26
27
28
29
30 public class Player implements Comparable<Player> {
31
32
33
34 protected static final String OBJECT_NAME = DataResource.PLAYER_NAME.getValue();
35
36
37
38
39 public static final String PLAYER_BYE = DataResource.PLAYER_BYE.getValue();
40
41
42
43
44 private final IntegerProperty theId;
45
46
47
48
49 private final ObjectProperty<String> theName;
50
51
52
53
54 private final ObjectProperty<String> theContact;
55
56
57
58
59 private final ObjectProperty<Boolean> theActive;
60
61
62
63
64 private final ObjectProperty<Boolean> theSelected;
65
66
67
68
69 private final ObjectProperty<PoolStatus> theStatus;
70
71
72
73
74 public Player() {
75
76 theId = new SimpleIntegerProperty(this, PlayerField.ID.toString());
77 theName = new SimpleObjectProperty<String>(this, PlayerField.NAME.toString());
78 theContact = new SimpleObjectProperty<String>(this, PlayerField.CONTACT.toString());
79 theActive = new SimpleObjectProperty<Boolean>(this, PlayerField.ACTIVE.toString(), Boolean.FALSE);
80 theSelected = new SimpleObjectProperty<Boolean>(this, PlayerField.SELECTED.toString(), Boolean.TRUE);
81 theStatus = new SimpleObjectProperty<PoolStatus>(this, PlayerField.STATUS.toString());
82 }
83
84 @Override
85 public String toString() {
86 return getName();
87 }
88
89
90
91
92
93
94 protected void setNamedProperty(final String pName,
95 final String pValue) {
96 if (pName.equals(PlayerField.ID.toString())) {
97 setId(DataParser.parseInteger(pValue));
98 } else if (pName.equals(PlayerField.NAME.toString())) {
99 setName(pValue);
100 } else if (pName.equals(PlayerField.CONTACT.toString())) {
101 setContact(pValue);
102 }
103 }
104
105
106
107
108
109 public IntegerProperty idProperty() {
110 return theId;
111 }
112
113
114
115
116
117 public ObjectProperty<String> nameProperty() {
118 return theName;
119 }
120
121
122
123
124
125 public ObjectProperty<String> contactProperty() {
126 return theContact;
127 }
128
129
130
131
132
133 public ObjectProperty<Boolean> selectedProperty() {
134 return theSelected;
135 }
136
137
138
139
140
141 public ObjectProperty<Boolean> activeProperty() {
142 return theActive;
143 }
144
145
146
147
148
149 public ObjectProperty<PoolStatus> statusProperty() {
150 return theStatus;
151 }
152
153
154
155
156
157 protected Integer getId() {
158 return theId.getValue();
159 }
160
161
162
163
164
165 public String getName() {
166 return theName.getValue();
167 }
168
169
170
171
172
173 public String getContact() {
174 return theContact.getValue();
175 }
176
177
178
179
180
181 public Boolean isSelected() {
182 return theSelected.getValue();
183 }
184
185
186
187
188
189 public Boolean isActive() {
190 return theActive.getValue();
191 }
192
193
194
195
196
197 public PoolStatus getStatus() {
198 return theStatus.getValue();
199 }
200
201
202
203
204
205 protected void setId(final Integer pId) {
206 theId.setValue(pId);
207 }
208
209
210
211
212
213 public void setName(final String pName) {
214 theName.setValue(pName == null
215 ? null
216 : pName.trim());
217 }
218
219
220
221
222
223 private void setContact(final String pContact) {
224 theContact.setValue(pContact == null
225 ? null
226 : pContact.trim());
227 }
228
229
230
231
232
233 protected void setActive(final Boolean pValue) {
234 theActive.setValue(pValue);
235 }
236
237
238
239
240
241 protected void setStatus(final PoolStatus pStatus) {
242 theStatus.setValue(pStatus);
243 }
244
245 @Override
246 public boolean equals(final Object pThat) {
247
248 if (this == pThat) {
249 return true;
250 }
251 if (pThat == null) {
252 return false;
253 }
254
255
256 if (!(pThat instanceof Player)) {
257 return false;
258 }
259
260
261 Player myThat = (Player) pThat;
262
263
264 if (!DataSet.isEqual(getId(), myThat.getId())) {
265 return false;
266 }
267
268
269 if (!DataSet.isEqual(getName(), myThat.getName())) {
270 return false;
271 }
272
273
274 return DataSet.isEqual(getContact(), myThat.getContact());
275 }
276
277 @Override
278 public int hashCode() {
279 return (theId.hashCode() * DataSet.HASH_PRIME)
280 + theName.hashCode();
281 }
282
283 @Override
284 public int compareTo(final Player pThat) {
285
286 if (equals(pThat)) {
287 return 0;
288 }
289 if (pThat == null) {
290 return -1;
291 }
292
293
294 String myName = getName();
295 String myOther = pThat.getName();
296
297
298 if (myName == null) {
299 return 1;
300 }
301 if (myOther == null) {
302 return -1;
303 }
304
305
306 return myName.compareToIgnoreCase(myOther);
307 }
308
309
310
311
312
313 protected PoolStatus validate() {
314
315 String myName = getName();
316 PoolStatus myStatus = ((myName == null)
317 || (myName.length() == 0)
318 || myName.equals(PLAYER_BYE))
319 ? PoolStatus.INVALID
320 : PoolStatus.VALID;
321
322
323 if (myStatus.isValid()) {
324 Integer myId = getId();
325 myStatus = myId <= 0
326 ? PoolStatus.INVALID
327 : PoolStatus.VALID;
328
329 }
330
331
332 setStatus(myStatus);
333 return myStatus;
334 }
335
336
337
338
339 public static final class PlayerComparator implements Comparator<Player> {
340 @Override
341 public int compare(final Player pFirst,
342 final Player pSecond) {
343 return pFirst.compareTo(pSecond);
344 }
345 }
346
347
348
349
350 public enum PlayerField {
351
352
353
354 ID,
355
356
357
358
359 NAME,
360
361
362
363
364 CONTACT,
365
366
367
368
369 SELECTED,
370
371
372
373
374 ACTIVE,
375
376
377
378
379 STATUS;
380
381
382
383
384 private String theName;
385
386 @Override
387 public String toString() {
388
389 if (theName == null) {
390
391 theName = DataResource.getKeyForPlayerField(this).getValue();
392 }
393
394
395 return theName;
396 }
397 }
398 }