craigmcc    2002/08/03 16:53:45

  Modified:    src/share/org/apache/struts/taglib/logic IterateTag.java
  Log:
  Improve the performance of iterating over an array of objects, by avoiding the
  need to copy the array into a list.  Unfortunately, that still doesn't help
  for arrays of primitives :-(.
  
  PR: Bugzilla #11394
  Submitted by: Eric Friedman <eric.d.friedman at wellsfargo.com>
  
  Revision  Changes    Path
  1.15      +17 -9     
jakarta-struts/src/share/org/apache/struts/taglib/logic/IterateTag.java
  
  Index: IterateTag.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/logic/IterateTag.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- IterateTag.java   12 Mar 2002 05:35:40 -0000      1.14
  +++ IterateTag.java   3 Aug 2002 23:53:45 -0000       1.15
  @@ -65,6 +65,7 @@
   
   import java.lang.reflect.Array;
   import java.util.ArrayList;
  +import java.util.Arrays;
   import java.util.Collection;
   import java.util.Enumeration;
   import java.util.Iterator;
  @@ -311,12 +312,19 @@
   
        // Construct an iterator for this collection
        if (collection.getClass().isArray()) {
  -            int length = Array.getLength(collection);
  -            ArrayList c = new ArrayList(length);
  -            for (int i = 0; i < length; i++) {
  -                c.add(Array.get(collection, i));
  +            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();
               }
  -            iterator = c.iterator();
        } else if (collection instanceof Collection)
            iterator = ((Collection) collection).iterator();
        else if (collection instanceof Iterator)
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to