Velocity
  1. Velocity
  2. VELOCITY-379

foreach should generalise for Iterable, not just Collection

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.5
    • Fix Version/s: 1.5
    • Component/s: Engine
    • Labels:
      None
    • Environment:
      Operating System: All
      Platform: All

      Description

      As of Java 1.5 the java.lang.Iterable interface is the general contract that
      should be used when looking for things that provide Iterators. This allows for
      user defined non collection types to be generally usable by things that just
      need iteration such as the Java Language's for(X x : y) looping construct and
      seems perfectly applicable to the velocity Foreach directive.
      I'm not sure of the versioning policy (which java version) of Velocity so if you
      are deliberately not creating a 1.5 dependency then this is not a bug.

        Activity

        Hide
        Nathan Bubna added a comment -

        This was re-resolved by having #foreach fall back to looking for an iterator() method when it can't figure out another way to get an iterator from the reference. This is backwards compatible and supports the JDK 5 Iterable interface.

        See VELOCITY-443 and revision 579331 for more info.

        Show
        Nathan Bubna added a comment - This was re-resolved by having #foreach fall back to looking for an iterator() method when it can't figure out another way to get an iterator from the reference. This is backwards compatible and supports the JDK 5 Iterable interface. See VELOCITY-443 and revision 579331 for more info.
        Hide
        Henning Schmiedehausen added a comment -

        Close all resolved issues for Engine 1.5 release.

        Show
        Henning Schmiedehausen added a comment - Close all resolved issues for Engine 1.5 release.
        Hide
        Henning Schmiedehausen added a comment -

        For Velocity 1.5 you can use a custome Uberspector to be able to iterate on Iterable. There is example code on how to do this in whiteboard/henning/jdk15 and an actual implementation in whiteboard/henning/jdk15/src/java/org/apache/velocity/util/introspection/JDK15UberspectImpl.java

        Show
        Henning Schmiedehausen added a comment - For Velocity 1.5 you can use a custome Uberspector to be able to iterate on Iterable. There is example code on how to do this in whiteboard/henning/jdk15 and an actual implementation in whiteboard/henning/jdk15/src/java/org/apache/velocity/util/introspection/JDK15UberspectImpl.java
        Hide
        Trejkaz added a comment -

        I'd be curious to know how to work around this on the current Velocity. Our application needs to output a large amount of text, which we have to generate on the fly because it's too big to store in memory. As we can't seem to simply insert a Reader into the output, we're planning to do this via an Iterator<String>. Problem is, if I use an Iterator, I get a warning in the logs for every document I output.

        Using an Iterable<String> would be much better, so in the meantime is there a way to add support for that without having to modify Velocity itself?

        Show
        Trejkaz added a comment - I'd be curious to know how to work around this on the current Velocity. Our application needs to output a large amount of text, which we have to generate on the fly because it's too big to store in memory. As we can't seem to simply insert a Reader into the output, we're planning to do this via an Iterator<String>. Problem is, if I use an Iterator, I get a warning in the logs for every document I output. Using an Iterable<String> would be much better, so in the meantime is there a way to add support for that without having to modify Velocity itself?
        Hide
        David Blaikie added a comment -

        Alternatively you could just generalise and use reflection to find an
        iterator():Iterator method regardless of the class, perhaps warning if it isn't
        a Collection..

        Show
        David Blaikie added a comment - Alternatively you could just generalise and use reflection to find an iterator():Iterator method regardless of the class, perhaps warning if it isn't a Collection..
        Hide
        Marinó A. Jónsson added a comment -

        Are we talking v1.3 build compatibility or v1.3 runtime compatibility? If it
        were possible to support this at runtime without breaking the v1.3 runtime
        compatibility I think that would be the way to go - given that we're not talking
        about v1.3 build compatibility. In the case of aiming for a v1.3 build
        compatibility I would like to hear the pros for that move as opposed to at least
        a v1.4 compatibility (or imo preferably v1.5).

        Show
        Marinó A. Jónsson added a comment - Are we talking v1.3 build compatibility or v1.3 runtime compatibility? If it were possible to support this at runtime without breaking the v1.3 runtime compatibility I think that would be the way to go - given that we're not talking about v1.3 build compatibility. In the case of aiming for a v1.3 build compatibility I would like to hear the pros for that move as opposed to at least a v1.4 compatibility (or imo preferably v1.5).
        Hide
        Will Glass-Husain added a comment -

        Hi,

        Cool idea, but unlikely in the short term as Velocity aims for 1.3
        compatibility. (There's been suggestions to bring it up to 1.4). I suggest
        you make an adapter.

        Thanks for the issue, though. I'll leave this open in case there's other
        comments.

        Show
        Will Glass-Husain added a comment - Hi, Cool idea, but unlikely in the short term as Velocity aims for 1.3 compatibility. (There's been suggestions to bring it up to 1.4). I suggest you make an adapter. Thanks for the issue, though. I'll leave this open in case there's other comments.

          People

          • Assignee:
            Henning Schmiedehausen
            Reporter:
            David Blaikie
          • Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development