0
votes

First of all, please consider I am beginner as a developer and as a Spring user too. I am searching for a solution to this problem:

There is a given list filled with JsonNode objects, called attributes. I have to find which one is evaulating true against a given boolean expression. I would like to register a collection in a context, iterating over it and finding if any attributes evaulate true with SpEl, so I don't have to make this quite expensive context creation every time.

Do you have any idea how to do it faster? (The code is dummy. Problem is real.)

//given
List<JsonNode> attributes;

attributes
    .stream()
    .anyMatch(attribute -> {
        ExpressionParser parser = new SpelExpressionParser();
        Expression exp = parser.parseExpression("attribute.name == 'John Doe'");
        EvaluationContext ctx = new StandardEvaluationContext(attribute);
        return exp.getValue(ctx, Boolean.class);
    });

Thank you for your help!

1

1 Answers

1
votes

You don't need to parse and build a context for each element; you can pass the root object into the getValue().

Parse the expression once and use getValue(context, attribute, Boolean.class) instead.

Unless you need to customize the context, you don't even need one.

getValue(attribute, Boolean.class)