001    /*
002     * Copyright (c) 2007-2014 Concurrent, Inc. All Rights Reserved.
003     *
004     * Project and contact information: http://www.cascading.org/
005     *
006     * This file is part of the Cascading project.
007     *
008     * Licensed under the Apache License, Version 2.0 (the "License");
009     * you may not use this file except in compliance with the License.
010     * You may obtain a copy of the License at
011     *
012     *     http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing, software
015     * distributed under the License is distributed on an "AS IS" BASIS,
016     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017     * See the License for the specific language governing permissions and
018     * limitations under the License.
019     */
020    
021    package cascading.operation.expression;
022    
023    import java.beans.ConstructorProperties;
024    
025    import cascading.flow.FlowProcess;
026    import cascading.operation.Filter;
027    import cascading.operation.FilterCall;
028    import cascading.tuple.Tuple;
029    import org.codehaus.janino.ExpressionEvaluator;
030    
031    /**
032     * Class ExpressionFilter dynamically resolves a given expression using argument {@link Tuple} values. Any Tuple that
033     * returns true for the given expression will be removed from the stream. This {@link Filter}
034     * is based on the <a href="http://www.janino.net/">Janino</a> compiler.
035     * <p/>
036     * Specifically this filter uses the {@link ExpressionEvaluator}, thus the syntax from that class is inherited here.
037     * <p/>
038     * An expression may use field names directly as parameters in the expression, or field positions with the syntax
039     * "$n", where n is an integer.
040     * <p/>
041     * Given an argument tuple with the fields "a" and "b", the following expression returns true: <br/>
042     * <code>a + b == $0 + $1</code><br/>
043     * <p/>
044     * Further, the types of the tuple elements will be coerced into the given parameterTypes. Regardless of the actual
045     * tuple element values, they will be converted to the types expected by the expression.
046     * <p/>
047     * Field names used in the expression should be valid Java variable names; for example, '+' or '-' are not allowed.
048     * Also the use of a field name that begins with an upper-case character is likely to fail and should be avoided.
049     */
050    public class ExpressionFilter extends ExpressionOperation implements Filter<ScriptOperation.Context>
051      {
052      /**
053       * Constructor ExpressionFilter creates a new ExpressionFilter instance.
054       *
055       * @param expression    of type String
056       * @param parameterType of type Class
057       */
058      @ConstructorProperties({"expression", "parameterType"})
059      public ExpressionFilter( String expression, Class parameterType )
060        {
061        super( expression, parameterType );
062        }
063    
064      /**
065       * Constructor ExpressionFilter creates a new ExpressionFilter instance.
066       *
067       * @param expression     of type String
068       * @param parameterNames of type String[]
069       * @param parameterTypes of type Class[]
070       */
071      @ConstructorProperties({"expression", "parameterNames", "parameterTypes"})
072      public ExpressionFilter( String expression, String[] parameterNames, Class[] parameterTypes )
073        {
074        super( expression, parameterNames, parameterTypes );
075        }
076    
077      @Override
078      public boolean isRemove( FlowProcess flowProcess, FilterCall<Context> filterCall )
079        {
080        return (Boolean) evaluate( filterCall.getContext(), filterCall.getArguments() );
081        }
082      }