Velocity
  1. Velocity
  2. VELOCITY-533

Treat arrays like fixed-length lists

    Details

    • Type: New Feature New Feature
    • Status: Resolved
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.5
    • Fix Version/s: 1.6
    • Component/s: Engine
    • Labels:
      None

      Description

      I'd like to see arrays treated like fixed-length List objects as much as possible. This means auto-magically supporting size(), set(int,Object), get(int), and isEmpty() at a minimum. Support for something like iterator() probably isn't necessary, since #foreach already handles arrays, but it might be good for completeness. These should be possible via some Uberspect hackery.

      And since the goal is to free template authors from having to care about the differences between Lists and arrays, we might also consider supporting conversion between the two for method calls, so:

      $foo.wantsListHere($thisisAnArray)

      and

      $bar.wantsArrayHere($thisisAList)

      both work automatically. I haven't yet looked into what it would take to do this. If it is too hard or messy, i could be happy with just support for calling List methods on arrays.

      1. PrimitiveArrayWrapper.java
        1 kB
        Christopher Schultz
      2. PrimitiveArrayList.java
        0.7 kB
        Christopher Schultz

        Issue Links

          Activity

          Mark Thomas made changes -
          Workflow Default workflow, editable Closed status [ 12551952 ] jira [ 12552332 ]
          Mark Thomas made changes -
          Workflow jira [ 12400446 ] Default workflow, editable Closed status [ 12551952 ]
          Nathan Bubna made changes -
          Resolution Fixed [ 1 ]
          Status Open [ 1 ] Resolved [ 5 ]
          Hide
          Nathan Bubna added a comment -

          Thanks for the contribution Chris. I changed the name to reflect that it isn't just for arrays of primitives. This definitely cleans up and fills out the implementation nicely.

          Things to note:

          • When a java.lang.Object method is called on an array in a template, the wrapper is not used. This preserves backwards compatibility
          • When a length-changing method is called (e.g. add() or remove()), these currently exist but throw UnsupportedOperationExceptions. I'm not sure this is ideal, but i don't think it's wrong. So, i'm leaving it like that.

          All this is done as of revision 579378

          Show
          Nathan Bubna added a comment - Thanks for the contribution Chris. I changed the name to reflect that it isn't just for arrays of primitives. This definitely cleans up and fills out the implementation nicely. Things to note: When a java.lang.Object method is called on an array in a template, the wrapper is not used. This preserves backwards compatibility When a length-changing method is called (e.g. add() or remove()), these currently exist but throw UnsupportedOperationExceptions. I'm not sure this is ideal, but i don't think it's wrong. So, i'm leaving it like that. All this is done as of revision 579378
          Nathan Bubna made changes -
          Link This issue is duplicated by VELOCITY-365 [ VELOCITY-365 ]
          Christopher Schultz made changes -
          Attachment PrimitiveArrayList.java [ 12356073 ]
          Hide
          Christopher Schultz added a comment -

          Per discussion on the developer list, I am including a class that can wrap a primitive array (any array, actually) and provide (read-only) list-like access to it.

          This will be more useful than the collection-style wrapper since it allows direct-access to elements based on index instead of requiring element access through an iterator.

          Requires Java 1.2 or better since it uses the Java Collections API.

          Show
          Christopher Schultz added a comment - Per discussion on the developer list, I am including a class that can wrap a primitive array (any array, actually) and provide (read-only) list-like access to it. This will be more useful than the collection-style wrapper since it allows direct-access to elements based on index instead of requiring element access through an iterator. Requires Java 1.2 or better since it uses the Java Collections API.
          Christopher Schultz made changes -
          Field Original Value New Value
          Attachment PrimitiveArrayWrapper.java [ 12356072 ]
          Hide
          Christopher Schultz added a comment -

          Per discussion on the developer list, I am including a class that can wrap a primitive array (any array, actually) and provide collection-like access to it.

          Requires Java 1.2 or better since it uses the Java Collections API.

          Show
          Christopher Schultz added a comment - Per discussion on the developer list, I am including a class that can wrap a primitive array (any array, actually) and provide collection-like access to it. Requires Java 1.2 or better since it uses the Java Collections API.
          Hide
          Nathan Bubna added a comment -

          Ok, i've implemented support for size(), isEmpty(), get(int), and set(int,Object), as well as fairly thorough testing of the feature. All tests pass.

          Support for contains(Object), subList(int,int), and others may follow as i have time (anyone else is welcome to jump in here...). I still haven't looked into support for auto-conversion between lists and arrays used as method parameters when the other is wanted.

          Show
          Nathan Bubna added a comment - Ok, i've implemented support for size(), isEmpty(), get(int), and set(int,Object), as well as fairly thorough testing of the feature. All tests pass. Support for contains(Object), subList(int,int), and others may follow as i have time (anyone else is welcome to jump in here...). I still haven't looked into support for auto-conversion between lists and arrays used as method parameters when the other is wanted.
          Nathan Bubna created issue -

            People

            • Assignee:
              Unassigned
              Reporter:
              Nathan Bubna
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development