View Javadoc
1   package net.logAnalyzer.analysis;
2   
3   import java.text.SimpleDateFormat;
4   import java.util.Date;
5   import java.util.Hashtable;
6   import java.util.Vector;
7   
8   /***
9    * This class defines a template to implement results of analysis.
10   * 
11   * @author Karim REFEYTON
12   * @version 0.1
13   */
14  public abstract class LAAnalysis {
15      /***
16       * Standard format used for dates.
17       */
18      private SimpleDateFormat dateFormat = new SimpleDateFormat(
19              "yyyy/MM/dd HH:mm:ss");
20  
21      /***
22       * Originator analyzer.
23       */
24      private LAAnalyzer analyzer = null;
25  
26      /***
27       * Resulting keys.
28       */
29      private Vector keys = null;
30  
31      /***
32       * Resulting values groups.
33       */
34      private Hashtable valuesGroups = null;
35  
36      /***
37       * Index of values in the array {@link #valuesNames}.
38       */
39      private Hashtable valuesIndex = null;
40  
41      /***
42       * Names of values.
43       */
44      private String[] valuesNames = null;
45  
46      /***
47       * Construct a new analysis from the specified analyzer and values names.
48       * 
49       * @param analyzer
50       *            Analyzer.
51       * @param valuesNames
52       *            Names of values.
53       */
54      protected LAAnalysis(LAAnalyzer analyzer, String[] valuesNames) {
55          this.analyzer = analyzer;
56          this.keys = new Vector();
57          this.valuesGroups = new Hashtable();
58          this.valuesIndex = new Hashtable();
59          this.valuesNames = valuesNames;
60          // Construct the index hashtable associated to the values names array
61          for (int i = 0; i < valuesNames.length; i++) {
62              this.valuesIndex.put(this.valuesNames[i], new Integer(i));
63          }
64      }
65  
66      /***
67       * Returns the originator analyzer.
68       * 
69       * @return Analyzer.
70       */
71      public final LAAnalyzer getAnalyzer() {
72          return analyzer;
73      }
74  
75      /***
76       * Returns the format used for dates.  
77       * @return Date format.
78       */
79      public final SimpleDateFormat getDateFormat() {
80          return this.dateFormat;
81      }
82  
83      /***
84       * Returns <tt>true</tt> if the analysis contains the specified key.
85       * 
86       * @param key
87       *            Key to check.
88       * @return <tt>true</tt> if the key exists; <tt>false</tt> otherwise.
89       */
90      public final boolean containsKey(Comparable key) {
91          return valuesGroups.containsKey(key);
92      }
93  
94      /***
95       * Moves the key at the specified index to the specified new index.
96       * 
97       * @param index
98       *            Index of the key to move.
99       * @param newIndex
100      *            New index of the key.
101      */
102     protected final void moveKeyAt(int index, int newIndex) {
103         Comparable keyToMove = (Comparable) keys.remove(index);
104         keys.insertElementAt(keyToMove, newIndex);
105     }
106 
107     /***
108      * Returns the keys at the specified index.
109      * 
110      * @param index
111      *            Key index.
112      * @return Key at the specified index.
113      */
114     public final Comparable getKey(int index) {
115         return (Comparable) keys.get(index);
116     }
117 
118     /***
119      * Returns the keys at the specified index. Dates are formatted with
120      * {@link #dateFormat}.
121      * 
122      * @param index
123      *            Key index.
124      * @return Key at the specified index.
125      */
126     public final String getStringKey(int index) {
127         Comparable key = (Comparable) keys.get(index);
128         if (key instanceof Date) {
129             if (getAnalyzer() instanceof LAPeriodicAnalyzer) {
130                 return ((LAPeriodicAnalyzer) getAnalyzer())
131                         .normalizeToString((Date) key);
132             } else {
133                 return dateFormat.format(key);
134             }
135         } else {
136             return key.toString();
137         }
138     }
139 
140     /***
141      * Returns the resulting values keys vector.
142      * 
143      * @return Values keys vector.
144      */
145     public final Vector getKeys() {
146         return keys;
147     }
148 
149     /***
150      * Returns the value associated to the specified key.
151      * 
152      * @param key
153      *            Key of the values group.
154      * @param index
155      *            Index of the value.
156      * @return Value if exists; <tt>null</tt> otherwise.
157      */
158     protected final Date getDateValue(Comparable key, int index) {
159         return (Date) getObjectValue(key, index);
160     }
161 
162     /***
163      * Returns the value associated to the specified key.
164      * 
165      * @param key
166      *            Key of the values group.
167      * @param index
168      *            Index of the value.
169      * @return Value if exists; <tt>0</tt> otherwise.
170      */
171     protected final double getDoubleValue(Comparable key, int index) {
172         Number value = (Number) getObjectValue(key, index);
173         if (value != null) {
174             return value.doubleValue();
175         } else {
176             return 0D;
177         }
178     }
179 
180     /***
181      * Returns the value associated to the specified key.
182      * 
183      * @param key
184      *            Key of the values group.
185      * @param index
186      *            Index of the value.
187      * @return Value if exists; <tt>0</tt> otherwise.
188      */
189     protected final long getLongValue(Comparable key, int index) {
190         Number value = (Number) getObjectValue(key, index);
191         if (value != null) {
192             return value.longValue();
193         } else {
194             return 0L;
195         }
196     }
197 
198     /***
199      * Returns the value associated to the specified key. Dates are formatted
200      * with {@link #dateFormat}.
201      * 
202      * @param key
203      *            Key of the values group.
204      * @param index
205      *            Index of the value.
206      * @return Value if exists; <tt>n""ull</tt> otherwise.
207      */
208     protected final String getStringValue(Comparable key, int index) {
209         Object value = getObjectValue(key, index);
210         if (value != null) {
211             if (value instanceof Date) {
212                 return dateFormat.format(value);
213             } else {
214                 return value.toString();
215             }
216         } else {
217             return "";
218         }
219     }
220 
221     /***
222      * Returns the value associated to the specified key.
223      * 
224      * @param key
225      *            Key of the values group.
226      * @param index
227      *            Index of the value.
228      * @return Value if exists; <tt>null</tt> otherwise.
229      */
230     protected final Object getObjectValue(Comparable key, int index) {
231         Object[] values = (Object[]) valuesGroups.get(key);
232         if (values == null) {
233             return null;
234         } else {
235             return values[index];
236         }
237     }
238 
239     /***
240      * Set the value associated to the specified key.
241      * 
242      * @param key
243      *            Key of the values group.
244      * @param index
245      *            Index of the value.
246      * @param value
247      *            Value to set.
248      * @return Old value if exists; <tt>null</tt> otherwise.
249      */
250     protected final Date putDateValue(Comparable key, int index, Date value) {
251         Date oldValue = getDateValue(key, index);
252         putValue(key, index, value);
253         return oldValue;
254     }
255 
256     /***
257      * Set the value associated to the specified key.
258      * 
259      * @param key
260      *            Key of the values group.
261      * @param index
262      *            Index of the value.
263      * @param value
264      *            Value to set.
265      * @return Old value if exists; <tt>null</tt> otherwise.
266      */
267     protected final double putDoubleValue(Comparable key, int index, double value) {
268         double oldValue = getDoubleValue(key, index);
269         putValue(key, index, new Double(value));
270         return oldValue;
271     }
272 
273     /***
274      * Set the value associated to the specified key.
275      * 
276      * @param key
277      *            Key of the values group.
278      * @param index
279      *            Index of the value.
280      * @param value
281      *            Value to set.
282      * @return Old value if exists; <tt>null</tt> otherwise.
283      */
284     protected final long putLongValue(Comparable key, int index, long value) {
285         long oldValue = getLongValue(key, index);
286         putValue(key, index, new Long(value));
287         return oldValue;
288     }
289 
290     /***
291      * Set the value associated to the specified key.
292      * 
293      * @param key
294      *            Key of the values group.
295      * @param index
296      *            Index of the value.
297      * @param value
298      *            Value to set.
299      * @return Old value if exists; <tt>null</tt> otherwise.
300      */
301     protected final String putStringValue(Comparable key, int index, String value) {
302         String oldValue = getStringValue(key, index);
303         putValue(key, index, value);
304         return oldValue;
305     }
306 
307     /***
308      * Set the value associated to the specified key.
309      * 
310      * @param key
311      *            Key of the values group.
312      * @param index
313      *            Index of the value.
314      * @param value
315      *            Value to set.
316      */
317     private void putValue(Comparable key, int index, Object value) {
318         Object[] values = (Object[]) valuesGroups.get(key);
319         if (values == null) {
320             keys.add(key);
321             values = new Object[valuesNames.length];
322             valuesGroups.put(key, values);
323         }
324         values[index] = value;
325     }
326 
327     /***
328      * Returns the index of the value name.
329      * 
330      * @param valueName
331      *            Name of value.
332      * @return Index of the value name.
333      */
334     protected final int getValueNameIndex(String valueName) {
335         return ((Integer) valuesIndex.get(valueName)).intValue();
336     }
337 
338     /***
339      * Returns the value name.
340      * 
341      * @param index
342      *            Index of the value.
343      * @return Value name.
344      */
345     protected final String getValueName(int index) {
346         return valuesNames[index];
347     }
348 
349     /***
350      * Returns the values names.
351      * 
352      * @return Values names.
353      */
354     protected final String[] getValuesNames() {
355         return valuesNames;
356     }
357 
358     /***
359      * Change the specified old key to the new key.
360      * 
361      * @param oldKey
362      *            Key to change.
363      * @param newKey
364      *            New key.
365      */
366     public final void changeKey(Comparable oldKey, Comparable newKey) {
367         for (int i = 0; i < keys.size(); i++) {
368             if (keys.get(i).equals(oldKey)) {
369                 Object valuetomove = valuesGroups.remove(oldKey);
370                 valuesGroups.put(newKey, valuetomove);
371                 keys.add(i, newKey);
372                 keys.remove(i + 1);
373                 break;
374             }
375         }
376     }
377 
378     /***
379      * Remove the specified key of the analysis.
380      * 
381      * @param key
382      *            Key to remove.
383      */
384     protected final void removeKey(Comparable key) {
385         for (int i = 0; i < keys.size(); i++) {
386             if (keys.get(i).equals(key)) {
387                 keys.remove(i);
388                 valuesGroups.remove(key);
389                 break;
390             }
391         }
392     }
393 
394     /***
395      * Remove the key at the specified index of the analysis.
396      * 
397      * @param keyIndex
398      *            Key index to remove.
399      */
400     protected final void removeKey(int keyIndex) {
401         Comparable key = (Comparable) keys.remove(keyIndex);
402         valuesGroups.remove(key);
403     }
404 
405     /***
406      * Returns the number of values in the analysis.
407      * 
408      * @return Number of values.
409      */
410     public final long size() {
411         return valuesGroups.size();
412     }
413 
414     /***
415      * Sort results by the specified value form lowest to greatest.
416      * 
417      * @param index
418      *            Index of the value used to sort results.
419      */
420     protected final void sortByValueASC(int index) {
421         long nKeys = size();
422         for (int i = 0; i < nKeys - 1; i++) {
423             Comparable key = getKey(i);
424             for (int j = i + 1; j < nKeys; j++) {
425                 Comparable key2 = getKey(j);
426                 if (((Comparable) getObjectValue(key, index))
427                         .compareTo((Comparable) getObjectValue(key2, index)) > 0) {
428                     // Switch the keys order
429                     moveKeyAt(j, i);
430                     // Stop sorting this key and continues with the new key
431                     key = key2;
432                 }
433             }
434         }
435     }
436 
437     /***
438      * Sort results by the specified value form greatest to lowest.
439      * 
440      * @param index
441      *            Index of the value used to sort results.
442      */
443     protected final void sortByValueDESC(int index) {
444         long nKeys = size();
445         for (int i = 0; i < nKeys - 1; i++) {
446             Comparable key = getKey(i);
447             for (int j = i + 1; j < nKeys; j++) {
448                 Comparable key2 = getKey(j);
449                 if (((Comparable) getObjectValue(key, index))
450                         .compareTo((Comparable) getObjectValue(key2, index)) < 0) {
451                     // Switch the keys order
452                     moveKeyAt(j, i);
453                     // Stop sorting this key and continues with the new key
454                     key = key2;
455                 }
456             }
457         }
458     }
459 }