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.pipe.assembly; 022 023import java.beans.ConstructorProperties; 024 025import cascading.operation.Identity; 026import cascading.pipe.Each; 027import cascading.pipe.Pipe; 028import cascading.pipe.SubAssembly; 029import cascading.tuple.Fields; 030 031/** 032 * Class Rename is a {@link SubAssembly} that will rename the fromFields to the names in toFields. 033 * <p/> 034 * Note that if any input field names are not given, they will retain their names. 035 */ 036public class Rename extends SubAssembly 037 { 038 /** 039 * Rename the fromFields in the current Tuple to the given toFields. 040 * <p/> 041 * <pre> 042 * incoming: {"first", "middle", "last"} -> from:{"middle"} to:{"initial"} -> outgoing:{"first", "last", "initial"} 043 * </pre> 044 * 045 * @param previous of type Pipe 046 * @param fromFields of type Fields 047 * @param toFields of type Fields 048 */ 049 @ConstructorProperties({"previous", "fromFields", "toFields"}) 050 public Rename( Pipe previous, Fields fromFields, Fields toFields ) 051 { 052 super( previous ); 053 054 if( fromFields == null ) 055 throw new IllegalArgumentException( "fromFields may not be null" ); 056 057 if( toFields == null ) 058 throw new IllegalArgumentException( "toFields may not be null" ); 059 060 if( fromFields.isDefined() && fromFields.size() != toFields.size() ) 061 throw new IllegalArgumentException( "fields arguments must be same size, from: " + fromFields.printVerbose() + " to: " + toFields.printVerbose() ); 062 063 if( !toFields.isDefined() ) 064 throw new IllegalArgumentException( "toFields must define field names: " + toFields.printVerbose() ); 065 066 setTails( new Each( previous, fromFields, new Identity( toFields ), Fields.SWAP ) ); 067 } 068 }