001/*
002 * Copyright (c) 2016-2017 Chris K Wensel. 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.nested.core;
022
023import java.util.function.Function;
024
025import cascading.flow.FlowProcess;
026import cascading.operation.FunctionCall;
027import cascading.operation.OperationCall;
028import cascading.tuple.Fields;
029import cascading.tuple.TupleEntry;
030
031/**
032 *
033 */
034public abstract class NestedSpecBaseOperation<Node, Result, Context> extends NestedBaseOperation<Node, Result, Context>
035  {
036  transient Function<OperationCall<Context>, Node> resultNodeFunction = null;
037
038  public NestedSpecBaseOperation( NestedCoercibleType<Node, Result> nestedCoercibleType, Fields fieldDeclaration )
039    {
040    super( nestedCoercibleType, fieldDeclaration );
041    }
042
043  public NestedSpecBaseOperation( NestedCoercibleType<Node, Result> nestedCoercibleType, int numArgs, Fields fieldDeclaration )
044    {
045    super( nestedCoercibleType, numArgs, fieldDeclaration );
046    }
047
048  @Override
049  public void prepare( FlowProcess flowProcess, OperationCall<Context> operationCall )
050    {
051    if( isInto() )
052      resultNodeFunction = this::getArgument;
053    else
054      resultNodeFunction = o -> getRootNode();
055    }
056
057  protected abstract boolean isInto();
058
059  protected Node getResultNode( OperationCall<Context> operationCall )
060    {
061    return resultNodeFunction.apply( operationCall );
062    }
063
064  protected Node getArgument( OperationCall<?> operationCall )
065    {
066    TupleEntry arguments = ( (FunctionCall<Object>) operationCall ).getArguments();
067
068    Node object = (Node) arguments.getObject( arguments.size() - 1, getCoercibleType() );
069
070    return deepCopy( object );
071    }
072  }