io.dyn.core.handler.Guard.java Source code

Java tutorial

Introduction

Here is the source code for io.dyn.core.handler.Guard.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.core.handler;

import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.support.StandardEvaluationContext;

/**
 * @author Jon Brisbin <jon@jbrisbin.com>
 */
public class Guard {

    private Expression expression;

    public Guard(Expression expression) {
        this.expression = expression;
    }

    public Expression expression() {
        return expression;
    }

    public boolean checkGuard(Object target, EvaluationContext evaluationContext) {
        if (null == evaluationContext) {
            evaluationContext = new StandardEvaluationContext(target);
        }
        Object o = (null == target ? expression.getValue(evaluationContext)
                : expression.getValue(evaluationContext, target));
        if (null != o) {
            if (o instanceof Boolean) {
                return (Boolean) o;
            } else {
                return true;
            }
        }
        return false;
    }

    @Override
    public String toString() {
        return "Guard{" + "expression=" + expression + '}';
    }

}