1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.commsen.stopwatch.util;
17
18 import java.util.Comparator;
19
20 import com.commsen.stopwatch.Report;
21
22 public class ReportComparator implements Comparator {
23
24 public static final int GROUP = 0;
25 public static final int LABEL = 1;
26 public static final int COUNT = 2;
27 public static final int TIME_TOTAL = 3;
28 public static final int TIME_MIN = 4;
29 public static final int TIME_MAX = 5;
30 public static final int TIME_AVG = 6;
31
32 public static final int ASC = 1;
33 public static final int DESC = -1;
34
35 private int fieldCode;
36 private int orderCode;
37
38 public static final ReportComparator BY_GROUP_ASC = new ReportComparator(ReportComparator.GROUP,
39 ReportComparator.ASC);
40 public static final ReportComparator BY_GROUP_DESC = new ReportComparator(ReportComparator.GROUP,
41 ReportComparator.DESC);
42 public static final ReportComparator BY_LABEL_ASC = new ReportComparator(ReportComparator.LABEL,
43 ReportComparator.ASC);
44 public static final ReportComparator BY_LABEL_DESC = new ReportComparator(ReportComparator.LABEL,
45 ReportComparator.DESC);
46 public static final ReportComparator BY_COUNT_ASC = new ReportComparator(ReportComparator.COUNT,
47 ReportComparator.ASC);
48 public static final ReportComparator BY_COUNT_DESC = new ReportComparator(ReportComparator.COUNT,
49 ReportComparator.DESC);
50 public static final ReportComparator BY_MIN_TIME_ASC = new ReportComparator(ReportComparator.TIME_MIN,
51 ReportComparator.ASC);
52 public static final ReportComparator BY_MIN_TIME_DESC = new ReportComparator(ReportComparator.TIME_MIN,
53 ReportComparator.DESC);
54 public static final ReportComparator BY_MAX_TIME_ASC = new ReportComparator(ReportComparator.TIME_MAX,
55 ReportComparator.ASC);
56 public static final ReportComparator BY_MAX_TIME_DESC = new ReportComparator(ReportComparator.TIME_MAX,
57 ReportComparator.DESC);
58 public static final ReportComparator BY_AVG_TIME_ASC = new ReportComparator(ReportComparator.TIME_AVG,
59 ReportComparator.ASC);
60 public static final ReportComparator BY_AVG_TIME_DESC = new ReportComparator(ReportComparator.TIME_AVG,
61 ReportComparator.DESC);
62 public static final ReportComparator BY_TOTAL_TIME_ASC = new ReportComparator(ReportComparator.TIME_TOTAL,
63 ReportComparator.ASC);
64 public static final ReportComparator BY_TOTAL_TIME_DESC = new ReportComparator(ReportComparator.TIME_TOTAL,
65 ReportComparator.DESC);
66
67
68 public ReportComparator(int fieldCode, int orderCode) {
69 if (fieldCode < 0 || fieldCode > 6) throw new IllegalArgumentException("fieldCode should be between 0 and 6");
70 this.fieldCode = fieldCode;
71 if (orderCode != ASC && orderCode != DESC) throw new IllegalArgumentException("orderCode should be 1 or -1");
72 this.orderCode = orderCode;
73 }
74
75
76 public int compare(Object arg0, Object arg1) {
77 Report r1;
78 Report r2;
79
80 try {
81 r1 = (Report) arg0;
82 r2 = (Report) arg1;
83 } catch (ClassCastException e) {
84 return 0;
85 }
86
87 double d1 = r1.getTotalTime();
88 double d2 = r2.getTotalTime();
89
90 switch (fieldCode) {
91 case GROUP:
92 return orderCode * r1.getGroup().compareTo(r2.getGroup());
93 case LABEL:
94 return orderCode * r1.getLabel().compareTo(r2.getLabel());
95 case COUNT:
96 return orderCode * Double.compare(r1.getCount(), r2.getCount());
97
98
99 case TIME_TOTAL:
100 return orderCode * Double.compare(d1, d2);
101 case TIME_MIN:
102 return orderCode * Double.compare(r1.getMinTime(), r2.getMinTime());
103 case TIME_MAX:
104 return orderCode * Double.compare(r1.getMaxTime(), r2.getMaxTime());
105 case TIME_AVG:
106 return orderCode * Double.compare(r1.getAverageTime(), r2.getAverageTime());
107 default:
108 return 0;
109 }
110 }
111
112 }