001: /*
002: * $Id: SortIteratorFilter.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.ArrayList;
024: import java.util.Collections;
025: import java.util.Comparator;
026: import java.util.Iterator;
027: import java.util.List;
028:
029: import org.apache.commons.logging.LogFactory;
030:
031: import com.opensymphony.xwork2.Action;
032:
033: /**
034: * A bean that takes a source and comparator then attempt to sort the source
035: * utilizing the comparator. It is being used in SortIteratorTag.
036: *
037: * @see org.apache.struts2.views.jsp.iterator.SortIteratorTag
038: */
039: public class SortIteratorFilter extends IteratorFilterSupport implements
040: Iterator, Action {
041:
042: Comparator comparator;
043: Iterator iterator;
044: List list;
045:
046: // Attributes ----------------------------------------------------
047: Object source;
048:
049: public void setComparator(Comparator aComparator) {
050: this .comparator = aComparator;
051: }
052:
053: public List getList() {
054: return list;
055: }
056:
057: // Public --------------------------------------------------------
058: public void setSource(Object anIterator) {
059: source = anIterator;
060: }
061:
062: // Action implementation -----------------------------------------
063: public String execute() {
064: if (source == null) {
065: return ERROR;
066: } else {
067: try {
068: if (!MakeIterator.isIterable(source)) {
069: LogFactory.getLog(
070: SortIteratorFilter.class.getName()).warn(
071: "Cannot create SortIterator for source "
072: + source);
073:
074: return ERROR;
075: }
076:
077: list = new ArrayList();
078:
079: Iterator i = MakeIterator.convert(source);
080:
081: while (i.hasNext()) {
082: list.add(i.next());
083: }
084:
085: // Sort it
086: Collections.sort(list, comparator);
087: iterator = list.iterator();
088:
089: return SUCCESS;
090: } catch (Exception e) {
091: LogFactory.getLog(SortIteratorFilter.class.getName())
092: .warn("Error creating sort iterator.", e);
093:
094: return ERROR;
095: }
096: }
097: }
098:
099: // Iterator implementation ---------------------------------------
100: public boolean hasNext() {
101: return (source == null) ? false : iterator.hasNext();
102: }
103:
104: public Object next() {
105: return iterator.next();
106: }
107:
108: public void remove() {
109: throw new UnsupportedOperationException(
110: "Remove is not supported in SortIteratorFilter.");
111: }
112: }
|