View Javadoc

    1   /*
    2    * $Id: Sorter.java 471756 2006-11-06 15:01:43Z husted $
    3    *
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
    10   * with the License.  You may obtain a copy of the License at
    11   *
    12   *  http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing,
    15   * software distributed under the License is distributed on an
    16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    17   * KIND, either express or implied.  See the License for the
    18   * specific language governing permissions and limitations
    19   * under the License.
    20   */
    21  package org.apache.struts2.util;
    22
    23  import java.util.Comparator;
    24
    25  import com.opensymphony.xwork2.util.ValueStack;
    26  import com.opensymphony.xwork2.util.ValueStackFactory;
    27
    28
    29  /***
    30   * Sorters. Utility sorters for use with the "sort" tag.
    31   *
    32   * @see org.apache.struts2.views.jsp.iterator.SortIteratorTag
    33   * @see SortIteratorFilter
    34   */
    35  public class Sorter {
    36
    37      public Comparator getAscending() {
    38          return new Comparator() {
    39              public int compare(Object o1, Object o2) {
    40                  if (o1 instanceof Comparable) {
    41                      return ((Comparable) o1).compareTo(o2);
    42                  } else {
    43                      String s1 = o1.toString();
    44                      String s2 = o2.toString();
    45
    46                      return s1.compareTo(s2);
    47                  }
    48              }
    49          };
    50      }
    51
    52      public Comparator getAscending(final String anExpression) {
    53          return new Comparator() {
    54              private ValueStack stack = ValueStackFactory.getFactory().createValueStack();
    55
    56              public int compare(Object o1, Object o2) {
    57                  // Get value for first object
    58                  stack.push(o1);
    59
    60                  Object v1 = stack.findValue(anExpression);
    61                  stack.pop();
    62
    63                  // Get value for second object
    64                  stack.push(o2);
    65
    66                  Object v2 = stack.findValue(anExpression);
    67                  stack.pop();
    68
    69                  // Ensure non-null
    70                  if (v1 == null) {
    71                      v1 = "";
    72                  }
    73
    74                  if (v2 == null) {
    75                      v2 = "";
    76                  }
    77
    78                  // Compare them
    79                  if (v1 instanceof Comparable && v1.getClass().equals(v2.getClass())) {
    80                      return ((Comparable) v1).compareTo(v2);
    81                  } else {
    82                      String s1 = v1.toString();
    83                      String s2 = v2.toString();
    84
    85                      return s1.compareTo(s2);
    86                  }
    87              }
    88          };
    89      }
    90
    91      public Comparator getComparator(String anExpression, boolean ascending) {
    92          if (ascending) {
    93              return getAscending(anExpression);
    94          } else {
    95              return getDescending(anExpression);
    96          }
    97      }
    98
    99      public Comparator getDescending() {
    100         return new Comparator() {
    101             public int compare(Object o1, Object o2) {
    102                 if (o2 instanceof Comparable) {
    103                     return ((Comparable) o2).compareTo(o1);
    104                 } else {
    105                     String s1 = o1.toString();
    106                     String s2 = o2.toString();
    107
    108                     return s2.compareTo(s1);
    109                 }
    110             }
    111         };
    112     }
    113
    114     public Comparator getDescending(final String anExpression) {
    115         return new Comparator() {
    116             private ValueStack stack = ValueStackFactory.getFactory().createValueStack();
    117
    118             public int compare(Object o1, Object o2) {
    119                 // Get value for first object
    120                 stack.push(o1);
    121
    122                 Object v1 = stack.findValue(anExpression);
    123                 stack.pop();
    124
    125                 // Get value for second object
    126                 stack.push(o2);
    127
    128                 Object v2 = stack.findValue(anExpression);
    129                 stack.pop();
    130
    131                 // Ensure non-null
    132                 if (v1 == null) {
    133                     v1 = "";
    134                 }
    135
    136                 if (v2 == null) {
    137                     v2 = "";
    138                 }
    139
    140                 // Compare them
    141                 if (v2 instanceof Comparable && v1.getClass().equals(v2.getClass())) {
    142                     return ((Comparable) v2).compareTo(v1);
    143                 } else {
    144                     String s1 = v1.toString();
    145                     String s2 = v2.toString();
    146
    147                     return s2.compareTo(s1);
    148                 }
    149             }
    150         };
    151     }
    152 }