001/*
002 * Copyright (c) 2016-2017 Chris K Wensel <chris@wensel.net>. All Rights Reserved.
003 * Copyright (c) 2007-2017 Xplenty, Inc. All Rights Reserved.
004 *
005 * Project and contact information: http://www.cascading.org/
006 *
007 * This file is part of the Cascading project.
008 *
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *     http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 */
021
022package cascading.operation.filter;
023
024import java.beans.ConstructorProperties;
025
026import cascading.flow.FlowProcess;
027import cascading.operation.Filter;
028import cascading.operation.FilterCall;
029import cascading.tuple.Fields;
030import cascading.tuple.Tuple;
031import cascading.tuple.TupleEntry;
032
033/**
034 * Class Or is a {@link Filter} class that will logically 'or' the results of the constructor provided Filter
035 * instances.
036 * <p>
037 * Logically, if {@link Filter#isRemove(cascading.flow.FlowProcess, cascading.operation.FilterCall)} returns {@code true} for any of the given instances,
038 * this filter will return {@code true}.
039 *
040 * @see And
041 * @see Xor
042 * @see Not
043 */
044public class Or extends Logic
045  {
046  /**
047   * Constructor Or creates a new Or instance where all Filter instances receive all arguments.
048   *
049   * @param filters of type Filter...
050   */
051  @ConstructorProperties({"filters"})
052  public Or( Filter... filters )
053    {
054    super( filters );
055    }
056
057  /**
058   * Constructor Or creates a new Or instance.
059   *
060   * @param lhsArgumentSelector of type Fields
061   * @param lhsFilter           of type Filter
062   * @param rhsArgumentSelector of type Fields
063   * @param rhsFilter           of type Filter
064   */
065  @ConstructorProperties({"lhsArgumentsSelector", "lhsFilter", "rhsArgumentSelector", "rhsFilter"})
066  public Or( Fields lhsArgumentSelector, Filter lhsFilter, Fields rhsArgumentSelector, Filter rhsFilter )
067    {
068    super( lhsArgumentSelector, lhsFilter, rhsArgumentSelector, rhsFilter );
069    }
070
071  /**
072   * Constructor Or creates a new Or instance.
073   *
074   * @param argumentSelectors of type Fields[]
075   * @param filters           of type Filter[]
076   */
077  @ConstructorProperties({"argumentSelectors", "filters"})
078  public Or( Fields[] argumentSelectors, Filter[] filters )
079    {
080    super( argumentSelectors, filters );
081    }
082
083  @Override
084  public boolean isRemove( FlowProcess flowProcess, FilterCall filterCall )
085    {
086    TupleEntry arguments = filterCall.getArguments();
087    Context context = (Context) filterCall.getContext();
088
089    TupleEntry[] argumentEntries = context.argumentEntries;
090
091    for( int i = 0; i < argumentSelectors.length; i++ )
092      {
093      Tuple selected = arguments.selectTuple( argumentSelectors[ i ] );
094
095      argumentEntries[ i ].setTuple( selected );
096
097      if( filters[ i ].isRemove( flowProcess, context.calls[ i ] ) )
098        return true;
099      }
100
101    return false;
102    }
103  }