001 /**
002 * GRANITE DATA SERVICES
003 * Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004 *
005 * This file is part of the Granite Data Services Platform.
006 *
007 * Granite Data Services is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * Granite Data Services is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015 * General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this library; if not, write to the Free Software
019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020 * USA, or see <http://www.gnu.org/licenses/>.
021 */
022 package org.granite.gravity.selector;
023
024 import javax.jms.JMSException;
025
026 /**
027 * A filter performing a comparison of two objects
028 *
029 * @version $Revision: 1.2 $
030 */
031 public abstract class LogicExpression extends BinaryExpression implements BooleanExpression {
032
033 public static BooleanExpression createOR(BooleanExpression lvalue, BooleanExpression rvalue) {
034 return new LogicExpression(lvalue, rvalue) {
035 @Override
036 public Object evaluate(MessageEvaluationContext message) throws JMSException {
037
038 Boolean lv = (Boolean) left.evaluate(message);
039 // Can we do an OR shortcut??
040 if (lv !=null && lv.booleanValue()) {
041 return Boolean.TRUE;
042 }
043
044 Boolean rv = (Boolean) right.evaluate(message);
045 return rv==null ? null : rv;
046 }
047
048 @Override
049 public String getExpressionSymbol() {
050 return "OR";
051 }
052 };
053 }
054
055 public static BooleanExpression createAND(BooleanExpression lvalue, BooleanExpression rvalue) {
056 return new LogicExpression(lvalue, rvalue) {
057 @Override
058 public Object evaluate(MessageEvaluationContext message) throws JMSException {
059
060 Boolean lv = (Boolean) left.evaluate(message);
061
062 // Can we do an AND shortcut??
063 if (lv == null)
064 return null;
065 if (!lv.booleanValue()) {
066 return Boolean.FALSE;
067 }
068
069 Boolean rv = (Boolean) right.evaluate(message);
070 return rv == null ? null : rv;
071 }
072
073 @Override
074 public String getExpressionSymbol() {
075 return "AND";
076 }
077 };
078 }
079
080 /**
081 * @param left
082 * @param right
083 */
084 public LogicExpression(BooleanExpression left, BooleanExpression right) {
085 super(left, right);
086 }
087
088 abstract public Object evaluate(MessageEvaluationContext message) throws JMSException;
089
090 public boolean matches(MessageEvaluationContext message) throws JMSException {
091 Object object = evaluate(message);
092 return object!=null && object==Boolean.TRUE;
093 }
094
095 }