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.TupleEntry;
031
032/**
033 * Class Xor is a {@link Filter} class that will logically 'xor' (exclusive or) the results of the
034 * constructor provided Filter instances.
035 * <p>
036 * Logically, if {@link Filter#isRemove(cascading.flow.FlowProcess, cascading.operation.FilterCall)} returns {@code true} for all given instances,
037 * or returns {@code false} for all given instances, this filter will return {@code false}.
038 * <p>
039 * Note that Xor can only be applied to two values.
040 *
041 * @see And
042 * @see Or
043 * @see Not
044 */
045public class Xor extends Logic
046  {
047  /**
048   * Constructor Xor creates a new Xor instance where all Filter instances receive all arguments.
049   *
050   * @param filters of type Filter...
051   */
052  @ConstructorProperties({"filters"})
053  public Xor( Filter... filters )
054    {
055    super( filters );
056    }
057
058  /**
059   * Constructor Xor creates a new Xor instance.
060   *
061   * @param lhsArgumentSelector of type Fields
062   * @param lhsFilter           of type Filter
063   * @param rhsArgumentSelector of type Fields
064   * @param rhsFilter           of type Filter
065   */
066  @ConstructorProperties({"lhsArgumentsSelector", "lhsFilter", "rhsArgumentSelector", "rhsFilter"})
067  public Xor( Fields lhsArgumentSelector, Filter lhsFilter, Fields rhsArgumentSelector, Filter rhsFilter )
068    {
069    super( lhsArgumentSelector, lhsFilter, rhsArgumentSelector, rhsFilter );
070    }
071
072  @Override
073  public boolean isRemove( FlowProcess flowProcess, FilterCall filterCall )
074    {
075    Context context = (Logic.Context) filterCall.getContext();
076
077    TupleEntry lhsEntry = context.argumentEntries[ 0 ];
078    TupleEntry rhsEntry = context.argumentEntries[ 1 ];
079
080    lhsEntry.setTuple( filterCall.getArguments().selectTuple( argumentSelectors[ 0 ] ) );
081    rhsEntry.setTuple( filterCall.getArguments().selectTuple( argumentSelectors[ 1 ] ) );
082
083    boolean lhsResult = filters[ 0 ].isRemove( flowProcess, context.calls[ 0 ] );
084    boolean rhsResult = filters[ 1 ].isRemove( flowProcess, context.calls[ 1 ] );
085
086    return lhsResult != rhsResult;
087    }
088  }