org.springframework.expression.spel.ast.InlineMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-expression Show documentation
Show all versions of spring-expression Show documentation
Spring Expression Language (SpEL)
/*
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.expression.spel.ast;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.ExpressionState;
import org.springframework.expression.spel.SpelNode;
/**
* Represent a map in an expression, e.g. '{name:'foo',age:12}'
*
* @author Andy Clement
* @since 4.1
*/
public class InlineMap extends SpelNodeImpl {
// If the map is purely literals, it is a constant value and can be computed and cached
private TypedValue constant = null;
public InlineMap(int pos, SpelNodeImpl... args) {
super(pos, args);
checkIfConstant();
}
/**
* If all the components of the list are constants, or lists/maps that themselves
* contain constants, then a constant list can be built to represent this node.
* This will speed up later getValue calls and reduce the amount of garbage created.
*/
private void checkIfConstant() {
boolean isConstant = true;
for (int c = 0, max = getChildCount(); c < max; c++) {
SpelNode child = getChild(c);
if (!(child instanceof Literal)) {
if (child instanceof InlineList) {
InlineList inlineList = (InlineList) child;
if (!inlineList.isConstant()) {
isConstant = false;
break;
}
}
else if (child instanceof InlineMap) {
InlineMap inlineMap = (InlineMap) child;
if (!inlineMap.isConstant()) {
isConstant = false;
break;
}
}
else if (!((c%2)==0 && (child instanceof PropertyOrFieldReference))) {
isConstant = false;
break;
}
}
}
if (isConstant) {
Map