01: /*
02: * Copyright (c) 2002-2003 by OpenSymphony
03: * All rights reserved.
04: */
05: package com.opensymphony.webwork.util;
06:
07: import com.opensymphony.xwork.Action;
08:
09: import java.util.ArrayList;
10: import java.util.Iterator;
11: import java.util.List;
12:
13: /**
14: * A bean that takes several iterators and outputs the merge of them. Used by
15: * MergeIteratorTag.
16: *
17: * @author Rickard Öberg (rickard@middleware-company.com)
18: * @version $Revision: 2338 $
19: * @see com.opensymphony.webwork.views.jsp.iterator.MergeIteratorTag
20: * @see com.opensymphony.webwork.components.MergeIterator
21: */
22: public class MergeIteratorFilter extends IteratorFilterSupport
23: implements Iterator, Action {
24:
25: List iterators = new ArrayList();
26:
27: // Attributes ----------------------------------------------------
28: List sources = new ArrayList();
29: int idx = 0;
30:
31: // Public --------------------------------------------------------
32: public void setSource(Object anIterator) {
33: sources.add(anIterator);
34: }
35:
36: // Action implementation -----------------------------------------
37: public String execute() {
38: // Make source transformations
39: for (int i = 0; i < sources.size(); i++) {
40: Object source = sources.get(i);
41: iterators.add(getIterator(source));
42: }
43:
44: return SUCCESS;
45: }
46:
47: // Iterator implementation ---------------------------------------
48: public boolean hasNext() {
49: while (iterators.size() > 0) {
50: if (((Iterator) iterators.get(idx)).hasNext()) {
51: return true;
52: } else {
53: iterators.remove(idx);
54:
55: if (iterators.size() > 0) {
56: idx = idx % iterators.size();
57: }
58: }
59: }
60:
61: return false;
62: }
63:
64: public Object next() {
65: try {
66: return ((Iterator) iterators.get(idx)).next();
67: } finally {
68: idx = (idx + 1) % iterators.size();
69: }
70: }
71:
72: public void remove() {
73: throw new UnsupportedOperationException(
74: "Remove is not supported in MergeIteratorFilter.");
75: }
76: }
|