Example usage for org.apache.commons.collections IteratorUtils asIterator

List of usage examples for org.apache.commons.collections IteratorUtils asIterator

Introduction

In this page you can find the example usage for org.apache.commons.collections IteratorUtils asIterator.

Prototype

public static Iterator asIterator(Enumeration enumeration) 

Source Link

Document

Gets an iterator that provides an iterator view of the given enumeration.

Usage

From source file:info.magnolia.cms.i18n.DefaultMessagesImpl.java

/**
 * Iterate over the keys
 */
public Iterator keys() {
    return IteratorUtils.asIterator(this.getBundle().getKeys());
}

From source file:com.projity.grouping.core.hierarchy.AbstractMutableNodeHierarchy.java

public Iterator iterator(Node rootNode) {
    NodeBridge r;/*from   w  w  w  .j a v a  2s  .  c om*/
    if (rootNode != null && rootNode instanceof NodeBridge)
        r = (NodeBridge) rootNode;
    else
        r = (NodeBridge) getRoot();
    return IteratorUtils.asIterator(r.preorderEnumeration());
}

From source file:net.sourceforge.fenixedu.presentationTier.TagLib.OptionsTag.java

/**
 * Return an iterator for the option labels or values, based on our
 * configured properties.//from w  w  w . jav a2s .c o m
 * 
 * @param name
 *            Name of the bean attribute (if any)
 * @param property
 *            Name of the bean property (if any)
 * 
 * @exception JspException
 *                if an error occurs
 */
protected Iterator getIterator(String name, String property) throws JspException {

    // Identify the bean containing our collection
    String beanName = name;
    if (beanName == null) {
        beanName = Constants.BEAN_KEY;
    }
    Object bean = pageContext.findAttribute(beanName);
    if (bean == null) {
        throw new JspException(messages.getMessage("getter.bean", beanName));
    }

    // Identify the collection itself
    Object collection = bean;
    if (property != null) {
        try {
            collection = PropertyUtils.getProperty(bean, property);
            if (collection == null) {
                throw new JspException(messages.getMessage("getter.property", property));
            }
        } catch (IllegalAccessException e) {
            throw new JspException(messages.getMessage("getter.access", property, name));
        } catch (InvocationTargetException e) {
            Throwable t = e.getTargetException();
            throw new JspException(messages.getMessage("getter.result", property, t.toString()));
        } catch (NoSuchMethodException e) {
            throw new JspException(messages.getMessage("getter.method", property, name));
        }
    }

    // Construct and return an appropriate iterator
    if (collection.getClass().isArray()) {
        collection = Arrays.asList((Object[]) collection);
    }
    if (collection instanceof Collection) {
        return (((Collection) collection).iterator());
    } else if (collection instanceof Iterator) {
        return ((Iterator) collection);
    } else if (collection instanceof Map) {
        return (((Map) collection).entrySet().iterator());
    } else if (collection instanceof Enumeration) {
        return (IteratorUtils.asIterator((Enumeration) collection));
    } else {
        throw new JspException(messages.getMessage("optionsTag.iterator", collection.toString()));
    }

}

From source file:org.apache.struts.taglib.logic.IterateTag.java

/**
 * Construct an iterator for the specified collection, and begin
 * looping through the body once per element.
 *
 * @exception JspException if a JSP exception has occurred
 *///from  w w  w .j av  a  2  s. c om
