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