FileDocCategorySizeDatePackage
MergeIterator.javaAPI DocExample6434Mon Jul 23 13:26:36 BST 2007org.apache.struts2.components

MergeIterator

public class MergeIterator extends Component implements org.apache.struts2.components.Param.UnnamedParametric

Component for MergeIteratorTag, which job is to merge iterators and successive call to the merged iterator will cause each merge iterator to have a chance to expose its element, subsequently next call will allow the next iterator to expose its element. Once the last iterator is done exposing its element, the first iterator is allowed to do so again (unless it is exhausted of entries).

Internally the task are delegated to MergeIteratorFilter

Example if there are 3 lists being merged, each list have 3 entries, the following will be the logic.

  1. Display first element of the first list
  2. Display first element of the second list
  3. Display first element of the third list
  4. Display second element of the first list
  5. Display second element of the second list
  6. Display second element of the third list
  7. Display third element of the first list
  8. Display thrid element of the second list
  9. Display third element of the thrid list
  • id (String) - the id where the resultant merged iterator will be stored in the stack's context
public class MergeIteratorTagAction extends ActionSupport { private List myList1; private List myList2; private List myList3; public List getMyList1() { return myList1; } public List getMyList2() { return myList2; } public List getMyList3() { return myList3; } public String execute() throws Exception { myList1 = new ArrayList(); myList1.add("1"); myList1.add("2"); myList1.add("3"); myList2 = new ArrayList(); myList2.add("a"); myList2.add("b"); myList2.add("c"); myList3 = new ArrayList(); myList3.add("A"); myList3.add("B"); myList3.add("C"); return "done"; } } <s:merge id="myMergedIterator1"> <s:param value="%{myList1}" /> <s:param value="%{myList2}" /> <s:param value="%{myList3}" /> </s:merge> <s:iterator value="%{#myMergedIterator1}"> <s:property /> </s:iterator> This wil generate "1aA2bB3cC".
see
org.apache.struts2.util.MergeIteratorFilter
see
org.apache.struts2.views.jsp.iterator.MergeIteratorTag

Fields Summary
private static final Log
_log
private org.apache.struts2.util.MergeIteratorFilter
mergeIteratorFilter
private List
_parameters
Constructors Summary
public MergeIterator(com.opensymphony.xwork2.util.ValueStack stack)


       
        super(stack);
    
Methods Summary
public voidaddParameter(java.lang.Object value)

        _parameters.add(value);
    
public booleanend(java.io.Writer writer, java.lang.String body)


        for (Iterator parametersIterator = _parameters.iterator(); parametersIterator.hasNext(); ) {
            Object iteratorEntryObj = parametersIterator.next();
            if (! MakeIterator.isIterable(iteratorEntryObj)) {
                _log.warn("param with value resolved as "+iteratorEntryObj+" cannot be make as iterator, it will be ignored and hence will not appear in the merged iterator");
                continue;
            }
            mergeIteratorFilter.setSource(MakeIterator.convert(iteratorEntryObj));
        }

        mergeIteratorFilter.execute();

        // if id exists, we put it in the stack's context
        if (getId() != null && getId().length() > 0) {
            getStack().getContext().put(getId(), mergeIteratorFilter);
        }

        mergeIteratorFilter = null;

        return super.end(writer, body);
    
public voidsetId(java.lang.String id)

        super.setId(id);
    
public booleanstart(java.io.Writer writer)


        mergeIteratorFilter = new MergeIteratorFilter();
        _parameters = new ArrayList();

        return super.start(writer);