KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > util > Sorter


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.util;
6
7 import com.opensymphony.xwork.util.OgnlValueStack;
8
9 import java.util.Comparator JavaDoc;
10
11
12 /**
13  * Sorters. Utility sorters for use with the "sort" tag.
14  *
15  * @author Rickard Öberg (rickard@middleware-company.com)
16  * @version $Revision: 1.8 $
17  * @see com.opensymphony.webwork.views.jsp.iterator.SortIteratorTag
18  * @see SortIteratorFilter
19  */

20 public class Sorter {
21     //~ Methods ////////////////////////////////////////////////////////////////
22

23     public Comparator JavaDoc getAscending() {
24         return new Comparator JavaDoc() {
25             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
26                 if (o1 instanceof Comparable JavaDoc) {
27                     return ((Comparable JavaDoc) o1).compareTo(o2);
28                 } else {
29                     String JavaDoc s1 = o1.toString();
30                     String JavaDoc s2 = o2.toString();
31
32                     return s1.compareTo(s2);
33                 }
34             }
35         };
36     }
37
38     public Comparator JavaDoc getAscending(final String JavaDoc anExpression) {
39         return new Comparator JavaDoc() {
40             private OgnlValueStack stack = new OgnlValueStack();
41
42             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
43                 // Get value for first object
44
stack.push(o1);
45
46                 Object JavaDoc v1 = stack.findValue(anExpression);
47                 stack.pop();
48
49                 // Get value for second object
50
stack.push(o2);
51
52                 Object JavaDoc v2 = stack.findValue(anExpression);
53                 stack.pop();
54
55                 // Ensure non-null
56
if (v1 == null) {
57                     v1 = "";
58                 }
59
60                 if (v2 == null) {
61                     v2 = "";
62                 }
63
64                 // Compare them
65
if (v1 instanceof Comparable JavaDoc && v1.getClass().equals(v2.getClass())) {
66                     return ((Comparable JavaDoc) v1).compareTo(v2);
67                 } else {
68                     String JavaDoc s1 = v1.toString();
69                     String JavaDoc s2 = v2.toString();
70
71                     return s1.compareTo(s2);
72                 }
73             }
74         };
75     }
76
77     public Comparator JavaDoc getComparator(String JavaDoc anExpression, boolean ascending) {
78         if (ascending) {
79             return getAscending(anExpression);
80         } else {
81             return getDescending(anExpression);
82         }
83     }
84
85     public Comparator JavaDoc getDescending() {
86         return new Comparator JavaDoc() {
87             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
88                 if (o2 instanceof Comparable JavaDoc) {
89                     return ((Comparable JavaDoc) o2).compareTo(o1);
90                 } else {
91                     String JavaDoc s1 = o1.toString();
92                     String JavaDoc s2 = o2.toString();
93
94                     return s2.compareTo(s1);
95                 }
96             }
97         };
98     }
99
100     public Comparator JavaDoc getDescending(final String JavaDoc anExpression) {
101         return new Comparator JavaDoc() {
102             private OgnlValueStack stack = new OgnlValueStack();
103
104             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
105                 // Get value for first object
106
stack.push(o1);
107
108                 Object JavaDoc v1 = stack.findValue(anExpression);
109                 stack.pop();
110
111                 // Get value for second object
112
stack.push(o2);
113
114                 Object JavaDoc v2 = stack.findValue(anExpression);
115                 stack.pop();
116
117                 // Ensure non-null
118
if (v1 == null) {
119                     v1 = "";
120                 }
121
122                 if (v2 == null) {
123                     v2 = "";
124                 }
125
126                 // Compare them
127
if (v2 instanceof Comparable JavaDoc && v1.getClass().equals(v2.getClass())) {
128                     return ((Comparable JavaDoc) v2).compareTo(v1);
129                 } else {
130                     String JavaDoc s1 = v1.toString();
131                     String JavaDoc s2 = v2.toString();
132
133                     return s2.compareTo(s1);
134                 }
135             }
136         };
137     }
138 }
139
Popular Tags