io.dyn.el.SpelExpression.java Source code

Java tutorial

Introduction

Here is the source code for io.dyn.el.SpelExpression.java

Source

/*
 * Copyright (c) 2011-2012 by 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 io.dyn.el;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.ParserContext;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.SpelEvaluationException;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

/**
 * @author Jon Brisbin <jon@jbrisbin.com>
 */
public class SpelExpression implements Comparable {

    private static final ParserContext TEMPLATE_PARSER_CONTEXT = new TemplateParserContext("#{", "}");
    public static final ExpressionParser DEFAULT_PARSER = new SpelExpressionParser();

    private Expression expression;
    private EvaluationContext evaluationContext = new StandardEvaluationContext();
    private Map<String, Expression> expressionCache = Collections
            .synchronizedMap(new HashMap<String, Expression>());

    public SpelExpression(Expression expression, EvaluationContext evaluationContext) {
        this.expression = expression;
        if (null != evaluationContext) {
            this.evaluationContext = evaluationContext;
        }
    }

    public SpelExpression(String expression, EvaluationContext evaluationContext) {
        this.expression = toExpression(expression);
        if (null != evaluationContext) {
            this.evaluationContext = evaluationContext;
        }
    }

    public static Expression T(Class<?> clazz) {
        return $("T(" + clazz.getName() + ")");
    }

    public static Expression $(String exp) {
        return DEFAULT_PARSER.parseExpression(exp);
    }

    public Expression expression() {
        return expression;
    }

    public static Comparator<Expression> comparator(final EvaluationContext evaluationContext) {
        return new Comparator<Expression>() {
            @Override
            public int compare(Expression ex1, Expression ex2) {
                return new SpelExpression(ex1, evaluationContext).compareTo(ex2);
            }
        };
    }

    @Override
    public int compareTo(Object obj) {
        // Check for object equality
        if (null != obj && (obj == this || obj.equals(this))) {
            return 0;
        }
        // If that fails, try and check the expressions
        try {
            Object o1 = expression.getValue(evaluationContext);
            Object o2 = null;
            try {
                if (obj instanceof String) {
                    o2 = toExpression((String) obj).getValue(evaluationContext);
                } else if (obj instanceof SpelExpression) {
                    o2 = ((SpelExpression) o2).expression.getValue(evaluationContext);
                } else if (obj instanceof Expression) {
                    o2 = ((Expression) obj).getValue(evaluationContext);
                }
            } catch (NullPointerException npe) {
                return -1;
            }
            if (null != o1) {
                if (o1.equals(o2)) {
                    return 0;
                }
            }
        } catch (SpelEvaluationException ignored) {
        }

        return -1;
    }

    private Expression toExpression(String s) {
        Expression x = expressionCache.get(s);
        if (null == x) {
            x = DEFAULT_PARSER.parseExpression(s, TEMPLATE_PARSER_CONTEXT);
            expressionCache.put(s, x);
        }
        return x;
    }

}