View Javadoc

1   /*
2    *
3    * Copyright 2006 Commsen International
4    * 
5    * Licensed under the Common Public License, Version 1.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    *      http://www.opensource.org/licenses/cpl1.0.txt
10   * 
11   * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
12   * EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS 
13   * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
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  				// case TIME_TOTAL: return orderCode * Double.compare(r1.getTotalTime(),
98  				// r2.getTotalTime());
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 }