001: /*
002: * $Id: Sorter.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.util;
022:
023: import java.util.Comparator;
024:
025: import com.opensymphony.xwork2.util.ValueStack;
026: import com.opensymphony.xwork2.util.ValueStackFactory;
027:
028: /**
029: * Sorters. Utility sorters for use with the "sort" tag.
030: *
031: * @see org.apache.struts2.views.jsp.iterator.SortIteratorTag
032: * @see SortIteratorFilter
033: */
034: public class Sorter {
035:
036: public Comparator getAscending() {
037: return new Comparator() {
038: public int compare(Object o1, Object o2) {
039: if (o1 instanceof Comparable) {
040: return ((Comparable) o1).compareTo(o2);
041: } else {
042: String s1 = o1.toString();
043: String s2 = o2.toString();
044:
045: return s1.compareTo(s2);
046: }
047: }
048: };
049: }
050:
051: public Comparator getAscending(final String anExpression) {
052: return new Comparator() {
053: private ValueStack stack = ValueStackFactory.getFactory()
054: .createValueStack();
055:
056: public int compare(Object o1, Object o2) {
057: // Get value for first object
058: stack.push(o1);
059:
060: Object v1 = stack.findValue(anExpression);
061: stack.pop();
062:
063: // Get value for second object
064: stack.push(o2);
065:
066: Object v2 = stack.findValue(anExpression);
067: stack.pop();
068:
069: // Ensure non-null
070: if (v1 == null) {
071: v1 = "";
072: }
073:
074: if (v2 == null) {
075: v2 = "";
076: }
077:
078: // Compare them
079: if (v1 instanceof Comparable
080: && v1.getClass().equals(v2.getClass())) {
081: return ((Comparable) v1).compareTo(v2);
082: } else {
083: String s1 = v1.toString();
084: String s2 = v2.toString();
085:
086: return s1.compareTo(s2);
087: }
088: }
089: };
090: }
091:
092: public Comparator getComparator(String anExpression,
093: boolean ascending) {
094: if (ascending) {
095: return getAscending(anExpression);
096: } else {
097: return getDescending(anExpression);
098: }
099: }
100:
101: public Comparator getDescending() {
102: return new Comparator() {
103: public int compare(Object o1, Object o2) {
104: if (o2 instanceof Comparable) {
105: return ((Comparable) o2).compareTo(o1);
106: } else {
107: String s1 = o1.toString();
108: String s2 = o2.toString();
109:
110: return s2.compareTo(s1);
111: }
112: }
113: };
114: }
115:
116: public Comparator getDescending(final String anExpression) {
117: return new Comparator() {
118: private ValueStack stack = ValueStackFactory.getFactory()
119: .createValueStack();
120:
121: public int compare(Object o1, Object o2) {
122: // Get value for first object
123: stack.push(o1);
124:
125: Object v1 = stack.findValue(anExpression);
126: stack.pop();
127:
128: // Get value for second object
129: stack.push(o2);
130:
131: Object v2 = stack.findValue(anExpression);
132: stack.pop();
133:
134: // Ensure non-null
135: if (v1 == null) {
136: v1 = "";
137: }
138:
139: if (v2 == null) {
140: v2 = "";
141: }
142:
143: // Compare them
144: if (v2 instanceof Comparable
145: && v1.getClass().equals(v2.getClass())) {
146: return ((Comparable) v2).compareTo(v1);
147: } else {
148: String s1 = v1.toString();
149: String s2 = v2.toString();
150:
151: return s2.compareTo(s1);
152: }
153: }
154: };
155: }
156: }
|