velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ken Anderson ...@pacific.net>
Subject Re: #foreach tool
Date Tue, 10 Sep 2002 23:32:21 GMT
"FisherMill.java": Error #: 300 : constructor 
ArrayIterator(java.lang.Object) not found in class 
org.apache.velocity.util.ArrayIterator at line 71, column 33

Ken


bredelet@mac.com wrote:
> Guys,
> 
> I am writing a little tool that helps to write a #foreach that 
> terminates after 10 entries or such things. The source needs to be 
> documented but basically the functionality is here.
> 
> It can wrap a list, a map, an iterator, a collection, an enumeration.
> 
> To get the next element you call $tool.more(). If you don't call more(), 
> the tool will terminate the #foreach. That means that you can set a 
> condition that will stop the loop very easily.
> 
> After that, you can reuse the tool to display the remaining elements. 
> You need to do $tool.reset() to start over from the beginning.
> 
> Use $tool.hasNext() to check if there are more elements.
> I called it FisherMill because it sounds funny, if we put it in the 
> tools we could give it a better name :-)
> 
> This is an example of use:
> 
> Java
> ----
> context.put("mill",new FisherMill());
> 
> 
> 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()
> 
> 
> $numbers.reset()
> #foreach ($item in $numbers)
>    $numbers.more() + $numbers.more() = $numbers.more()
> #end
> 
> 
> Output
> ------
> 
>  1 2 3 5
> 8
> 
> 
>    1 + 2 = 3
>    5 + 8 = 13
> 
> 
> Cheers,
> -- Denis.
> 
> 
> ------------------------------------------------------------------------
> 
> //
> //  FisherMill.java
> //
> import java.util.Collection;
> import java.util.Enumeration;
> import java.util.Iterator;
> import java.util.Map;
> import java.util.Vector;
> 
> import org.apache.velocity.util.ArrayIterator;
> import org.apache.velocity.util.EnumerationIterator;
> 
> 
> public class FisherMill implements Iterator {
> 
>     private Object wrapped;
>     private Iterator iterator;
>     private boolean wantMore;
>     private boolean cachedNext;
>     protected Object next;
>     
>     public FisherMill()
>     {
>         this(null);
>     }
> 
>     public FisherMill(Object wrapped)
>     {
>         if (wrapped != null)
>         {
>             wrapAround(wrapped);
>         }
>         else
>         {
>             this.wrapped = null;
>             this.iterator = null;
>             this.wantMore = false;
>             this.cachedNext = false;
>         }
>     }
> 
>     public FisherMill wrap(Object list)
>     {
>         if (this.wrapped == null)
>         {
>             return new FisherMill(list);
>         }
>         else if (list != null)
>         {
>             this.wrapAround(list);
>             return this;
>         }
>         else
>         {
>             throw new IllegalArgumentException("Need a valid list to wrap");
>         }
>     }
> 
>     public void reset()
>     {
>         if (this.wrapped != null)
>         {
>             wrapAround(this.wrapped);
>         }
>     }
> 
>     private void wrapAround(Object wrapped)
>     {
>         // rip-off from org.apache.velocity.runtime.directive.ForEach.java
>         if ( wrapped.getClass().isArray() )
>             this.iterator = new ArrayIterator( wrapped );
>         else if ( wrapped instanceof Collection)
>             this.iterator = ( (Collection) wrapped).iterator();
>         else if ( wrapped instanceof Map )
>             this.iterator = ( (Map) wrapped).values().iterator();
>         else if ( wrapped instanceof Iterator )
>             this.iterator = ( (Iterator) wrapped);
>         else if ( wrapped instanceof Enumeration )
>             this.iterator = new EnumerationIterator( (Enumeration)  wrapped );
>         else
>             // Don't know what is the object.
>             // Should we put it in a one-item array?
>             throw new IllegalArgumentException("Don't know how to wrap this list");
> 
>         this.wrapped = wrapped;
>         this.wantMore = true;
>         this.cachedNext = false;
>     }
> 
> 
>     
>     public Object next()
>     {
>         if (!this.cachedNext)
>         {
>             this.cachedNext = true;
>             this.next = this.iterator.next();
>             return this.next;
>         }
>         else
>         {
>             return this.next;
>         }
>     }
> 
>     public boolean hasNext()
>     {
>         if (this.wantMore)
>         {
>             this.wantMore = false;	// don't want more unless more is called
>             return hasMore();
>         }
>         else
>         {
>             return false;
>         }
>     }
> 
>     public void remove() throws UnsupportedOperationException
>     {
>         // Let the iterator decide whether to implement this or not
>         this.iterator.remove();
>     }
> 
>     public String toString()
>     {
>         String representation = getClass().getName();
>         if (this.wrapped != null)
>         {
>             representation += "(" + this.wrapped + ")";
>         }
>         return representation;
>     }
> 
> 
>     public Object more()
>     {
>         this.wantMore = true;
>         if (hasMore())
>         {
>             Object next = next();
>             this.cachedNext = false;
>             return next;
>         }
>         else
>         {
>             return null;
>         }
>     }
> 
>     public boolean hasMore()
>     {
>         return cachedNext || this.iterator.hasNext();
>     }
> 
> }
> 
> 
> 
> ------------------------------------------------------------------------
> 
> --
> To unsubscribe, e-mail:   <mailto:velocity-user-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail: <mailto:velocity-user-help@jakarta.apache.org>


--
To unsubscribe, e-mail:   <mailto:velocity-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:velocity-user-help@jakarta.apache.org>


Mime
View raw message