%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
net.sf.infrared.web.util.BeanComparator |
|
|
1 | /* |
|
2 | * Copyright 2005 Tavant Technologies and Contributors |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
15 | * |
|
16 | * |
|
17 | * |
|
18 | * Original Author: kamal.govindraj (Tavant Technologies) |
|
19 | * Contributor(s): -; |
|
20 | * |
|
21 | */ |
|
22 | package net.sf.infrared.web.util; |
|
23 | ||
24 | import org.apache.commons.beanutils.MethodUtils; |
|
25 | ||
26 | import java.lang.reflect.InvocationTargetException; |
|
27 | import java.util.Comparator; |
|
28 | ||
29 | /** |
|
30 | * A simple comparator implementation that enables comparing two |
|
31 | * bean instances based on a specified property. |
|
32 | */ |
|
33 | public class BeanComparator implements Comparator |
|
34 | { |
|
35 | 0 | String methodName = null; |
36 | 0 | boolean ascending = true; |
37 | public BeanComparator(String methodName, boolean ascending) |
|
38 | 0 | { |
39 | 0 | this.methodName = methodName; |
40 | 0 | this.ascending = ascending; |
41 | 0 | } |
42 | ||
43 | public int compare(Object o1, Object o2) |
|
44 | { |
|
45 | 0 | Object fieldOne = null; |
46 | 0 | Object fieldTwo = null; |
47 | try |
|
48 | { |
|
49 | 0 | fieldOne = MethodUtils.invokeExactMethod(o1,methodName,null); |
50 | 0 | fieldTwo = MethodUtils.invokeExactMethod(o2,methodName,null); |
51 | } |
|
52 | 0 | catch (NoSuchMethodException e) |
53 | { |
|
54 | 0 | throw new RuntimeException(e); |
55 | } |
|
56 | 0 | catch (IllegalAccessException e) |
57 | { |
|
58 | 0 | throw new RuntimeException(e); |
59 | } |
|
60 | 0 | catch (IllegalArgumentException e) |
61 | { |
|
62 | 0 | throw new RuntimeException(e); |
63 | } |
|
64 | 0 | catch (InvocationTargetException e) |
65 | { |
|
66 | 0 | throw new RuntimeException(e); |
67 | 0 | } |
68 | ||
69 | 0 | if (fieldOne != null |
70 | && fieldTwo != null |
|
71 | && fieldOne.getClass().equals(fieldTwo.getClass()) |
|
72 | && fieldOne instanceof Comparable |
|
73 | && fieldTwo instanceof Comparable) |
|
74 | { |
|
75 | 0 | int retValue = ((Comparable)fieldOne).compareTo(fieldTwo); |
76 | 0 | return (ascending ? retValue : (retValue*-1)); |
77 | } |
|
78 | else |
|
79 | { |
|
80 | 0 | throw new IllegalArgumentException("Error comparing object " |
81 | + o1.toString() |
|
82 | + " with " |
|
83 | + o2.toString() ); |
|
84 | } |
|
85 | } |
|
86 | ||
87 | public void setAscending(boolean ascending) |
|
88 | { |
|
89 | 0 | this.ascending = ascending; |
90 | 0 | } |
91 | ||
92 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |