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;
022    
023    import java.beans.ConstructorProperties;
024    
025    import cascading.flow.FlowProcess;
026    import cascading.tuple.Fields;
027    import cascading.tuple.Tuple;
028    
029    /** Class Insert adds literal values to the Tuple stream. */
030    public class Insert extends BaseOperation implements Function
031      {
032      /** Field values */
033      private final Tuple values;
034    
035      /**
036       * Constructor Insert creates a new Insert instance with the given fields and values.
037       *
038       * @param fieldDeclaration of type Fields
039       * @param values           of type Object...
040       */
041      @ConstructorProperties({"fieldDeclaration", "values"})
042      public Insert( Fields fieldDeclaration, Object... values )
043        {
044        super( 0, fieldDeclaration );
045        this.values = new Tuple( values );
046    
047        if( !fieldDeclaration.isSubstitution() && fieldDeclaration.size() != values.length )
048          throw new IllegalArgumentException( "fieldDeclaration must be the same size as the given values" );
049        }
050    
051      public Tuple getValues()
052        {
053        return new Tuple( values );
054        }
055    
056      /** @see Function#operate(cascading.flow.FlowProcess, FunctionCall) */
057      public void operate( FlowProcess flowProcess, FunctionCall functionCall )
058        {
059        functionCall.getOutputCollector().add( values );
060        }
061    
062      @Override
063      public boolean equals( Object object )
064        {
065        if( this == object )
066          return true;
067        if( !( object instanceof Insert ) )
068          return false;
069        if( !super.equals( object ) )
070          return false;
071    
072        Insert insert = (Insert) object;
073    
074        if( values != null ? !values.equals( insert.values ) : insert.values != null )
075          return false;
076    
077        return true;
078        }
079    
080      @Override
081      public int hashCode()
082        {
083        int result = super.hashCode();
084        result = 31 * result + ( values != null ? values.hashCode() : 0 );
085        return result;
086        }
087      }