001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.filter.function;
018
019import org.apache.activemq.filter.FunctionCallExpression;
020import org.apache.activemq.filter.MessageEvaluationContext;
021
022/**
023 * Filter function that matches a value against a list of values and evaluates to an indicator of membership in the
024 * list.  For example:
025 * <p/>
026 * <p style="margin-left: 4em">
027 * INLIST( SPLIT('1,2,3', ',') , '2' )
028 * </p>
029 * <p/>
030 * Note that the first argument must be a List.  Strings containing lists are not acceptable; for example,
031 * INLIST('1,2,3', '1'), will cause an exception to be thrown at evaluation-time.
032 */
033
034public class inListFunction implements FilterFunction {
035    /**
036     * Check whether the given expression is a valid call of this function.  Two arguments are required.  Note that
037     * the evaluated results of the arguments will be compared with Object#equals().
038     *
039     * @param    expr - the expression consisting of a call to this function.
040     * @return true - if the expression is valid; false - otherwise.
041     */
042
043    public boolean isValid(FunctionCallExpression expr) {
044        if (expr.getNumArguments() != 2)
045            return false;
046
047        return true;
048    }
049
050
051    /**
052     * Check whether the given expression, which consists of a call to this function, evaluates to a Boolean.
053     * If the function can return different more than one type of value at evaluation-time, it must decide whether
054     * to cast the result to a Boolean at this time.
055     *
056     * @param    expr - the expression consisting of a call to this function.
057     * @return true - if the expression is valid; false - otherwise.
058     */
059
060    public boolean returnsBoolean(FunctionCallExpression expr) {
061        return true;
062    }
063
064
065    /**
066     * Evalutate the given expression, which consists of a call to this function, in the context given.  Checks
067     * whether the second argument is a member of the list in the first argument.
068     *
069     * @param    expr - the expression consisting of a call to this function.
070     * @param    message_ctx - the context in which the call is being evaluated.
071     * @return Boolean - the result of the evaluation.
072     */
073
074    public Object evaluate(FunctionCallExpression expr, MessageEvaluationContext message_ctx)
075            throws javax.jms.JMSException {
076        java.util.List arr;
077        int cur;
078        Object cand;
079        boolean found_f;
080
081        arr = (java.util.List) expr.getArgument(0).evaluate(message_ctx);
082        cand = expr.getArgument(1).evaluate(message_ctx);
083
084        cur = 0;
085        found_f = false;
086        while ((cur < arr.size()) && (!found_f)) {
087            found_f = arr.get(cur).equals(cand);
088            cur++;
089        }
090
091        return Boolean.valueOf(found_f);
092    }
093}