public int doStartTag() throws JspException {

    // Acquire the collection we are going to iterate over
    Object collection = this.collection;
    if (collection == null) {
        collection = RequestUtils.lookup(pageContext, name, property, scope);
    }

    if (collection == null) {
        JspException e = new JspException(messages.getMessage("iterate.collection"));
        RequestUtils.saveException(pageContext, e);
        throw e;
    }

    // Construct an iterator for this collection
    if (collection.getClass().isArray()) {
        try {
            // If we're lucky, it is an array of objects
            // that we can iterate over with no copying
            iterator = Arrays.asList((Object[]) collection).iterator();
        } catch (ClassCastException e) {
            // Rats -- it is an array of primitives
            int length = Array.getLength(collection);
            ArrayList c = new ArrayList(length);
            for (int i = 0; i < length; i++) {
                c.add(Array.get(collection, i));
            }
            iterator = c.iterator();
        }
    } else if (collection instanceof Collection) {
        iterator = ((Collection) collection).iterator();
    } else if (collection instanceof Iterator) {
        iterator = (Iterator) collection;
    } else if (collection instanceof Map) {
        iterator = ((Map) collection).entrySet().iterator();
    } else if (collection instanceof Enumeration) {
        iterator = IteratorUtils.asIterator((Enumeration) collection);
    } else {
        JspException e = new JspException(messages.getMessage("iterate.iterator"));
        RequestUtils.saveException(pageContext, e);
        throw e;
    }

    // Calculate the starting offset
    if (offset == null) {
        offsetValue = 0;
    } else {
        try {
            offsetValue = Integer.parseInt(offset);
        } catch (NumberFormatException e) {
            Integer offsetObject = (Integer) RequestUtils.lookup(pageContext, offset, null);
            if (offsetObject == null) {
                offsetValue = 0;
            } else {
                offsetValue = offsetObject.intValue();
            }
        }
    }
    if (offsetValue < 0) {
        offsetValue = 0;
    }

    // Calculate the rendering length
    if (length == null) {
        lengthValue = 0;
    } else {
        try {
            lengthValue = Integer.parseInt(length);
        } catch (NumberFormatException e) {
            Integer lengthObject = (Integer) RequestUtils.lookup(pageContext, length, null);
            if (lengthObject == null) {
                lengthValue = 0;
            } else {
                lengthValue = lengthObject.intValue();
            }
        }
    }
    if (lengthValue < 0) {
        lengthValue = 0;
    }
    lengthCount = 0;

    // Skip the leading elements up to the starting offset
    for (int i = 0; i < offsetValue; i++) {
        if (iterator.hasNext()) {
            iterator.next();
        }
    }

    // Store the first value and evaluate, or skip the body if none
    if (iterator.hasNext()) {
        Object element = iterator.next();
        if (element == null) {
            pageContext.removeAttribute(id);
        } else {
            pageContext.setAttribute(id, element);
        }
        lengthCount++;
        started = true;
        if (indexId != null) {
            pageContext.setAttribute(indexId, new Integer(getIndex()));
        }
        return (EVAL_BODY_TAG);
    } else {
        return (SKIP_BODY);
    }

}

From source file:org.icefaces.ace.model.tree.NodeEnumerationToNodeEntryIterator.java

public NodeEnumerationToNodeEntryIterator(KeySegmentConverter converter, NodeKey parentKey,
        Enumeration<V> children) {
    iter = (Iterator<V>) IteratorUtils.asIterator(children);
    this.converter = converter;
    this.parentKey = parentKey;
}

From source file:org.mule.transports.soap.glue.GlueMessageAdapter.java

public GlueMessageAdapter(Object message) {
    if (message instanceof GlueMessageHolder) {
        GlueMessageHolder holder = (GlueMessageHolder) message;
        this.message = holder.getMessage();
        Iterator iter = IteratorUtils.asIterator(holder.getService().getContext().getPropertyNames());
        String key;/*from  w  w w .  j a  v a 2  s . c o  m*/
        while (iter.hasNext()) {
            key = iter.next().toString();
            setProperty(key, holder.getService().getContext().getProperty(key));
        }
    } else {
        this.message = message;
    }
    String value = (String) ThreadContext.removeProperty(MuleProperties.MULE_REPLY_TO_PROPERTY);
    if (value != null) {
        setReplyTo(value);
    }
    value = (String) ThreadContext.removeProperty(MuleProperties.MULE_CORRELATION_ID_PROPERTY);
    if (value != null) {
        setCorrelationId(value);
    }

    value = (String) ThreadContext.removeProperty(MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY);
    if (value != null && !"-1".equals(value)) {
        setCorrelationSequence(Integer.parseInt(value));
    }
    value = (String) ThreadContext.removeProperty(MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY);
    if (value != null && !"-1".equals(value)) {
        setCorrelationGroupSize(Integer.parseInt(value));
    }
}

From source file:org.springmodules.validation.valang.predicates.AbstractPropertyPredicate.java

protected final Iterator getIterator(Object literal) {
    if (literal instanceof Collection) {
        return ((Collection) literal).iterator();
    } else if (literal instanceof Iterator) {
        return (Iterator) literal;
    } else if (literal instanceof Enumeration) {
        return IteratorUtils.asIterator((Enumeration) literal);
    } else if (literal instanceof Object[]) {
        return IteratorUtils.arrayIterator((Object[]) literal);
    } else {/* www .j  a va2 s. c o  m*/
        throw new ClassCastException("Could not convert literal value to iterator!");
    }
}

From source file:org.springmodules.validation.valang.predicates.AbstractPropertyPredicate.java

protected final Object[] getArray(Object literal) {
    if (literal instanceof Collection) {
        return ((Collection) literal).toArray();
    } else if (literal instanceof Iterator) {
        return IteratorUtils.toArray((Iterator) literal);
    } else if (literal instanceof Enumeration) {
        return IteratorUtils.toArray(IteratorUtils.asIterator((Enumeration) literal));
    } else if (literal instanceof Object[]) {
        return (Object[]) literal;
    } else {//from ww w. j  a  v  a2 s  .  co m
        throw new ClassCastException("Could not convert literal value to array!");
    }
}