protected TupleBuilder createOutgoingBuilder( final Operator operator, final Fields incomingFields, final Fields argumentSelector, final Fields remainderFields, final Fields declaredFields, final Fields outgoingSelector )
{
final Fields inputDeclarationFields = Fields.asDeclaration( incomingFields );
if( operator.getOutputSelector().isResults() )
return new TupleBuilder()
{
@Override
public Tuple makeResult( Tuple input, Tuple output )
{
return output;
}
};
if( operator.getOutputSelector().isAll() && !( incomingFields.isUnknown() || declaredFields.isUnknown() ) )
return new TupleBuilder()
{
Tuple result = createComposite( inputDeclarationFields, declaredFields );
@Override
public Tuple makeResult( Tuple input, Tuple output )
{
return TupleViews.reset( result, input, output );
}
};
if( operator.getOutputSelector().isReplace() )
{
if( incomingFields.isUnknown() )
return new TupleBuilder()
{
Fields resultFields = operator.getFieldDeclaration().isArguments() ? argumentSelector : declaredFields;
@Override
public Tuple makeResult( Tuple input, Tuple output )
{
Tuple result = new Tuple( input );
result.set( Fields.UNKNOWN, resultFields, output );
return result;
}
};
return new TupleBuilder()
{
Fields resultFields = operator.getFieldDeclaration().isArguments() ? argumentSelector : declaredFields;
Tuple result = createOverride( inputDeclarationFields, resultFields );
@Override
public Tuple makeResult( Tuple input, Tuple output )
{
return TupleViews.reset( result, input, output );
}
};
}
if( operator.getOutputSelector().isSwap() )
{
if( remainderFields.size() == 0 ) // the same as Fields.RESULTS
return new TupleBuilder()
{
@Override
public Tuple makeResult( Tuple input, Tuple output )
{
return output;
}
};
else if( declaredFields.isUnknown() )
return new TupleBuilder()
{
@Override
public Tuple makeResult( Tuple input, Tuple output )
{
return input.get( incomingFields, remainderFields ).append( output );
}
};
else
return new TupleBuilder()
{
Tuple view = createNarrow( inputDeclarationFields.getPos( remainderFields ) );
Tuple result = createComposite( Fields.asDeclaration( remainderFields ), declaredFields );
@Override
public Tuple makeResult( Tuple input, Tuple output )
{
TupleViews.reset( view, input );
return TupleViews.reset( result, view, output );
}
};
}
if( incomingFields.isUnknown() || declaredFields.isUnknown() )
return new TupleBuilder()
{
Fields selector = outgoingSelector.isUnknown() ? Fields.ALL : outgoingSelector;
TupleEntry incoming = new TupleEntry( incomingFields, true );
TupleEntry declared = new TupleEntry( declaredFields, true );
@Override
public Tuple makeResult( Tuple input, Tuple output )
{
incoming.setTuple( input );
declared.setTuple( output );
return TupleEntry.select( selector, incoming, declared );
}
};
return new TupleBuilder()
{
Fields inputFields = operator.getFieldDeclaration().isArguments() ? Fields.mask( inputDeclarationFields, declaredFields ) : inputDeclarationFields;
Tuple appended = createComposite( inputFields, declaredFields );
Fields allFields = Fields.resolve( Fields.ALL, inputFields, declaredFields );
Tuple result = createNarrow( allFields.getPos( outgoingSelector ), appended );