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.pipe.joiner;
022
023import java.beans.ConstructorProperties;
024import java.util.Arrays;
025import java.util.Iterator;
026
027import cascading.tuple.Fields;
028import cascading.tuple.Tuple;
029
030/**
031 * Class MixedJoin will return an {@link java.util.Iterator} that will iterate over a given
032 * {@link Joiner} and return tuples that represent a join as defined by the given boolean array.
033 * <p/>
034 * So if joining three streams, {@code boolean []{true,false,false}} will result in a 'inner', 'outer', 'outer' join.
035 * <p/>
036 * Joins perform based on the equality of the join keys. In the case of null values, Java treats two
037 * null values as equivalent. SQL does not treat null values as equal. To produce SQL like results in a given
038 * join, a new {@link java.util.Comparator} will need to be used on the joined values to prevent null from
039 * equaling null. As a convenience, see the {@link cascading.util.NullNotEquivalentComparator} class.
040 */
041public class MixedJoin extends BaseJoiner
042  {
043  /** Field INNER */
044  public static boolean INNER = true;
045  /** Field OUTER */
046  public static boolean OUTER = false;
047
048  final boolean[] asInner;
049
050  /**
051   * Constructor MixedJoin creates a new MixedJoin instance.
052   *
053   * @param asInner of type boolean[]
054   */
055  @ConstructorProperties({"asInner"})
056  public MixedJoin( boolean[] asInner )
057    {
058    this.asInner = Arrays.copyOf( asInner, asInner.length );
059    }
060
061  @ConstructorProperties({"fieldDeclaration", "asInner"})
062  public MixedJoin( Fields fieldDeclaration, boolean[] asInner )
063    {
064    super( fieldDeclaration );
065    this.asInner = asInner;
066    }
067
068  /** @see Joiner#numJoins() */
069  public int numJoins()
070    {
071    return asInner.length - 1;
072    }
073
074  public Iterator<Tuple> getIterator( JoinerClosure closure )
075    {
076    return new JoinIterator( closure );
077    }
078
079  public class JoinIterator extends OuterJoin.JoinIterator
080    {
081    public JoinIterator( JoinerClosure closure )
082      {
083      super( closure );
084      }
085
086    @Override
087    protected boolean isOuter( int i )
088      {
089      return !asInner[ i ] && super.isOuter( i );
090      }
091    }
092  }