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