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.HashMap;
20  import java.util.Iterator;
21  import java.util.ListIterator;
22  import java.util.Map;
23  
24  import javafx.collections.FXCollections;
25  import javafx.collections.ObservableList;
26  
27  /**
28   * Player Performance Analysis.
29   */
30  public class AnalysisTable {
31      /**
32       * Map of entries.
33       */
34      private final Map<Integer, PlayerAnalysis> theEntries;
35  
36      /**
37       * Observable list.
38       */
39      private final ObservableList<PlayerAnalysis> theTable;
40  
41      /**
42       * Constructor.
43       */
44      protected AnalysisTable() {
45          /* Create the player hash map */
46          theEntries = new HashMap<Integer, PlayerAnalysis>();
47  
48          /* Create the table */
49          theTable = FXCollections.observableArrayList();
50      }
51  
52      /**
53       * Reset analysis.
54       */
55      protected void reset() {
56          Iterator<PlayerAnalysis> myIterator = theTable.iterator();
57          while (myIterator.hasNext()) {
58              PlayerAnalysis myCurr = myIterator.next();
59              myCurr.reset();
60          }
61      }
62  
63      /**
64       * Obtain player analysis.
65       * @param pPlayer the player.
66       * @return the analysis
67       */
68      protected PlayerAnalysis getAnalysis(final Player pPlayer) {
69          /* Access existing entry */
70          Integer myId = pPlayer.getId();
71          PlayerAnalysis myAnalysis = theEntries.get(myId);
72  
73          /* If we need a new entry */
74          if (myAnalysis == null) {
75              /* Create new analysis and store in the map */
76              myAnalysis = new PlayerAnalysis(pPlayer);
77              theEntries.put(myId, myAnalysis);
78  
79              /* Add the analysis to the list */
80              theTable.add(myAnalysis);
81          }
82  
83          /* Return the analysis */
84          return myAnalysis;
85      }
86  
87      /**
88       * Obtain the table.
89       * @return the table
90       */
91      protected ObservableList<PlayerAnalysis> getTable() {
92          return theTable;
93      }
94  
95      /**
96       * Is the table active?
97       * @return true/false
98       */
99      protected boolean isActive() {
100         return theTable.size() > 1
101                && theTable.get(0).isActive();
102     }
103 
104     /**
105      * Update TieBreaker.
106      * @param pMaster the master table to update
107      */
108     protected void updateTieBreaker(final AnalysisTable pMaster) {
109         /* Loop through the entries */
110         int myTieBreaker = 1;
111         PlayerAnalysis myLast = null;
112         ListIterator<PlayerAnalysis> myIterator = theTable.listIterator();
113         while (myIterator.hasNext()) {
114             PlayerAnalysis myCurr = myIterator.next();
115 
116             /* Locate player in master table */
117             PlayerAnalysis myAnalysis = pMaster.getAnalysis(myCurr.getPlayer());
118 
119             /* If we have a previous entry */
120             if (myLast != null) {
121                 /* Compare the entry */
122                 int iDiff = myCurr.tieBreakCompareTo(myLast);
123                 if (iDiff > 0) {
124                     /* Increment position if later */
125                     myTieBreaker++;
126                 }
127             }
128 
129             /* Store tieBreak and record last entry */
130             myAnalysis.setTieBreaker(myTieBreaker);
131             myLast = myCurr;
132         }
133     }
134 }