View Javadoc

1   /*
2    * $Id: SortIteratorFilter.java 439747 2006-09-03 09:22:46Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.util;
19  
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.Comparator;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import org.apache.commons.logging.LogFactory;
27  
28  import com.opensymphony.xwork2.Action;
29  
30  
31  /***
32   * A bean that takes a source and comparator then attempt to sort the source
33   * utilizing the comparator. It is being used in SortIteratorTag.
34   *
35   * @see org.apache.struts2.views.jsp.iterator.SortIteratorTag
36   */
37  public class SortIteratorFilter extends IteratorFilterSupport implements Iterator, Action {
38  
39      Comparator comparator;
40      Iterator iterator;
41      List list;
42  
43      // Attributes ----------------------------------------------------
44      Object source;
45  
46  
47      public void setComparator(Comparator aComparator) {
48          this.comparator = aComparator;
49      }
50  
51      public List getList() {
52          return list;
53      }
54  
55      // Public --------------------------------------------------------
56      public void setSource(Object anIterator) {
57          source = anIterator;
58      }
59  
60      // Action implementation -----------------------------------------
61      public String execute() {
62          if (source == null) {
63              return ERROR;
64          } else {
65              try {
66                  if (!MakeIterator.isIterable(source)) {
67                      LogFactory.getLog(SortIteratorFilter.class.getName()).warn("Cannot create SortIterator for source " + source);
68  
69                      return ERROR;
70                  }
71  
72                  list = new ArrayList();
73  
74                  Iterator i = MakeIterator.convert(source);
75  
76                  while (i.hasNext()) {
77                      list.add(i.next());
78                  }
79  
80                  // Sort it
81                  Collections.sort(list, comparator);
82                  iterator = list.iterator();
83  
84                  return SUCCESS;
85              } catch (Exception e) {
86                  LogFactory.getLog(SortIteratorFilter.class.getName()).warn("Error creating sort iterator.", e);
87  
88                  return ERROR;
89              }
90          }
91      }
92  
93      // Iterator implementation ---------------------------------------
94      public boolean hasNext() {
95          return (source == null) ? false : iterator.hasNext();
96      }
97  
98      public Object next() {
99          return iterator.next();
100     }
101 
102     public void remove() {
103         throw new UnsupportedOperationException("Remove is not supported in SortIteratorFilter.");
104     }
105 }