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.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   * Player Definition.
29   */
30  public class Player implements Comparable<Player> {
31      /**
32       * Player Object Name.
33       */
34      protected static final String OBJECT_NAME = DataResource.PLAYER_NAME.getValue();
35  
36      /**
37       * Bye indication.
38       */
39      public static final String PLAYER_BYE = DataResource.PLAYER_BYE.getValue();
40  
41      /**
42       * Player Id.
43       */
44      private final IntegerProperty theId;
45  
46      /**
47       * Player Name.
48       */
49      private final ObjectProperty<String> theName;
50  
51      /**
52       * Player Contact.
53       */
54      private final ObjectProperty<String> theContact;
55  
56      /**
57       * Player Active.
58       */
59      private final ObjectProperty<Boolean> theActive;
60  
61      /**
62       * Player Selected.
63       */
64      private final ObjectProperty<Boolean> theSelected;
65  
66      /**
67       * Player Status.
68       */
69      private final ObjectProperty<PoolStatus> theStatus;
70  
71      /**
72       * Constructor.
73       */
74      public Player() {
75          /* Allocate properties */
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       * Set named property.
91       * @param pName the name of the property.
92       * @param pValue the value of the property.
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      * Obtain the id property.
107      * @return the player id property.
108      */
109     public IntegerProperty idProperty() {
110         return theId;
111     }
112 
113     /**
114      * Obtain the name property.
115      * @return the player name property.
116      */
117     public ObjectProperty<String> nameProperty() {
118         return theName;
119     }
120 
121     /**
122      * Obtain the contact property.
123      * @return the contact property.
124      */
125     public ObjectProperty<String> contactProperty() {
126         return theContact;
127     }
128 
129     /**
130      * Obtain the selected property.
131      * @return the selected property.
132      */
133     public ObjectProperty<Boolean> selectedProperty() {
134         return theSelected;
135     }
136 
137     /**
138      * Obtain the active property.
139      * @return the active property.
140      */
141     public ObjectProperty<Boolean> activeProperty() {
142         return theActive;
143     }
144 
145     /**
146      * Obtain the status property.
147      * @return the status.
148      */
149     public ObjectProperty<PoolStatus> statusProperty() {
150         return theStatus;
151     }
152 
153     /**
154      * Obtain the id.
155      * @return the player name.
156      */
157     protected Integer getId() {
158         return theId.getValue();
159     }
160 
161     /**
162      * Obtain the name.
163      * @return the player name.
164      */
165     public String getName() {
166         return theName.getValue();
167     }
168 
169     /**
170      * Obtain the contact.
171      * @return the contact.
172      */
173     public String getContact() {
174         return theContact.getValue();
175     }
176 
177     /**
178      * Is the player selected?
179      * @return true/false.
180      */
181     public Boolean isSelected() {
182         return theSelected.getValue();
183     }
184 
185     /**
186      * Is the player active?
187      * @return true/false.
188      */
189     public Boolean isActive() {
190         return theActive.getValue();
191     }
192 
193     /**
194      * Obtain the status.
195      * @return the status.
196      */
197     public PoolStatus getStatus() {
198         return theStatus.getValue();
199     }
200 
201     /**
202      * Set the id.
203      * @param pId the id.
204      */
205     protected void setId(final Integer pId) {
206         theId.setValue(pId);
207     }
208 
209     /**
210      * Set the name.
211      * @param pName the name.
212      */
213     public void setName(final String pName) {
214         theName.setValue(pName == null
215                                       ? null
216                                       : pName.trim());
217     }
218 
219     /**
220      * Set the contact.
221      * @param pContact the contact.
222      */
223     private void setContact(final String pContact) {
224         theContact.setValue(pContact == null
225                                             ? null
226                                             : pContact.trim());
227     }
228 
229     /**
230      * Set the active flag.
231      * @param pValue the new value.
232      */
233     protected void setActive(final Boolean pValue) {
234         theActive.setValue(pValue);
235     }
236 
237     /**
238      * Set the status.
239      * @param pStatus the status.
240      */
241     protected void setStatus(final PoolStatus pStatus) {
242         theStatus.setValue(pStatus);
243     }
244 
245     @Override
246     public boolean equals(final Object pThat) {
247         /* Handle trivial cases */
248         if (this == pThat) {
249             return true;
250         }
251         if (pThat == null) {
252             return false;
253         }
254 
255         /* Handle wrong class */
256         if (!(pThat instanceof Player)) {
257             return false;
258         }
259 
260         /* Access as player */
261         Player myThat = (Player) pThat;
262 
263         /* Check names */
264         if (!DataSet.isEqual(getId(), myThat.getId())) {
265             return false;
266         }
267 
268         /* Check names */
269         if (!DataSet.isEqual(getName(), myThat.getName())) {
270             return false;
271         }
272 
273         /* Test on contact */
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         /* Check for equality and null player */
286         if (equals(pThat)) {
287             return 0;
288         }
289         if (pThat == null) {
290             return -1;
291         }
292 
293         /* Access names */
294         String myName = getName();
295         String myOther = pThat.getName();
296 
297         /* Check for null names */
298         if (myName == null) {
299             return 1;
300         }
301         if (myOther == null) {
302             return -1;
303         }
304 
305         /* Compare names */
306         return myName.compareToIgnoreCase(myOther);
307     }
308 
309     /**
310      * Validate player.
311      * @return the status
312      */
313     protected PoolStatus validate() {
314         /* Check Name */
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         /* Check Id */
323         if (myStatus.isValid()) {
324             Integer myId = getId();
325             myStatus = myId <= 0
326                                 ? PoolStatus.INVALID
327                                 : PoolStatus.VALID;
328 
329         }
330 
331         /* Set and return the status */
332         setStatus(myStatus);
333         return myStatus;
334     }
335 
336     /**
337      * Comparator class.
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      * Player fields.
349      */
350     public enum PlayerField {
351         /**
352          * ID.
353          */
354         ID,
355 
356         /**
357          * Name.
358          */
359         NAME,
360 
361         /**
362          * Contact.
363          */
364         CONTACT,
365 
366         /**
367          * Selected.
368          */
369         SELECTED,
370 
371         /**
372          * Active.
373          */
374         ACTIVE,
375 
376         /**
377          * Status.
378          */
379         STATUS;
380 
381         /**
382          * The String name.
383          */
384         private String theName;
385 
386         @Override
387         public String toString() {
388             /* If we have not yet loaded the name */
389             if (theName == null) {
390                 /* Load the name */
391                 theName = DataResource.getKeyForPlayerField(this).getValue();
392             }
393 
394             /* return the name */
395             return theName;
396         }
397     }
398 }