org.apache.velocity.util.EnumerationIterator Java Examples

The following examples show how to use org.apache.velocity.util.EnumerationIterator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestableUberspect.java    From ApprovalTests.Java with Apache License 2.0 6 votes vote down vote up
public static Iterator<?> getStandardIterator(Object obj, Info i)
{
  if (obj.getClass().isArray())
  {
    return new ArrayIterator(obj);
  }
  else if (obj instanceof Collection)
  {
    return ((Collection<?>) obj).iterator();
  }
  else if (obj instanceof Map)
  {
    return ((Map<?, ?>) obj).values().iterator();
  }
  else if (obj instanceof Iterator)
  {
    return ((Iterator<?>) obj);
  }
  else if (obj instanceof Enumeration)
  { return new EnumerationIterator((Enumeration<?>) obj); }
  throw new VelocityParsingError("Could not determine type of iterator in " + "#foreach loop ", i);
}
 
Example #2
Source File: ClassUtils.java    From velocity-tools with Apache License 2.0 4 votes vote down vote up
/**
 * Retrieves an Iterator from or creates and Iterator for the specified object.
 * This method is almost entirely copied from Engine's UberspectImpl class.
 * @param obj the target obj
 * @return an iterator over the content of obj, or null if not found
 * @throws NoSuchMethodException if no iterator() method
 * @throws IllegalAccessException if iterator() method not callable
 * @throws InvocationTargetException if iterator() method throwed
 */
public static Iterator getIterator(Object obj)
    throws NoSuchMethodException, IllegalAccessException, InvocationTargetException
{
    if (obj.getClass().isArray())
    {
        return new ArrayIterator(obj);
    }
    else if (obj instanceof Collection)
    {
        return ((Collection) obj).iterator();
    }
    else if (obj instanceof Map)
    {
        return ((Map) obj).values().iterator();
    }
    else if (obj instanceof Iterator)
    {
        return ((Iterator) obj);
    }
    else if (obj instanceof Iterable)
    {
        return ((Iterable)obj).iterator();
    }
    else if (obj instanceof Enumeration)
    {
        return new EnumerationIterator((Enumeration) obj);
    }
    else
    {
        // look for an iterator() method to support
        // any user tools/DTOs that want to work in
        // foreach w/o implementing the Collection interface
        Method iter = obj.getClass().getMethod("iterator");
        if (Iterator.class.isAssignableFrom(iter.getReturnType()))
        {
            return (Iterator)iter.invoke(obj);
        }
        else
        {
            return null;
        }
    }
}