类 IteratorTool

java.lang.Object
org.apache.velocity.tools.generic.IteratorTool
所有已实现的接口:
Iterator

@DefaultKey("mill") @Deprecated public class IteratorTool extends Object implements Iterator
已过时。
Use LoopTool instead

A convenience tool to use with #foreach loops. It wraps a list to let the designer specify a condition to terminate the loop, and reuse the same list in different loops.

Example of use:

  Java
  ----
  context.put("mill", new IteratorTool());


  VTL
  ---

  #set ($list = [1, 2, 3, 5, 8, 13])
  #set ($numbers = $mill.wrap($list))

  #foreach ($item in $numbers)
  #if ($item < 8) $numbers.more()#end
  #end

  $numbers.more()


  Output
  ------

   1 2 3 5
  8

 Example tools.xml config (if you want to use this with VelocityView):
 <tools>
   <toolbox scope="request">
     <tool class="org.apache.velocity.tools.generic.IteratorTool"/>
   </toolbox>
 </tools>
 

Warning: It is not recommended to use hasNext() with this tool as it is used to control the #foreach. Use hasMore() instead.

版本:
$Id: IteratorTool.java 598471 2007-11-27 00:26:10Z nbubna $
作者:
Denis Bredelet
  • 字段概要

    字段
    修饰符和类型
    字段
    说明
    private boolean
    已过时。
     
    private Iterator
    已过时。
     
    protected Object
    已过时。
     
    private boolean
    已过时。
     
    private Object
    已过时。
     
  • 构造器概要

    构造器
    构造器
    说明
    已过时。
    Create a IteratorTool instance to use as tool.
    已过时。
    Create a IteratorTool instance to use in #foreach.
  • 方法概要

    修饰符和类型
    方法
    说明
    boolean
    已过时。
    Returns true if there are more elements in the wrapped list.
    boolean
    已过时。
    Returns true if there are more elements in the list and more() was called.
    private void
    已过时。
    Wraps a list with the tool.
    已过时。
    Asks for the next element in the list.
    已过时。
    Gets the next object in the list.
    void
    已过时。
    Removes the current element from the list.
    void
    已过时。
    Resets the wrapper so that it starts over at the beginning of the list.
    void
    已过时。
    Puts a condition to break out of the loop.
    已过时。
    Returns this object as a String.
    wrap(Object list)
    已过时。
    Wraps a list with the tool.

    从类继承的方法 java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    从接口继承的方法 java.util.Iterator

    forEachRemaining
  • 字段详细资料

    • wrapped

      private Object wrapped
      已过时。
    • iterator

      private Iterator iterator
      已过时。
    • wantMore

      private boolean wantMore
      已过时。
    • cachedNext

      private boolean cachedNext
      已过时。
    • next

      protected Object next
      已过时。
  • 构造器详细资料

    • IteratorTool

      public IteratorTool()
      已过时。
      Create a IteratorTool instance to use as tool. When it is created this way, the tool returns a new instance each time wrap() is called. This is useful when you want to allow the designers to create instances.
    • IteratorTool

      public IteratorTool(Object wrapped)
      已过时。
      Create a IteratorTool instance to use in #foreach.
      参数:
      wrapped - The list to wrap.
  • 方法详细资料

    • wrap

      public IteratorTool wrap(Object list)
      已过时。
      Wraps a list with the tool.
      The list can be an array, a Collection, a Map, an Iterator or an Enumeration.
      If the list is a Map, the tool iterates over the values.
      If the list is an Iterator or an Enumeration, the tool can be used only once.
      参数:
      list - The list to wrap.
      返回:
      A new wrapper if this object is used as a tool, or itself if it is a wrapper.
    • internalWrap

      private void internalWrap(Object wrapped)
      已过时。
      Wraps a list with the tool. This object can therefore be used instead of the list itself in a #foreach. The list can be an array, a Collection, a Map, an Iterator or an Enumeration.
      - If the list is a Map, the tool iterates over the values.
      - If the list is an Iterator or an Enumeration, the tool can be used only once.
      参数:
      wrapped - The list to wrap.
    • reset

      public void reset()
      已过时。

      Resets the wrapper so that it starts over at the beginning of the list.

      Note to programmers: This method has no effect if the wrapped object is an enumeration or an iterator.

    • next

      public Object next()
      已过时。

      Gets the next object in the list. This method is called by #foreach to define $item in:

       #foreach( $item in $list )
       

      This method is not intended for template designers, but they can use them if they want to read the value of the next item without doing more().

      指定者:
      next 在接口中 Iterator
      返回:
      The next item in the list.
      抛出:
      NoSuchElementException - if there are no more elements in the list.
    • hasNext

      public boolean hasNext()
      已过时。
      Returns true if there are more elements in the list and more() was called.
      This code always return false:
       tool.hasNext()? tool.hasNext(): false;
       
      指定者:
      hasNext 在接口中 Iterator
      返回:
      true if there are more elements, and either more() or hasNext() was called since last call.
    • remove

      public void remove() throws UnsupportedOperationException
      已过时。
      Removes the current element from the list. The current element is defined as the last element that was read from the list, either with next() or with more().
      指定者:
      remove 在接口中 Iterator
      抛出:
      UnsupportedOperationException - if the wrapped list iterator doesn't support this operation.
    • more

      public Object more()
      已过时。

      Asks for the next element in the list. This method is to be used by the template designer in #foreach loops.

      If this method is called in the body of #foreach, the loop continues as long as there are elements in the list.
      If this method is not called the loop terminates after the current iteration.

      返回:
      The next element in the list, or null if there are no more elements.
    • hasMore

      public boolean hasMore()
      已过时。
      Returns true if there are more elements in the wrapped list.
      If this object doesn't wrap a list, the method always returns false.
      返回:
      true if there are more elements in the list.
    • stop

      public void stop()
      已过时。
      Puts a condition to break out of the loop. The #foreach loop will terminate after this iteration, unless more() is called after stop().
    • toString

      public String toString()
      已过时。
      Returns this object as a String.
      If this object is used as a tool, it just gives the class name.
      Otherwise it appends the wrapped list to the class name.
      覆盖:
      toString 在类中 Object
      返回:
      A string representation of this object.