    001/*
    002 * Copyright (c) 2016 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.flow.planner.iso.expression;
    023
    024import cascading.flow.planner.PlannerContext;
    025import cascading.flow.planner.Scope;
    026import cascading.flow.planner.graph.ElementGraph;
    027
    028/**
    029 * All is a special case allowing allowing a node-node match with any number of edges
    030 */
    031public class PathScopeExpression extends ScopeExpression
    032  {
    033  public static final PathScopeExpression BLOCKING = new PathScopeExpression( Mode.Blocking );
    034  public static final PathScopeExpression NON_BLOCKING = new PathScopeExpression( Mode.NonBlocking );
    035
    036  public static final PathScopeExpression ALL_BLOCKING = new PathScopeExpression( Applies.All, Mode.Blocking );
    037  public static final PathScopeExpression ALL_NON_BLOCKING = new PathScopeExpression( Applies.All, Mode.NonBlocking );
    038
    039  public static final PathScopeExpression ANY_BLOCKING = new PathScopeExpression( Applies.Any, Mode.Blocking );
    040  public static final PathScopeExpression ANY_NON_BLOCKING = new PathScopeExpression( Applies.Any, Mode.NonBlocking );
    041
    042  public static final PathScopeExpression ALL_BLOCKING_NO_CAPTURE = new PathScopeExpression( false, Applies.All, Mode.Blocking );
    043  public static final PathScopeExpression ALL_NON_BLOCKING_NO_CAPTURE = new PathScopeExpression( false, Applies.All, Mode.NonBlocking );
    044
    045  public enum Mode
    046    {
    047      Ignore, Blocking, NonBlocking
    048    }
    049
    050  private Mode mode = Mode.Ignore;
    051
    052  public PathScopeExpression()
    053    {
    054    }
    055
    056  public PathScopeExpression( Applies applies )
    057    {
    058    this.applies = applies;
    059    }
    060
    061  public PathScopeExpression( boolean capture, Applies applies )
    062    {
    063    super( capture, applies );
    064    }
    065
    066  public PathScopeExpression( Mode mode )
    067    {
    068    this.mode = mode;
    069    }
    070
    071  public PathScopeExpression( Applies applies, Mode mode )
    072    {
    073    super( applies );
    074    this.mode = mode;
    075    }
    076
    077  public PathScopeExpression( boolean capture, Applies applies, Mode mode )
    078    {
    079    super( capture, applies );
    080    this.mode = mode;
    081    }
    082
    083  @Override
    084  public boolean acceptsAll()
    085    {
    086    return appliesToAllPaths() && isIgnoreMode();
    087    }
    088
    089  public boolean isIgnoreMode()
    090    {
    091    return mode == Mode.Ignore;
    092    }
    093
    094  public Mode getMode()
    095    {
    096    return mode;
    097    }
    098
    099  @Override
    100  public boolean applies( PlannerContext plannerContext, ElementGraph elementGraph, Scope scope )
    101    {
    102    switch( mode )
    103      {
    104      case Ignore:
    105        return true;
    106      case Blocking:
    107        return !scope.isNonBlocking();
    108      case NonBlocking:
    109        return scope.isNonBlocking();
    110      default:
    111        throw new IllegalStateException( "should never reach here" );
    112      }
    113    }
    114
    115  @Override
    116  public String toString()
    117    {
    118    final StringBuilder sb = new StringBuilder( "PathScopeExpression{" );
    119    sb.append( "capture=" ).append( capture );
    120    sb.append( ", applies=" ).append( applies );
    121    sb.append( ", mode=" ).append( mode );
    122    sb.append( '}' );
    123    return sb.toString();
    124    }
    125  }