001/*
002 * Copyright (c) 2007-2017 Xplenty, 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
021package cascading.operation.expression;
022
023import java.beans.ConstructorProperties;
024
025import cascading.flow.FlowProcess;
026import cascading.operation.Filter;
027import cascading.operation.FilterCall;
028import cascading.tuple.Tuple;
029import 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 */
050public class ExpressionFilter extends ExpressionOperation implements Filter<ScriptOperation.Context>
051  {
052  /**
053   * Constructor ExpressionFilter creates a new ExpressionFilter instance.
054   * <p/>
055   * This constructor, when used with incoming arguments that have type information, the argument field
056   * names can be used directly in the the expression, for example {@code a + b }. The type of {@code a} and {@code b}
057   * will be inherited from the incoming argument fields.
058   * <p/>
059   * Or, if the incoming argument selector is {@link cascading.tuple.Fields#NONE}, an expression using only static method calls
060   * or constants can be used, for example {@code Math.random() < SomeClass.someValue() }.
061   *
062   * @param expression of type String
063   */
064  @ConstructorProperties({"expression"})
065  public ExpressionFilter( String expression )
066    {
067    super( expression );
068    }
069
070  /**
071   * Constructor ExpressionFilter creates a new ExpressionFilter instance.
072   *
073   * @param expression    of type String
074   * @param parameterType of type Class
075   */
076  @ConstructorProperties({"expression", "parameterType"})
077  public ExpressionFilter( String expression, Class parameterType )
078    {
079    super( expression, parameterType );
080    }
081
082  /**
083   * Constructor ExpressionFilter creates a new ExpressionFilter instance.
084   *
085   * @param expression     of type String
086   * @param parameterNames of type String[]
087   * @param parameterTypes of type Class[]
088   */
089  @ConstructorProperties({"expression", "parameterNames", "parameterTypes"})
090  public ExpressionFilter( String expression, String[] parameterNames, Class[] parameterTypes )
091    {
092    super( expression, parameterNames, parameterTypes );
093    }
094
095  @Override
096  public boolean isRemove( FlowProcess flowProcess, FilterCall<Context> filterCall )
097    {
098    return (Boolean) evaluate( filterCall.getContext(), filterCall.getArguments() );
099    }
100  }