KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > comparators > ContentComparator


1 package com.dotmarketing.comparators;
2
3 import java.util.Comparator JavaDoc;
4 import java.util.Date JavaDoc;
5
6 import org.apache.commons.beanutils.PropertyUtils;
7
8 import com.dotmarketing.portlets.contentlet.model.Contentlet;
9
10 /**
11  * @author Maria & David (Fat contentlet adaptations)
12  *
13  */

14 public class ContentComparator implements Comparator JavaDoc<Contentlet> {
15
16     private String JavaDoc orderType = "";
17
18     private String JavaDoc orderField = "";
19     
20     public ContentComparator(String JavaDoc orderField, String JavaDoc orderType) {
21         super();
22         this.orderType = orderType;
23         this.orderField = orderField;
24     }
25
26     public ContentComparator(String JavaDoc orderFieldAndType) {
27         super();
28         String JavaDoc[] values = orderFieldAndType.split(" ");
29         if (values.length > 0)
30             this.orderField = values[0];
31         
32         if (values.length > 1)
33             this.orderType = values[1];
34         else
35             this.orderType = "asc";
36     }
37
38     public int compare(Contentlet w1, Contentlet w2) {
39
40         try {
41             Object JavaDoc value1 = PropertyUtils.getSimpleProperty(w1, orderField);
42             Object JavaDoc value2 = PropertyUtils.getSimpleProperty(w2, orderField);
43             
44             int ret = 0;
45             
46             if (value1 == null || value2 == null) {
47                 return ret;
48             } else if (value1 instanceof Integer JavaDoc) {
49                 ret = ((Integer JavaDoc)value1).compareTo((Integer JavaDoc)value2);
50             } else if (value1 instanceof Long JavaDoc) {
51                 ret = ((Long JavaDoc)value1).compareTo((Long JavaDoc)value2);
52             } else if (value1 instanceof Date JavaDoc) {
53                 ret = ((Date JavaDoc)value1).compareTo((Date JavaDoc)value2);
54             } else if (value1 instanceof String JavaDoc) {
55                 ret = ((String JavaDoc)value1).compareTo((String JavaDoc)value2);
56             } else if (value1 instanceof Float JavaDoc) {
57                 ret = ((Float JavaDoc)value1).compareTo((Float JavaDoc)value2);
58             } else if (value1 instanceof Boolean JavaDoc) {
59                 ret = ((Boolean JavaDoc)value1).compareTo((Boolean JavaDoc)value2);
60             }
61
62             if (orderType.equals("asc")) {
63                 return ret;
64             }
65
66             return ret * -1;
67
68         } catch (Exception JavaDoc e) {
69
70         }
71         return 0;
72     }
73 }
Popular Tags