|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2018 the original author or authors. |
| 2 | + * Copyright 2002-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
17 | 17 | package org.springframework.expression;
|
18 | 18 |
|
19 | 19 | import java.util.List;
|
| 20 | +import java.util.function.Supplier; |
20 | 21 |
|
21 | 22 | import org.springframework.lang.Nullable;
|
22 | 23 |
|
23 | 24 | /**
|
24 | 25 | * Expressions are executed in an evaluation context. It is in this context that
|
25 | 26 | * references are resolved when encountered during expression evaluation.
|
26 | 27 | *
|
27 |
| - * <p>There is a default implementation of this EvaluationContext interface: |
28 |
| - * {@link org.springframework.expression.spel.support.StandardEvaluationContext} |
29 |
| - * which can be extended, rather than having to implement everything manually. |
| 28 | + * <p>There are two default implementations of this interface. |
| 29 | + * <ul> |
| 30 | + * <li>{@link org.springframework.expression.spel.support.SimpleEvaluationContext |
| 31 | + * SimpleEvaluationContext}: a simpler builder-style {@code EvaluationContext} |
| 32 | + * variant for data-binding purposes, which allows for opting into several SpEL |
| 33 | + * features as needed.</li> |
| 34 | + * <li>{@link org.springframework.expression.spel.support.StandardEvaluationContext |
| 35 | + * StandardEvaluationContext}: a powerful and highly configurable {@code EvaluationContext} |
| 36 | + * implementation, which can be extended, rather than having to implement everything |
| 37 | + * manually.</li> |
| 38 | + * </ul> |
30 | 39 | *
|
31 | 40 | * @author Andy Clement
|
32 | 41 | * @author Juergen Hoeller
|
| 42 | + * @author Sam Brannen |
33 | 43 | * @since 3.0
|
34 | 44 | */
|
35 | 45 | public interface EvaluationContext {
|
@@ -85,15 +95,38 @@ public interface EvaluationContext {
|
85 | 95 | OperatorOverloader getOperatorOverloader();
|
86 | 96 |
|
87 | 97 | /**
|
88 |
| - * Set a named variable within this evaluation context to a specified value. |
| 98 | + * Assign the value created by the specified {@link Supplier} to a named variable |
| 99 | + * within this evaluation context. |
| 100 | + * <p>In contrast to {@link #setVariable(String, Object)}, this method should only |
| 101 | + * be invoked to support the assignment operator ({@code =}) within an expression. |
| 102 | + * <p>By default, this method delegates to {@code setVariable(String, Object)}, |
| 103 | + * providing the value created by the {@code valueSupplier}. Concrete implementations |
| 104 | + * may override this <em>default</em> method to provide different semantics. |
| 105 | + * @param name the name of the variable to assign |
| 106 | + * @param valueSupplier the supplier of the value to be assigned to the variable |
| 107 | + * @return a {@link TypedValue} wrapping the assigned value |
| 108 | + * @since 5.2.24 |
| 109 | + */ |
| 110 | + default TypedValue assignVariable(String name, Supplier<TypedValue> valueSupplier) { |
| 111 | + TypedValue typedValue = valueSupplier.get(); |
| 112 | + setVariable(name, typedValue.getValue()); |
| 113 | + return typedValue; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Set a named variable in this evaluation context to a specified value. |
| 118 | + * <p>In contrast to {@link #assignVariable(String, Supplier)}, this method |
| 119 | + * should only be invoked programmatically when interacting directly with the |
| 120 | + * {@code EvaluationContext} — for example, to provide initial |
| 121 | + * configuration for the context. |
89 | 122 | * @param name the name of the variable to set
|
90 | 123 | * @param value the value to be placed in the variable
|
91 | 124 | */
|
92 | 125 | void setVariable(String name, @Nullable Object value);
|
93 | 126 |
|
94 | 127 | /**
|
95 | 128 | * Look up a named variable within this evaluation context.
|
96 |
| - * @param name variable to lookup |
| 129 | + * @param name the name of the variable to look up |
97 | 130 | * @return the value of the variable, or {@code null} if not found
|
98 | 131 | */
|
99 | 132 | @Nullable
|
|
0 commit comments