View Javadoc

1   // %3549957018:hoplugins.teamAnalyzer.manager%
2   package hoplugins.teamAnalyzer.manager;
3   
4   import hoplugins.commons.utils.ListUtil;
5   import hoplugins.commons.utils.PluginProperty;
6   
7   import hoplugins.teamAnalyzer.comparator.AppearanceComparator;
8   import hoplugins.teamAnalyzer.comparator.PerformanceComparator;
9   import hoplugins.teamAnalyzer.report.PositionReport;
10  import hoplugins.teamAnalyzer.report.SpotReport;
11  import hoplugins.teamAnalyzer.report.TacticReport;
12  import hoplugins.teamAnalyzer.report.TeamReport;
13  import hoplugins.teamAnalyzer.vo.PlayerAppearance;
14  import hoplugins.teamAnalyzer.vo.SpotLineup;
15  import hoplugins.teamAnalyzer.vo.TeamLineup;
16  
17  import java.util.ArrayList;
18  import java.util.Arrays;
19  import java.util.Collection;
20  import java.util.Iterator;
21  import java.util.SortedSet;
22  
23  
24  /***
25   * TODO Missing Class Documentation
26   *
27   * @author TODO Author Name
28   */
29  public class TeamLineupBuilder {
30      //~ Instance fields ----------------------------------------------------------------------------
31  
32      private TeamLineup teamLineup;
33  
34      //~ Constructors -------------------------------------------------------------------------------
35  
36      /***
37       * Creates a new TeamLineupBuilder object.
38       *
39       * @param teamReport TODO Missing Constructuor Parameter Documentation
40       */
41      public TeamLineupBuilder(TeamReport teamReport) {
42          teamLineup = new TeamLineup();
43          teamLineup.setRating(teamReport.getRating());
44          teamLineup.setStars(teamReport.getStars());
45  
46          for (int spot = 1; spot < 12; spot++) {
47              SpotReport spotReport = (SpotReport) teamReport.getSpotReport(spot);
48  
49              if (spotReport != null) {
50                  SpotLineup spotLineup = buildSpotLineup(spotReport);
51  
52                  teamLineup.setSpotLineup(spotLineup, spot);
53              }
54          }
55      }
56  
57      //~ Methods ------------------------------------------------------------------------------------
58  
59      /***
60       * TODO Missing Method Documentation
61       *
62       * @return TODO Missing Return Method Documentation
63       */
64      public TeamLineup getLineup() {
65          return teamLineup;
66      }
67  
68      /***
69       * TODO Missing Method Documentation
70       *
71       * @param positions TODO Missing Method Parameter Documentation
72       *
73       * @return TODO Missing Return Method Documentation
74       */
75      private Collection getAllTactics(Collection positions) {
76          Collection tactics = new ArrayList();
77  
78          for (Iterator iter = positions.iterator(); iter.hasNext();) {
79              PositionReport positionReport = (PositionReport) iter.next();
80  
81              tactics.addAll(positionReport.getTacticReports());
82          }
83  
84          return tactics;
85      }
86  
87      /***
88       * TODO Missing Method Documentation
89       *
90       * @param collection TODO Missing Method Parameter Documentation
91       *
92       * @return TODO Missing Return Method Documentation
93       */
94      private PlayerAppearance getPlayer(Collection collection) {
95          PlayerAppearance[] appearances = getSortedAppearance(collection);
96  
97          if (appearances.length == 1) {
98              return appearances[0];
99          }
100 
101         if (appearances[0].getAppearance() > appearances[1].getAppearance()) {
102             return appearances[0];
103         }
104 
105         PlayerAppearance app = new PlayerAppearance();
106 
107         if ((appearances.length > 2)
108             && (appearances[2].getAppearance() == appearances[0].getAppearance())) {
109             app.setName(PluginProperty.getString("TeamLineupBuilder.Unknown")); //$NON-NLS-1$
110         } else {
111             //			String status1 = (appearances[0].getStatus()!=PlayerManager.AVAILABLE)? "*":"";			
112             //			String status2 = (appearances[1].getStatus()!=PlayerManager.AVAILABLE)? "*":"";
113             app.setName(appearances[0].getName() + "/" + appearances[1].getName());
114         }
115 
116         app.setApperarence(appearances[0].getAppearance());
117 
118         return app;
119     }
120 
121     /***
122      * TODO Missing Method Documentation
123      *
124      * @param tacticsReport TODO Missing Method Parameter Documentation
125      *
126      * @return TODO Missing Return Method Documentation
127      */
128     private int getPosition(TacticReport[] tacticsReport) {
129         if (isSingle(tacticsReport)) {
130             return tacticsReport[0].getTacticCode();
131         }
132 
133         if (tacticsReport[0].getPosition() == tacticsReport[1].getPosition()) {
134             return tacticsReport[0].getTacticCode();
135         }
136 
137         return -1;
138     }
139 
140     /***
141      * TODO Missing Method Documentation
142      *
143      * @param tacticsReport TODO Missing Method Parameter Documentation
144      *
145      * @return TODO Missing Return Method Documentation
146      */
147     private boolean isSingle(TacticReport[] tacticsReport) {
148         if (tacticsReport.length == 1) {
149             return true;
150         }
151 
152         if (tacticsReport[0].getAppearance() > tacticsReport[1].getAppearance()) {
153             return true;
154         }
155 
156         return false;
157     }
158 
159     /***
160      * TODO Missing Method Documentation
161      *
162      * @param appearance TODO Missing Method Parameter Documentation
163      *
164      * @return TODO Missing Return Method Documentation
165      */
166     private PlayerAppearance[] getSortedAppearance(Collection appearance) {
167         SortedSet sorted = ListUtil.getSortedSet(appearance, new AppearanceComparator());
168         int size = sorted.size();
169         PlayerAppearance[] array = new PlayerAppearance[size];
170         int i = 0;
171 
172         for (Iterator iter = sorted.iterator(); iter.hasNext();) {
173             PlayerAppearance element = (PlayerAppearance) iter.next();
174 
175             array[i] = element;
176             i++;
177         }
178 
179         return array;
180     }
181 
182     /***
183      * TODO Missing Method Documentation
184      *
185      * @param tactics TODO Missing Method Parameter Documentation
186      *
187      * @return TODO Missing Return Method Documentation
188      */
189     private TacticReport[] getSortedTactics(Collection tactics) {
190         SortedSet sorted = ListUtil.getSortedSet(tactics, new PerformanceComparator());
191         int size = sorted.size();
192         TacticReport[] tacticsReport = new TacticReport[size];
193         int i = 0;
194 
195         for (Iterator iter = sorted.iterator(); iter.hasNext();) {
196             TacticReport element = (TacticReport) iter.next();
197 
198             tacticsReport[i] = element;
199             i++;
200         }
201 
202         return tacticsReport;
203     }
204 
205     /***
206      * TODO Missing Method Documentation
207      *
208      * @param spotReport TODO Missing Method Parameter Documentation
209      *
210      * @return TODO Missing Return Method Documentation
211      */
212     private SpotLineup buildSpotLineup(SpotReport spotReport) {
213         SpotLineup spotLineup = new SpotLineup(spotReport);
214 
215         spotLineup.setSpot(spotReport.getSpot());
216 
217         PlayerAppearance appearance = getPlayer(spotReport.getPlayerAppearance());
218 
219         spotLineup.setName(appearance.getName());
220         spotLineup.setPlayerId(appearance.getPlayerId());
221         spotLineup.setAppearance(appearance.getAppearance());
222         spotLineup.setStatus(appearance.getStatus());
223 
224         Collection tacticsReports = getAllTactics(spotReport.getPositionReports());
225         TacticReport[] tacticsReport = getSortedTactics(tacticsReports);
226 
227         spotLineup.setTactics(Arrays.asList(tacticsReport));
228         spotLineup.setPosition(getPosition(tacticsReport));
229 
230         return spotLineup;
231     }
232 }