You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Henri Biestro (Jira)" <ji...@apache.org> on 2021/06/07 13:15:06 UTC

[jira] [Closed] (JEXL-228) Ability to read j.u.Collection elements other than instanceof j.u.List by using [] and . operator

     [ https://issues.apache.org/jira/browse/JEXL-228?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Biestro closed JEXL-228.
------------------------------

> Ability to read j.u.Collection elements other than instanceof j.u.List by using [] and . operator
> -------------------------------------------------------------------------------------------------
>
>                 Key: JEXL-228
>                 URL: https://issues.apache.org/jira/browse/JEXL-228
>             Project: Commons JEXL
>          Issue Type: Improvement
>    Affects Versions: 3.1
>            Reporter: Dmitri Blinov
>            Assignee: Henri Biestro
>            Priority: Minor
>             Fix For: 3.2
>
>
> In the current implementation of Jexl there is a possibility to address in a script the specific elements of {{List}} and {{Array}} by using operators {{[]}} or {{.}}, whereas its not that simple for descendants of {{Set}} or any other {{Collection}} descendant, as there is no such an operator for them out of the box. But in practice this is not so uncommon task for a script writer to get, for example, the first element of the LinkedHashSet, which is supposed to maintain the logical order of its elements, but unfortunately does not implement {{List}} interface for us to be able to address its elements in that order. I believe such a basic functionality - deserves to be available for script witers from the start.
> I have been experimenting with overloading access operators in customized JexlArithmetic for a while and have managed to overload operators {{[]}} and {{.}} so that they could be used not only for {{List}} interfaces but for other {{Collection}} types as well:
> {code}
>     protected Object nth(Collection c, int i) {
>        if (c instanceof List) {
>          List list = (List) c;
>          return list.get(i);
>        } else {
>          for (Object o : c) {
>             if (i-- == 0)
>               return o;
>          }
>        }
>        return null;
>     }
>     public Object propertyGet(Collection c, Number n) {
>        return nth(c, n.intValue());
>     }
>     public Object arrayGet(Collection c, Number n) {
>        return nth(c, n.intValue());
>     }
> {code}
> But I'm not satisfied with the fact that I have disabled alltogether the initial Jexl implementation of access operators for {{LIst}} types.
> The suggestion is to add the ability for Jexl scripts to read, not manipulate, elements of any {{Collection}} type. 
> BTW, I have found that for {{List}} elements this solution is roughly 50% faster compared with that of built-in Jexl List access implementation, so may be there should be a place for a performance improvement in a way Jexl itself accesses list elements.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)