View Javadoc

    1   /*
    2    * $Id: SortIteratorFilter.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.ArrayList;
    24  import java.util.Collections;
    25  import java.util.Comparator;
    26  import java.util.Iterator;
    27  import java.util.List;
    28
    29  import org.apache.commons.logging.LogFactory;
    30
    31  import com.opensymphony.xwork2.Action;
    32
    33
    34  /***
    35   * A bean that takes a source and comparator then attempt to sort the source
    36   * utilizing the comparator. It is being used in SortIteratorTag.
    37   *
    38   * @see org.apache.struts2.views.jsp.iterator.SortIteratorTag
    39   */
    40  public class SortIteratorFilter extends IteratorFilterSupport implements Iterator, Action {
    41
    42      Comparator comparator;
    43      Iterator iterator;
    44      List list;
    45
    46      // Attributes ----------------------------------------------------
    47      Object source;
    48
    49
    50      public void setComparator(Comparator aComparator) {
    51          this.comparator = aComparator;
    52      }
    53
    54      public List getList() {
    55          return list;
    56      }
    57
    58      // Public --------------------------------------------------------
    59      public void setSource(Object anIterator) {
    60          source = anIterator;
    61      }
    62
    63      // Action implementation -----------------------------------------
    64      public String execute() {
    65          if (source == null) {
    66              return ERROR;
    67          } else {
    68              try {
    69                  if (!MakeIterator.isIterable(source)) {
    70                      LogFactory.getLog(SortIteratorFilter.class.getName()).warn("Cannot create SortIterator for source " + source);
    71
    72                      return ERROR;
    73                  }
    74
    75                  list = new ArrayList();
    76
    77                  Iterator i = MakeIterator.convert(source);
    78
    79                  while (i.hasNext()) {
    80                      list.add(i.next());
    81                  }
    82
    83                  // Sort it
    84                  Collections.sort(list, comparator);
    85                  iterator = list.iterator();
    86
    87                  return SUCCESS;
    88              } catch (Exception e) {
    89                  LogFactory.getLog(SortIteratorFilter.class.getName()).warn("Error creating sort iterator.", e);
    90
    91                  return ERROR;
    92              }
    93          }
    94      }
    95
    96      // Iterator implementation ---------------------------------------
    97      public boolean hasNext() {
    98          return (source == null) ? false : iterator.hasNext();
    99      }
    100
    101     public Object next() {
    102         return iterator.next();
    103     }
    104
    105     public void remove() {
    106         throw new UnsupportedOperationException("Remove is not supported in SortIteratorFilter.");
    107     }
    108 }