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