01: /*
02: * $Id: MergeIteratorFilter.java 471756 2006-11-06 15:01:43Z husted $
03: *
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "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.Iterator;
25: import java.util.List;
26:
27: import com.opensymphony.xwork2.Action;
28:
29: /**
30: * A bean that takes several iterators and outputs the merge of them. Used by
31: * MergeIteratorTag.
32: *
33: * @see org.apache.struts2.views.jsp.iterator.MergeIteratorTag
34: * @see org.apache.struts2.components.MergeIterator
35: */
36: public class MergeIteratorFilter extends IteratorFilterSupport
37: implements Iterator, Action {
38:
39: List iterators = new ArrayList();
40:
41: // Attributes ----------------------------------------------------
42: List sources = new ArrayList();
43: int idx = 0;
44:
45: // Public --------------------------------------------------------
46: public void setSource(Object anIterator) {
47: sources.add(anIterator);
48: }
49:
50: // Action implementation -----------------------------------------
51: public String execute() {
52: // Make source transformations
53: for (int i = 0; i < sources.size(); i++) {
54: Object source = sources.get(i);
55: iterators.add(getIterator(source));
56: }
57:
58: return SUCCESS;
59: }
60:
61: // Iterator implementation ---------------------------------------
62: public boolean hasNext() {
63: while (iterators.size() > 0) {
64: if (((Iterator) iterators.get(idx)).hasNext()) {
65: return true;
66: } else {
67: iterators.remove(idx);
68:
69: if (iterators.size() > 0) {
70: idx = idx % iterators.size();
71: }
72: }
73: }
74:
75: return false;
76: }
77:
78: public Object next() {
79: try {
80: return ((Iterator) iterators.get(idx)).next();
81: } finally {
82: idx = (idx + 1) % iterators.size();
83: }
84: }
85:
86: public void remove() {
87: throw new UnsupportedOperationException(
88: "Remove is not supported in MergeIteratorFilter.");
89: }
90: }
|