001    /*
002     * Copyright (c) 2007-2015 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    
021    package cascading.flow.stream;
022    
023    import cascading.CascadingException;
024    import cascading.flow.FlowProcess;
025    import cascading.operation.ValueAssertion;
026    import cascading.pipe.Each;
027    import cascading.pipe.OperatorException;
028    import cascading.tuple.Fields;
029    import cascading.tuple.TupleEntry;
030    
031    /**
032     *
033     */
034    public class ValueAssertionEachStage extends EachStage
035      {
036      private ValueAssertion valueAssertion;
037    
038      public ValueAssertionEachStage( FlowProcess flowProcess, Each each )
039        {
040        super( flowProcess, each );
041        }
042    
043      @Override
044      protected Fields getIncomingPassThroughFields()
045        {
046        return incomingScopes.get( 0 ).getIncomingFunctionPassThroughFields();
047        }
048    
049      @Override
050      protected Fields getIncomingArgumentsFields()
051        {
052        return incomingScopes.get( 0 ).getIncomingFunctionArgumentFields();
053        }
054    
055      @Override
056      public void initialize()
057        {
058        super.initialize();
059    
060        valueAssertion = each.getValueAssertion();
061        }
062    
063      @Override
064      public void receive( Duct previous, TupleEntry incomingEntry )
065        {
066        argumentsEntry.setTuple( argumentsBuilder.makeResult( incomingEntry.getTuple(), null ) );
067    
068        try
069          {
070          valueAssertion.doAssert( flowProcess, operationCall );
071    
072          next.receive( this, incomingEntry );
073          }
074        catch( CascadingException exception )
075          {
076          handleException( exception, argumentsEntry );
077          }
078        catch( Throwable throwable )
079          {
080          handleException( new OperatorException( each, "operator Each failed executing operation", throwable ), argumentsEntry );
081          }
082        }
083      }