|
1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * |
|
5 * Redistribution and use in source and binary forms, with or without |
|
6 * modification, are permitted provided that the following conditions are met: |
|
7 * |
|
8 * - Redistributions of source code must retain the above copyright notice, |
|
9 * this list of conditions and the following disclaimer. |
|
10 * - Redistributions in binary form must reproduce the above copyright notice, |
|
11 * this list of conditions and the following disclaimer in the documentation |
|
12 * and/or other materials provided with the distribution. |
|
13 * - Neither the name of Nokia Corporation nor the names of its contributors |
|
14 * may be used to endorse or promote products derived from this software |
|
15 * without specific prior written permission. |
|
16 * |
|
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
27 * POSSIBILITY OF SUCH DAMAGE. |
|
28 * |
|
29 * Initial Contributors: |
|
30 * Nokia Corporation - initial contribution. |
|
31 * |
|
32 * Contributors: |
|
33 * |
|
34 * Description: |
|
35 * |
|
36 */ |
|
37 |
|
38 using System; |
|
39 using System.IO; |
|
40 using System.Collections.Generic; |
|
41 using System.Threading; |
|
42 using SymbianUtils; |
|
43 |
|
44 namespace HeapComparisonLib.CSV |
|
45 { |
|
46 internal class CSVWorkerTwoDataSetComparator : DisposableObject |
|
47 { |
|
48 #region Observer related |
|
49 public enum TEvent |
|
50 { |
|
51 EExportingStarted = 0, |
|
52 EExportingProgress, |
|
53 EExportingComplete |
|
54 } |
|
55 |
|
56 public delegate void Observer( TEvent aEvent, string aFullName ); |
|
57 public event Observer eObserver; |
|
58 #endregion |
|
59 |
|
60 #region Constructors & destructor |
|
61 public CSVWorkerTwoDataSetComparator( CSVDataSet aCol1, CSVDataSet aCol2, string aOutputFileName ) |
|
62 { |
|
63 // Make copies because we actually dequeue items from the list as we processed them |
|
64 // and we must not alter the originals or else we'll break the subsequent comparison(s) |
|
65 iCol1 = new CSVDataSet( aCol1 ); |
|
66 iCol2 = new CSVDataSet( aCol2 ); |
|
67 iExcelExporter = new CSVExcelExporterTwoDataSets( aOutputFileName, aCol1.FileName, aCol2.FileName ); |
|
68 } |
|
69 #endregion |
|
70 |
|
71 #region API |
|
72 public void CompareAndSaveAsync() |
|
73 { |
|
74 iWorkerThread = new System.Threading.Thread( new ThreadStart( ThreadFunction ) ); |
|
75 iWorkerThread.Start(); |
|
76 } |
|
77 #endregion |
|
78 |
|
79 #region Properties |
|
80 public int Progress |
|
81 { |
|
82 get |
|
83 { |
|
84 int count = 0; |
|
85 int index = 0; |
|
86 // |
|
87 lock ( this ) |
|
88 { |
|
89 index = iIndex; |
|
90 count = iPairings.Count; |
|
91 } |
|
92 |
|
93 float ret = (float) index / (float) count; |
|
94 ret *= 100.0f; |
|
95 return (int) ret; |
|
96 } |
|
97 } |
|
98 #endregion |
|
99 |
|
100 #region From DisposableObject |
|
101 protected override void CleanupManagedResources() |
|
102 { |
|
103 try |
|
104 { |
|
105 if ( iExcelExporter != null ) |
|
106 { |
|
107 iExcelExporter.Dispose(); |
|
108 iExcelExporter = null; |
|
109 } |
|
110 } |
|
111 finally |
|
112 { |
|
113 base.CleanupManagedResources(); |
|
114 } |
|
115 } |
|
116 #endregion |
|
117 |
|
118 #region Thread function |
|
119 private void ThreadFunction() |
|
120 { |
|
121 // Report started |
|
122 if ( eObserver != null ) |
|
123 { |
|
124 eObserver( TEvent.EExportingStarted, null ); |
|
125 } |
|
126 |
|
127 // Create our list of sorted comparison pairs. |
|
128 BuildPairs(); |
|
129 |
|
130 // Now compare them and export to excel |
|
131 CompareAndExport(); |
|
132 |
|
133 // Dispose excel object - closes file |
|
134 iExcelExporter.Dispose(); |
|
135 iExcelExporter = null; |
|
136 |
|
137 // Report finished |
|
138 if ( eObserver != null ) |
|
139 { |
|
140 eObserver( TEvent.EExportingComplete, null ); |
|
141 } |
|
142 } |
|
143 #endregion |
|
144 |
|
145 #region Internal methods |
|
146 private void BuildPairs() |
|
147 { |
|
148 // Treat the first collection as the master list. |
|
149 int count = iCol1.Count; |
|
150 for ( int i = count - 1; i >= 0; i-- ) |
|
151 { |
|
152 CSVThread master = iCol1[ i ]; |
|
153 |
|
154 // Remove primary entry also |
|
155 iCol1.Remove( master ); |
|
156 |
|
157 // Create or locate secondary |
|
158 CSVThread secondary = FindAppropriateSecondary( master, iCol2 ); |
|
159 |
|
160 // Create a pairing |
|
161 iPairings.Add( master.FullName, new CSVThreadPair( master, secondary ) ); |
|
162 } |
|
163 |
|
164 // Now do the same but for the secondary list this time. It should |
|
165 // be largely empty. The only entries that will remain are threads |
|
166 // which don't exist in the primary list. |
|
167 count = iCol2.Count; |
|
168 for ( int i = count - 1; i >= 0; i-- ) |
|
169 { |
|
170 CSVThread master = iCol2[ i ]; |
|
171 |
|
172 // Remove primary entry also |
|
173 iCol2.Remove( master ); |
|
174 |
|
175 // Create or locate secondary |
|
176 CSVThread secondary = FindAppropriateSecondary( master, iCol1 ); |
|
177 |
|
178 // Create a pairing - but this time the master is the 2nd entry |
|
179 // in the pair. This ensures we keep the threads in the correct |
|
180 // columns in the final excel spreadsheet. |
|
181 iPairings.Add( master.FullName, new CSVThreadPair( secondary, master ) ); |
|
182 } |
|
183 } |
|
184 |
|
185 private CSVThread FindAppropriateSecondary( CSVThread aMaster, CSVDataSet aSecondaryCol ) |
|
186 { |
|
187 System.Diagnostics.Debug.Assert( aMaster.FullName != string.Empty ); |
|
188 |
|
189 // We must try to find the corresponding secondary thread with which to compare |
|
190 // aMaster. |
|
191 CSVThread secondary = aSecondaryCol[ aMaster.FullName ]; |
|
192 if ( secondary == null ) |
|
193 { |
|
194 // Secondary collection doesn't contain this thread, so make a default entry. |
|
195 secondary = CSVThread.NewDefault( aMaster.FullName ); |
|
196 } |
|
197 else |
|
198 { |
|
199 aSecondaryCol.Remove( secondary ); |
|
200 } |
|
201 |
|
202 return secondary; |
|
203 } |
|
204 |
|
205 private void CompareAndExport() |
|
206 { |
|
207 lock ( this ) |
|
208 { |
|
209 iIndex = 0; |
|
210 } |
|
211 |
|
212 foreach ( KeyValuePair<string, CSVThreadPair> kvp in iPairings ) |
|
213 { |
|
214 CSVThreadPair pair = kvp.Value; |
|
215 |
|
216 // Report progress to any observers |
|
217 if ( eObserver != null ) |
|
218 { |
|
219 eObserver( TEvent.EExportingProgress, kvp.Key ); |
|
220 } |
|
221 |
|
222 lock ( this ) |
|
223 { |
|
224 ++iIndex; |
|
225 } |
|
226 |
|
227 CompareAndExport( pair.Master, pair.Secondary ); |
|
228 } |
|
229 } |
|
230 |
|
231 private void CompareAndExport( CSVThread aMaster, CSVThread aSecondary ) |
|
232 { |
|
233 iExcelExporter.CompareThread( aMaster, aSecondary ); |
|
234 } |
|
235 #endregion |
|
236 |
|
237 #region Data members |
|
238 private readonly CSVDataSet iCol1; |
|
239 private readonly CSVDataSet iCol2; |
|
240 private CSVExcelExporterTwoDataSets iExcelExporter = null; |
|
241 private SortedList<string, CSVThreadPair> iPairings = new SortedList< string, CSVThreadPair>(); |
|
242 private System.Threading.Thread iWorkerThread; |
|
243 private int iIndex = 0; |
|
244 #endregion |
|
245 } |
|
246 } |