001    package jp.osdl.jbento2.analyzer;
002    
003    import java.io.BufferedWriter;
004    import java.io.FileWriter;
005    import java.io.IOException;
006    import java.util.ArrayList;
007    import java.util.Comparator;
008    import java.util.HashMap;
009    import java.util.Iterator;
010    import java.util.List;
011    import java.util.Map;
012    import java.util.Set;
013    import java.util.SortedSet;
014    import java.util.TreeSet;
015    
016    public class MultiResultCollector {
017    
018        private String srcDir = null;
019        private String fileName = null;
020        private String dest = "result";
021        private String delimiter = ",";
022        private String target = null;
023        private SarAverageHandler[] handlers = null;
024        private Map map = new HashMap();
025        private String destName = null;
026        private TextTable tt = null;
027        
028        //?½Ç‚�?½?½Ð”�l?½?½?½Å?ёւ�
029        public void analyzeNumeric() throws IOException {
030            SortedSet rowSeries = createLabelNumeric(0);
031            SortedSet columnXLabel = createLabelNumeric(1);
032            
033            firstRow(rowSeries,target);
034            addRow(rowSeries,columnXLabel);
035            end();
036        }
037       
038        //xLabel?½?½?½?½?½?½?½l?½?½?½Å?ёւ�
039        public void analyze() throws IOException {
040            SortedSet rowSeries = createLabelNutural(0);
041            SortedSet columnXLabel = createLabelNumeric(1);
042            
043            firstRow(rowSeries,target);
044            addRow(rowSeries,columnXLabel);
045            end();
046        }
047        
048        public SortedSet createLabelNumeric(int number) {
049            SortedSet firstData = new TreeSet(new NumericComparator());
050            Set set = map.keySet();
051            Iterator ite = set.iterator();
052            List list;
053    
054            while (ite.hasNext()) {
055                list = (ArrayList) ite.next();
056                firstData.add((String) list.get(number));
057            }
058    
059            return firstData;
060        }
061        
062        public SortedSet createLabelNutural(int number) {
063            SortedSet firstData = new TreeSet();
064            Set set = map.keySet();
065            Iterator ite = set.iterator();
066            List list;
067    
068            while (ite.hasNext()) {
069                list = (ArrayList) ite.next();
070                firstData.add((String) list.get(number));
071            }
072    
073            return firstData;
074        }
075        
076        class NumericComparator implements Comparator {
077            public int compare(Object o1, Object o2) {
078                String s1 = (String) o1;
079                String s2 = (String) o2;
080                try {
081                    return Double.valueOf(s1).compareTo(Double.valueOf(s2));
082                } catch (NumberFormatException e) {
083                    return s1.compareTo(s2);
084                }
085            }
086        }
087        
088        public void firstRow(SortedSet rowSeries,String target){
089            
090            List data = new ArrayList();
091            tt = new TextTable(rowSeries.size() + 1,TextTable.ALIGN_RIGHT,",");
092            
093            data.add(0,target);
094            Iterator ite_row = rowSeries.iterator();
095            while (ite_row.hasNext()) {
096                String series = (String)ite_row.next();
097                data.add(series);
098            }
099            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    }