KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > polepos > reporters > Graph


1 /*
2 This file is part of the PolePosition database benchmark
3 http://www.polepos.org
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public
16 License along with this program; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 MA 02111-1307, USA. */

19
20 package org.polepos.reporters;
21
22 import java.util.*;
23
24 import org.polepos.framework.*;
25
26
27 public class Graph {
28     
29     private final List<TeamCar>teamCars;
30     private final List<TurnSetup> setups;
31     private final Map<ResultsKey,Result> results;
32     
33     private final Circuit circuit;
34     private final Lap lap;
35     
36     public Graph(Result result){
37         teamCars= new ArrayList<TeamCar>();
38         setups=new ArrayList<TurnSetup>();
39         results=new HashMap<ResultsKey,Result>();
40         circuit = result.getCircuit();
41         lap = result.getLap();
42     }
43
44     public void addResult(TeamCar teamCar, Result result) {
45         TurnSetup setup = result.getSetup();
46         results.put(new ResultsKey(teamCar,setup),result);
47         if(!teamCars.contains(teamCar)) {
48             teamCars.add(teamCar);
49         }
50         if(!setups.contains(setup)) {
51             setups.add(setup);
52         }
53     }
54     
55     public Circuit circuit(){
56         return circuit;
57     }
58     
59     public Lap lap(){
60         return lap;
61     }
62     
63     public void compareCheckSums(){
64         
65         for (TurnSetup setup : setups()){
66             
67             long checkSum = 0;
68             boolean first = true;
69             
70             for (TeamCar teamCar: teamCars()){
71                 
72                 if(first){
73                     Result res = results.get(new ResultsKey(teamCar,setup));
74                     if(res != null){
75                         checkSum = res.getCheckSum();
76                         first = false;
77                     }
78                 } else{
79                     Result res = results.get(new ResultsKey(teamCar,setup));
80                     if(res != null){
81                         if(checkSum != res.getCheckSum()){
82                             System.err.println("Inconsistent checksum for " + res.getTeam().name() + " in " + circuit.name() + ":" + lap.name());
83                         }
84                     }
85                 }
86             }
87         }
88     }
89     
90     
91     public List<TeamCar> teamCars() {
92         return Collections.unmodifiableList(teamCars);
93     }
94
95     public List<TurnSetup> setups() {
96         return Collections.unmodifiableList(setups);
97     }
98     
99     public final long timeFor(TeamCar teamCar, TurnSetup setup) {
100         Result res = results.get(new ResultsKey(teamCar,setup));
101         if(res == null){
102             return Integer.MAX_VALUE;
103         }
104         return res.getTime();
105     }
106     
107     private class ResultsKey {
108         
109         final TeamCar teamCar;
110         final TurnSetup setup;
111         
112         public ResultsKey(TeamCar teamCar, TurnSetup setup) {
113             this.teamCar = teamCar;
114             this.setup = setup;
115         }
116         
117         @Override JavaDoc
118         public boolean equals(Object JavaDoc obj) {
119             if(obj==this) {
120                 return true;
121             }
122             if(obj==null||obj.getClass()!=getClass()) {
123                 return false;
124             }
125             ResultsKey key=(ResultsKey)obj;
126             return teamCar.equals(key.teamCar) && setup.equals(key.setup);
127         }
128         
129         @Override JavaDoc
130         public int hashCode() {
131             return teamCar.hashCode() + setup.hashCode();
132         }
133     }
134 }
135
Popular Tags