    001/*
    002 * Copyright (c) 2007-2016 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
    021package cascading.operation.text;
    022
    023import java.beans.ConstructorProperties;
    024
    025import cascading.flow.FlowProcess;
    026import cascading.operation.BaseOperation;
    027import cascading.operation.Function;
    028import cascading.operation.FunctionCall;
    029import cascading.operation.OperationCall;
    030import cascading.tuple.Fields;
    031import cascading.tuple.Tuple;
    032
    033/** Class FieldJoiner joins the values in a Tuple with a given delimiter and stuffs the result into a new field. */
    034public class FieldJoiner extends BaseOperation<Tuple> implements Function<Tuple>
    035  {
    036  /** Field FIELD_NAME */
    037  public static final String FIELD_NAME = "joined";
    038
    039  /** Field delimiter */
    040  private String delimiter = "\t";
    041
    042  /**
    043   * Constructor FieldJoiner creates a new FieldJoiner instance.
    044   *
    045   * @param delimiter of type String
    046   */
    047  @ConstructorProperties({"delimiter"})
    048  public FieldJoiner( String delimiter )
    049    {
    050    this( new Fields( FIELD_NAME ) );
    051    this.delimiter = delimiter;
    052    }
    053
    054  /**
    055   * Constructor FieldJoiner creates a new FieldJoiner instance.
    056   *
    057   * @param fieldDeclaration of type Fields
    058   */
    059  @ConstructorProperties({"fieldDeclaration"})
    060  public FieldJoiner( Fields fieldDeclaration )
    061    {
    062    super( fieldDeclaration );
    063    }
    064
    065  /**
    066   * Constructor FieldJoiner creates a new FieldJoiner instance.
    067   *
    068   * @param fieldDeclaration of type Fields
    069   * @param delimiter        of type String
    070   */
    071  @ConstructorProperties({"fieldDeclaration", "delimiter"})
    072  public FieldJoiner( Fields fieldDeclaration, String delimiter )
    073    {
    074    super( fieldDeclaration );
    075    this.delimiter = delimiter;
    076    }
    077
    078  /**
    079   * Method getFormat returns the delimiter of this FieldJoiner object.
    080   *
    081   * @return the delimiter (type String) of this FieldJoiner object.
    082   */
    083  public String getDelimiter()
    084    {
    085    return delimiter;
    086    }
    087
    088  @Override
    089  public void prepare( FlowProcess flowProcess, OperationCall<Tuple> operationCall )
    090    {
    091    operationCall.setContext( Tuple.size( 1 ) );
    092    }
    093
    094  @Override
    095  public void operate( FlowProcess flowProcess, FunctionCall<Tuple> functionCall )
    096    {
    097    functionCall.getContext().set( 0, functionCall.getArguments().getTuple().toString( delimiter, false ) );
    098    functionCall.getOutputCollector().add( functionCall.getContext() );
    099    }
    100
    101  @Override
    102  public boolean equals( Object object )
    103    {
    104    if( this == object )
    105      return true;
    106    if( !( object instanceof FieldJoiner ) )
    107      return false;
    108    if( !super.equals( object ) )
    109      return false;
    110
    111    FieldJoiner that = (FieldJoiner) object;
    112
    113    if( delimiter != null ? !delimiter.equals( that.delimiter ) : that.delimiter != null )
    114      return false;
    115
    116    return true;
    117    }
    118
    119  @Override
    120  public int hashCode()
    121    {
    122    int result = super.hashCode();
    123    result = 31 * result + ( delimiter != null ? delimiter.hashCode() : 0 );
    124    return result;
    125    }
    126  }