FileDocCategorySizeDatePackage
SubsetIteratorFilter.javaAPI DocExample5508Mon Jul 23 13:26:56 BST 2007org.apache.struts2.util

SubsetIteratorFilter

public class SubsetIteratorFilter extends IteratorFilterSupport implements com.opensymphony.xwork2.Action, Iterator
A bean that takes an iterator and outputs a subset of it.

Fields Summary
private static final Log
_log
Iterator
iterator
Object
source
int
count
int
currentCount
Decider
decider
int
start
Constructors Summary
Methods Summary
protected booleandecide(java.lang.Object element)

        if (decider != null) {
            try {
                boolean okToAdd = decider.decide(element);
                return okToAdd;
            }
            catch(Exception e) {
                _log.warn("decider ["+decider+"] encountered an error while decide adding element ["+element+"], element will be ignored, it will not appeared in subseted iterator", e);
                return false;
            }
        }
        return true;
    
public java.lang.Stringexecute()

        if (source == null) {
            LogFactory.getLog(SubsetIteratorFilter.class.getName()).warn("Source is null returning empty set.");

            return ERROR;
        }

        // Make source transformations
        source = getIterator(source);

        // Calculate iterator filter
        if (source instanceof Iterator) {
            iterator = (Iterator) source;


            // Read away <start> items
            for (int i = 0; (i < start) && iterator.hasNext(); i++) {
                iterator.next();
            }


            // now let Decider decide if element should be added (if a decider exist)
            if (decider != null) {
                List list = new ArrayList();
                while(iterator.hasNext()) {
                    Object currentElement = iterator.next();
                    if (decide(currentElement)) {
                        list.add(currentElement);
                    }
                }
                iterator = list.iterator();
            }

        } else if (source.getClass().isArray()) {
            ArrayList list = new ArrayList(((Object[]) source).length);
            Object[] objects = (Object[]) source;
            int len = objects.length;

            if (count >= 0) {
                len = start + count;
                if (len > objects.length) {
                    len = objects.length;
                }
            }

            for (int j = start; j < len; j++) {
                if (decide(objects[j])) {
                    list.add(objects[j]);
                }
            }

            count = -1; // Don't have to check this in the iterator code
            iterator = list.iterator();
        }

        if (iterator == null) {
            throw new IllegalArgumentException("Source is not an iterator:" + source);
        }

        return SUCCESS;
    
public booleanhasNext()

        return (iterator == null) ? false : (iterator.hasNext() && ((count < 0) || (currentCount < count)));
    
public java.lang.Objectnext()

        currentCount++;

        return iterator.next();
    
public voidremove()

        iterator.remove();
    
public voidsetCount(int aCount)



        
        this.count = aCount;
    
public voidsetDecider(org.apache.struts2.util.SubsetIteratorFilter$Decider aDecider)

        this.decider = aDecider;
    
public voidsetSource(java.lang.Object anIterator)

        source = anIterator;
    
public voidsetStart(int aStart)

        this.start = aStart;