You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Andy Boyko <ab...@fabgear.com> on 2000/11/15 08:16:52 UTC

PATCH: taglib/form/OptionsTag - Map support

This patch allows the form taglib's OptionsTag to properly
support a java.util.Map as the collection it's iterating.

It assumes that the key of the map entries should be displayed
as the value of the <option> tag, and the value mapped to by
that key should be displayed as the label for the <option>.

Andy Boyko    aboyko@fabgear.com

RCS file:
/home/cvspublic/jakarta-struts/src/share/org/apache/struts/taglib/form/Optio
nsTag.java,v
retrieving revision 1.2
diff -u -r1.2 OptionsTag.java
--- src/share/org/apache/struts/taglib/form/OptionsTag.java     2000/11/04
01:26:59  1.2
+++ src/share/org/apache/struts/taglib/form/OptionsTag.java     2000/11/15
07:08:49
@@ -183,10 +183,20 @@
        // Render the options tags for each element of the values collection
        StringBuffer sb = new StringBuffer();
        while (valuesIterator.hasNext()) {
-           String value = (String) valuesIterator.next();
-           String label = value;
-           if (labelsIterator.hasNext())
-               label = (String) labelsIterator.next();
+
+            Object next = valuesIterator.next();
+
+            String label, value;
+            if ( next instanceof Map.Entry ) {
+                Map.Entry entry = (Map.Entry) next;
+                value = (String) entry.getKey();
+                label = (String) entry.getValue();
+            } else {
+               value = (String) next;
+               label = value;
+               if (labelsIterator.hasNext())
+                   label = (String) labelsIterator.next();
+            }
            sb.append("<option value=\"");
            sb.append(value);
            sb.append("\"");


RE: PATCH: taglib/form/OptionsTag - Map support

Posted by Andrew Boyko <ab...@fabgear.com>.
> Currently, the patch allows the mixing of Map and non Map entries
> since the if statement is inside the while. Is that what you intended ?
>
> Pulling the if outside the while would make the type checking 'instanceof'
> a one time overhead, instead of for each iteration.

True.  Sort of.  The type of the collection is buried in the
getIterator() function; without changing more code than I was
comfortable doing at 2AM, there's not a good way to determine
the type of the collection from its iterator until you
start iterating.

But, yes, the right answer would be to set an isMap flag
when getting the iterator, so we could skip the instanceof
here.  I'll do that soon.

Andy Boyko   aboyko@fabgear.com


> >         while (valuesIterator.hasNext()) {
> > -           String value = (String) valuesIterator.next();
> > -           String label = value;
> > -           if (labelsIterator.hasNext())
> > -               label = (String) labelsIterator.next();
> > +
> > +            Object next = valuesIterator.next();
> > +
> > +            String label, value;
> > +            if ( next instanceof Map.Entry ) {
> > +                Map.Entry entry = (Map.Entry) next;
> > +                value = (String) entry.getKey();
> > +                label = (String) entry.getValue();
> > +            } else {
> > +               value = (String) next;
> > +               label = value;
> > +               if (labelsIterator.hasNext())
> > +                   label = (String) labelsIterator.next();
> > +            }
>
> --
> Robert Leland			Robert@free2create.org
> 804 N. Kenmore Street		+01-703-525-3580
> Arlington VA 22201
>
>


Re: PATCH: taglib/form/OptionsTag - Map support

Posted by Robert Leland <Ro...@free2create.org>.
Currently, the patch allows the mixing of Map and non Map entries
since the if statement is inside the while. Is that what you intended ?

Pulling the if outside the while would make the type checking 'instanceof'
a one time overhead, instead of for each iteration. 

-Rob
>         while (valuesIterator.hasNext()) {
> -           String value = (String) valuesIterator.next();
> -           String label = value;
> -           if (labelsIterator.hasNext())
> -               label = (String) labelsIterator.next();
> +
> +            Object next = valuesIterator.next();
> +
> +            String label, value;
> +            if ( next instanceof Map.Entry ) {
> +                Map.Entry entry = (Map.Entry) next;
> +                value = (String) entry.getKey();
> +                label = (String) entry.getValue();
> +            } else {
> +               value = (String) next;
> +               label = value;
> +               if (labelsIterator.hasNext())
> +                   label = (String) labelsIterator.next();
> +            }

-- 
Robert Leland			Robert@free2create.org
804 N. Kenmore Street		+01-703-525-3580
Arlington VA 22201