001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.util;
006:
007: import com.opensymphony.xwork.util.OgnlValueStack;
008:
009: import java.util.Comparator;
010:
011: /**
012: * Sorters. Utility sorters for use with the "sort" tag.
013: *
014: * @author Rickard Öberg (rickard@middleware-company.com)
015: * @version $Revision: 1282 $
016: * @see com.opensymphony.webwork.views.jsp.iterator.SortIteratorTag
017: * @see SortIteratorFilter
018: */
019: public class Sorter {
020:
021: public Comparator getAscending() {
022: return new Comparator() {
023: public int compare(Object o1, Object o2) {
024: if (o1 instanceof Comparable) {
025: return ((Comparable) o1).compareTo(o2);
026: } else {
027: String s1 = o1.toString();
028: String s2 = o2.toString();
029:
030: return s1.compareTo(s2);
031: }
032: }
033: };
034: }
035:
036: public Comparator getAscending(final String anExpression) {
037: return new Comparator() {
038: private OgnlValueStack stack = new OgnlValueStack();
039:
040: public int compare(Object o1, Object o2) {
041: // Get value for first object
042: stack.push(o1);
043:
044: Object v1 = stack.findValue(anExpression);
045: stack.pop();
046:
047: // Get value for second object
048: stack.push(o2);
049:
050: Object v2 = stack.findValue(anExpression);
051: stack.pop();
052:
053: // Ensure non-null
054: if (v1 == null) {
055: v1 = "";
056: }
057:
058: if (v2 == null) {
059: v2 = "";
060: }
061:
062: // Compare them
063: if (v1 instanceof Comparable
064: && v1.getClass().equals(v2.getClass())) {
065: return ((Comparable) v1).compareTo(v2);
066: } else {
067: String s1 = v1.toString();
068: String s2 = v2.toString();
069:
070: return s1.compareTo(s2);
071: }
072: }
073: };
074: }
075:
076: public Comparator getComparator(String anExpression,
077: boolean ascending) {
078: if (ascending) {
079: return getAscending(anExpression);
080: } else {
081: return getDescending(anExpression);
082: }
083: }
084:
085: public Comparator getDescending() {
086: return new Comparator() {
087: public int compare(Object o1, Object o2) {
088: if (o2 instanceof Comparable) {
089: return ((Comparable) o2).compareTo(o1);
090: } else {
091: String s1 = o1.toString();
092: String s2 = o2.toString();
093:
094: return s2.compareTo(s1);
095: }
096: }
097: };
098: }
099:
100: public Comparator getDescending(final String anExpression) {
101: return new Comparator() {
102: private OgnlValueStack stack = new OgnlValueStack();
103:
104: public int compare(Object o1, Object o2) {
105: // Get value for first object
106: stack.push(o1);
107:
108: Object v1 = stack.findValue(anExpression);
109: stack.pop();
110:
111: // Get value for second object
112: stack.push(o2);
113:
114: Object 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
128: && v1.getClass().equals(v2.getClass())) {
129: return ((Comparable) v2).compareTo(v1);
130: } else {
131: String s1 = v1.toString();
132: String s2 = v2.toString();
133:
134: return s2.compareTo(s1);
135: }
136: }
137: };
138: }
139: }
|