View Javadoc

1   package jp.osdl.jbento2.analyzer;
2   
3   import java.io.BufferedWriter;
4   import java.io.FileWriter;
5   import java.io.IOException;
6   import java.util.ArrayList;
7   import java.util.Comparator;
8   import java.util.HashMap;
9   import java.util.Iterator;
10  import java.util.List;
11  import java.util.Map;
12  import java.util.Set;
13  import java.util.SortedSet;
14  import java.util.TreeSet;
15  
16  public class MultiResultCollector {
17  
18      private String srcDir = null;
19      private String fileName = null;
20      private String dest = "result";
21      private String delimiter = ",";
22      private String target = null;
23      private SarAverageHandler[] handlers = null;
24      private Map map = new HashMap();
25      private String destName = null;
26      private TextTable tt = null;
27      
28      //�ǂ���Д�l���ŕёւ�
29      public void analyzeNumeric() throws IOException {
30          SortedSet rowSeries = createLabelNumeric(0);
31          SortedSet columnXLabel = createLabelNumeric(1);
32          
33          firstRow(rowSeries,target);
34          addRow(rowSeries,columnXLabel);
35          end();
36      }
37     
38      //xLabel�������l���ŕёւ�
39      public void analyze() throws IOException {
40          SortedSet rowSeries = createLabelNutural(0);
41          SortedSet columnXLabel = createLabelNumeric(1);
42          
43          firstRow(rowSeries,target);
44          addRow(rowSeries,columnXLabel);
45          end();
46      }
47      
48      public SortedSet createLabelNumeric(int number) {
49          SortedSet firstData = new TreeSet(new NumericComparator());
50          Set set = map.keySet();
51          Iterator ite = set.iterator();
52          List list;
53  
54          while (ite.hasNext()) {
55              list = (ArrayList) ite.next();
56              firstData.add((String) list.get(number));
57          }
58  
59          return firstData;
60      }
61      
62      public SortedSet createLabelNutural(int number) {
63          SortedSet firstData = new TreeSet();
64          Set set = map.keySet();
65          Iterator ite = set.iterator();
66          List list;
67  
68          while (ite.hasNext()) {
69              list = (ArrayList) ite.next();
70              firstData.add((String) list.get(number));
71          }
72  
73          return firstData;
74      }
75      
76      class NumericComparator implements Comparator {
77          public int compare(Object o1, Object o2) {
78              String s1 = (String) o1;
79              String s2 = (String) o2;
80              try {
81                  return Double.valueOf(s1).compareTo(Double.valueOf(s2));
82              } catch (NumberFormatException e) {
83                  return s1.compareTo(s2);
84              }
85          }
86      }
87      
88      public void firstRow(SortedSet rowSeries,String target){
89          
90          List data = new ArrayList();
91          tt = new TextTable(rowSeries.size() + 1,TextTable.ALIGN_RIGHT,",");
92          
93          data.add(0,target);
94          Iterator ite_row = rowSeries.iterator();
95          while (ite_row.hasNext()) {
96              String series = (String)ite_row.next();
97              data.add(series);
98          }
99          tt.addRow((String[])data.toArray(new String[rowSeries.size()+1]));
100     }
101     
102     public void addRow(SortedSet rowSeries,SortedSet columnXLabel){
103         
104         List list = new ArrayList();
105         List data = new ArrayList();
106         
107         Iterator ite_column = columnXLabel.iterator();
108         while (ite_column.hasNext()) {
109             
110             String xLabel = (String)ite_column.next();
111             data.add(xLabel);
112             Iterator ite_row = rowSeries.iterator();
113             
114             while (ite_row.hasNext()) {
115                 
116                 String series = (String)ite_row.next();
117                 list.add(0,series);
118                 list.add(1,xLabel);
119                 
120                 if (!map.containsKey(list)) {
121                     data.add("");
122                 } else {
123                     data.add((String)map.get(list));
124                 }
125                 
126                 list.clear();
127             }
128             tt.addRow((String[])data.toArray(new String[rowSeries.size()+1]));
129             data.clear();
130         }
131         
132     }
133     
134     public void end() {
135         BufferedWriter writer = null;
136         try {
137             writer = new BufferedWriter(new FileWriter(destName));
138             writer.write(tt.toString());
139             writer.flush();
140         } catch (IOException e){
141             e.printStackTrace(System.err);
142         } finally {
143             try {
144                 writer.close();
145             } catch (IOException e) {
146                 //nothing to do
147             }
148         }
149     }
150     
151     public String getDelimiter() {
152         return delimiter;
153     }
154 
155     public void setDelimiter(String delimiter) {
156         this.delimiter = delimiter;
157     }
158 
159     public String getDestDir() {
160         return dest;
161     }
162 
163     public void setDestDir(String destDir) {
164         this.dest = destDir;
165     }
166 
167     public String getFileName() {
168         return fileName;
169     }
170 
171     public void setFileName(String fileName) {
172         this.fileName = fileName;
173     }
174 
175     public String getSrcDir() {
176         return srcDir;
177     }
178 
179     public void setSrcDir(String srcDir) {
180         this.srcDir = srcDir;
181     }
182 
183     public SarAverageHandler[] getHandlers() {
184         return handlers;
185     }
186 
187     public void setHandlers(SarAverageHandler[] handlers) {
188         this.handlers = handlers;
189     }
190 
191     public String getDestName() {
192         return destName;
193     }
194 
195     public void setDestName(String destName) {
196         this.destName = destName;
197     }
198 
199     public Map getMap() {
200         return map;
201     }
202 
203     public void setMap(Map map) {
204         this.map = map;
205     }
206 
207     public String getTarget() {
208         return target;
209     }
210 
211     public void setTarget(String target) {
212         this.target = target;
213     }
214 
215 }