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.pipe.assembly; 023 024import java.beans.ConstructorProperties; 025 026import cascading.operation.Identity; 027import cascading.pipe.Each; 028import cascading.pipe.Pipe; 029import cascading.pipe.SubAssembly; 030import cascading.tuple.Fields; 031 032/** 033 * Class Rename is a {@link SubAssembly} that will rename the fromFields to the names in toFields. 034 * <p> 035 * Note that if any input field names are not given, they will retain their names. 036 */ 037public class Rename extends SubAssembly 038 { 039 /** 040 * Rename the fromFields in the current Tuple to the given toFields. 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 }