[
https://issues.apache.org/jira/browse/FREEMARKER-43?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15731460#comment-15731460
]
Daniel Dekany edited comment on FREEMARKER-43 at 12/8/16 7:48 AM:
------------------------------------------------------------------
If that's indeed a tuple, then your Java class already provides some kind of
ordering for the elements. How does it do it? Does those Java classes,
regardless for FreeMarker already have that {{forEach(consumer)}} method for
example? I'm asking because this sounds like an {{ObjectWrapper}}
responsibility. If the Java object has, for example, a {{forEach}} method, then
it's wrappable as {{TemplateCollectionModel}} right now, no need to add
anything to it. Only the {{ObjectWrapper}} implementation has to be aware of
this pattern.
As of adding default methods to FreeMarker API interfaces, FreeMarker doesn't
require Java 8, so we can't use Java 8 features in its API-s (unless they are
"optional" API-s that you can avoid referring to).
was (Author: ddekany):
If that's indeed a tuple, then your Java class already provides some kind of
ordering for the elements. How does it do it? Does those Java classes,
regardless for FreeMarker already have that {{forEach(consumer)}} method for
example? I'm asking because this sounds like an {{ObjectWrapper}}
responsibility. If the Java object has, for example, a {{forEach}} method, then
it's wrappable as {{TemplateCollectionModel}} right now, no need to add
anything to it. Only the {{ObjectWrapper}} implementation has to be aware of
this pattern.
As of adding default methods to FreeMarker API interfaces, FreeMarker doesn't
require Java 8, so we can use Java 8 features in its API-s (unless they are
"optional" API-s that you can avoid referring to).
> Consider adding a Java 8 Style forEach method to
> TemplateCollectionModel/TemplateSequenceModel
> ----------------------------------------------------------------------------------------------
>
> Key: FREEMARKER-43
> URL: https://issues.apache.org/jira/browse/FREEMARKER-43
> Project: Apache Freemarker
> Issue Type: Improvement
> Components: engine
> Affects Versions: 2.3.25-incubating
> Reporter: Thomas Weidner
> Priority: Minor
>
> I have a lot of "tuple-like" types in my model. For example:
> {code:java}
> class Tuple {
> int a;
> float b;
> String c;
> }
> {code}
> In the template, i would like to iterate through the components of tuple
> using #list. To implement TemplateSequenceModel or TemplateCollectionModel, I
> basically need to create a temporary List object from the tuple, which seems
> unnecessary.
> Java 8 added a forEach model to java.lang.Iterable, which could be easily
> implemented for Tuple without temporary objects:
> {code:java}
> // in Tuple
> void forEach(Consumer<Object> c) {
> c.accept(a);
> c.accept(b);
> c.accept(c);
> }
> {code}
> Unfortunately, there is no such method in
> TemplateSequenceModel/TemplateCollectionModel.
> I propose to add such a method to the both classes and use this method in
> IteratorBlock instead of handcrafted iteration. This would allow to more
> efficient iteration for custom classes, while not imposing worse performance
> on "standard" collections/interfaces (these also provide forEach in java8).
> As this change would require java8, this API change would not break existing
> implementations of Template*Model, as forEach could be implemented as default
> interface method.
> I could provide a prototype patch if requested.
> Maybe as a separate bug, TemplateCollectionModel, TemplateSequenceModel
> should both inherit from a new TemplateIterableModel which only provides
> forEach.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)