    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.operation.text;
    022
    023import java.beans.ConstructorProperties;
    024import java.text.SimpleDateFormat;
    025import java.util.Calendar;
    026import java.util.Locale;
    027import java.util.TimeZone;
    028
    029import cascading.flow.FlowProcess;
    030import cascading.operation.Function;
    031import cascading.operation.FunctionCall;
    032import cascading.tuple.Fields;
    033import cascading.tuple.Tuple;
    034import cascading.util.Pair;
    035
    036/**
    037 * Class DateFormatter is used to convert a date timestamp to a formatted string, where a timestamp
    038 * is the number of milliseconds since January 1, 1970, 00:00:00 GMT,  using the {@link SimpleDateFormat} syntax.
    039 * <p/>
    040 * Note the timezone data is given to the SimpleDateFormat, not the internal Calendar instance which interprets
    041 * the 'timestamp' value as it is assumed the timestamp is already in GMT.
    042 */
    043public class DateFormatter extends DateOperation implements Function<Pair<SimpleDateFormat, Tuple>>
    044  {
    045  /** Field FIELD_NAME */
    046  public static final String FIELD_NAME = "datetime";
    047
    048  /**
    049   * Constructor DateParser creates a new DateParser instance that creates a simple long time stamp of the parsed date.
    050   *
    051   * @param dateFormatString of type String
    052   */
    053  @ConstructorProperties({"dateFormatString"})
    054  public DateFormatter( String dateFormatString )
    055    {
    056    super( 1, new Fields( FIELD_NAME ), dateFormatString );
    057    }
    058
    059  /**
    060   * Constructor DateParser creates a new DateParser instance.
    061   *
    062   * @param fieldDeclaration of type Fields
    063   * @param dateFormatString of type String
    064   */
    065  @ConstructorProperties({"fieldDeclaration", "dateFormatString"})
    066  public DateFormatter( Fields fieldDeclaration, String dateFormatString )
    067    {
    068    super( 1, fieldDeclaration, dateFormatString );
    069    }
    070
    071  /**
    072   * Constructor DateFormatter creates a new DateFormatter instance.
    073   *
    074   * @param fieldDeclaration of type Fields
    075   * @param dateFormatString of type String
    076   * @param zone             of type TimeZone
    077   */
    078  @ConstructorProperties({"fieldDeclaration", "dateFormatString", "zone"})
    079  public DateFormatter( Fields fieldDeclaration, String dateFormatString, TimeZone zone )
    080    {
    081    super( 1, fieldDeclaration, dateFormatString, zone, null );
    082    }
    083
    084  /**
    085   * Constructor DateFormatter creates a new DateFormatter instance.
    086   *
    087   * @param fieldDeclaration of type Fields
    088   * @param dateFormatString of type String
    089   * @param zone             of type TimeZone
    090   * @param locale           of type Locale
    091   */
    092  @ConstructorProperties({"fieldDeclaration", "dateFormatString", "zone", "locale"})
    093  public DateFormatter( Fields fieldDeclaration, String dateFormatString, TimeZone zone, Locale locale )
    094    {
    095    super( 1, fieldDeclaration, dateFormatString, zone, locale );
    096    }
    097
    098  @Override
    099  public void operate( FlowProcess flowProcess, FunctionCall<Pair<SimpleDateFormat, Tuple>> functionCall )
    100    {
    101    long ts = functionCall.getArguments().getLong( 0 );
    102
    103    Calendar calendar = getCalendar();
    104
    105    calendar.setTimeInMillis( ts );
    106
    107    functionCall.getContext().getRhs().set( 0, functionCall.getContext().getLhs().format( calendar.getTime() ) );
    108
    109    functionCall.getOutputCollector().add( functionCall.getContext().getRhs() );
    110    }
    111  }