/** * <p>Tells the LoopTool to watch the specified Array, Collection, Map, * Iterator, Iterable, Enumeration or POJO with an iterator() method * while the template iterates over the values within it. * </p> * <p>Under the covers, this is returning an iterable wrapper that * is also pushed onto this tool's stack. That allows this tool to * know which iterator to give later commands (i.e. stop() or skip()). * </p> * @param obj an object that Velocity's #foreach directive can iterate over * @return a {@link ManagedIterator} that this tool instance will track */ public ManagedIterator watch(Object obj) { Iterator iterator = getIterator(obj); if (iterator == null) { return null; } ManagedIterator managed = manage(iterator, null); iterators.push(managed); this.last = managed; return managed; }
/** * This is just like {@link #watch(Object)} except that it also takes * a name which is given to the {@link ManagedIterator} that is returned. * This allows the user to send stop or skip commands to that specific * iterator even when there are nested iterators within it that are being * watched. If the given name is {@code null}, then this will return * {@code null} even if the object can be watched. Provided names cannot * be {@code null}. * @param obj an object that Velocity's #foreach directive can iterate over * @param name loop name * @return a {@link ManagedIterator} that this tool instance will track * @see #watch(Object) */ public ManagedIterator watch(Object obj, String name) { // don't mess around with null names if (name == null) { return null; } Iterator iterator = getIterator(obj); if (iterator == null) { return null; } ManagedIterator managed = manage(iterator, name); iterators.push(managed); this.last = managed; return managed; }