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 2017/07/01 14:33:02 UTC

[jira] [Resolved] (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 resolved JEXL-228.
--------------------------------
       Resolution: Won't Do
         Assignee: Henri Biestro
    Fix Version/s: 3.2

After more thoughts, I disagree JEXL should propose a generic get based on the 'ordinal' in any collection.
The expected complexity of the property/array get/set is O(1) (may be O(log(n)); it certainly can not be destructive.
On a set, the get/set should equates to adding/removing an element in the set; if elements are integers/numbers, there is no way to make the distinction (besides trying to play with arrayGet vs propertyGet). For other collections - like queue -, the get (poll) implies a (destructive) side-effect.

I dont think the generic case is safe enough to be added as a default feature; this does not preclude you implementing in your own platform through the various customization points. Btw, if arrayGet/propetyGet return JexlEngine.TRY_FAILED, the default resolution mechanism will apply.

> 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
(v6.4.14#64029)