001/*
002 * Copyright (c) 2016-2017 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.graph;
023
024import java.util.Arrays;
025import java.util.Collection;
026import java.util.HashSet;
027import java.util.Set;
028import java.util.function.Predicate;
029
030import cascading.flow.FlowElement;
031import cascading.flow.planner.Scope;
032import org.jgrapht.Graph;
033import org.jgrapht.graph.MaskSubgraph;
034
035import static cascading.flow.planner.graph.ElementGraphs.directed;
036import static cascading.util.Util.createIdentitySet;
037
038/**
039 *
040 */
041public class ElementMaskSubGraph extends BaseElementGraph implements ElementGraph
042  {
043  private ElementGraph elementGraph;
044  private VertexMask vertexMask;
045  private EdgeMask edgeMask;
046
047  private static class VertexMask implements Predicate<FlowElement>
048    {
049    Set<FlowElement> maskedElements = createIdentitySet();
050
051    public VertexMask( Collection<FlowElement> flowElements )
052      {
053      if( flowElements != null )
054        maskedElements.addAll( flowElements );
055      }
056
057    @Override
058    public boolean test( FlowElement flowElement )
059      {
060      return maskedElements.contains( flowElement );
061      }
062    }
063
064  private static class EdgeMask implements Predicate<Scope>
065    {
066    Set<Scope> maskedScopes = new HashSet<>();
067
068    public EdgeMask( Collection<Scope> scopes )
069      {
070      if( scopes != null )
071        maskedScopes.addAll( scopes );
072      }
073
074    @Override
075    public boolean test( Scope scope )
076      {
077      return maskedScopes.contains( scope );
078      }
079    }
080
081  public ElementMaskSubGraph( ElementGraph elementGraph, FlowElement... maskedFlowElements )
082    {
083    this( elementGraph, new VertexMask( Arrays.asList( maskedFlowElements ) ), new EdgeMask( null ) );
084    }
085
086  public ElementMaskSubGraph( ElementGraph elementGraph, Collection<FlowElement> maskedFlowElements )
087    {
088    this( elementGraph, new VertexMask( maskedFlowElements ), new EdgeMask( null ) );
089    }
090
091  public ElementMaskSubGraph( ElementGraph elementGraph, Collection<FlowElement> maskedFlowElements, Collection<Scope> maskedScopes )
092    {
093    this( elementGraph, new VertexMask( maskedFlowElements ), new EdgeMask( maskedScopes ) );
094    }
095
096  public ElementMaskSubGraph( ElementMaskSubGraph graph )
097    {
098    this( graph.elementGraph, graph.vertexMask, graph.edgeMask );
099    }
100
101  protected ElementMaskSubGraph( ElementGraph elementGraph, VertexMask vertexMask, EdgeMask edgeMask )
102    {
103    this.graph = new DirectedMaskSubGraph( directed( elementGraph ), vertexMask, edgeMask );
104
105    this.elementGraph = elementGraph;
106    this.vertexMask = vertexMask;
107    this.edgeMask = edgeMask;
108    }
109
110  @Override
111  public ElementGraph copyElementGraph()
112    {
113    return new ElementMaskSubGraph( ElementMaskSubGraph.this );
114    }
115
116  private class DirectedMaskSubGraph extends MaskSubgraph<FlowElement, Scope>
117    {
118    public DirectedMaskSubGraph( Graph<FlowElement, Scope> base, VertexMask vertexMask, EdgeMask edgeMask )
119      {
120      super( base, vertexMask, edgeMask );
121      }
122    }
123  }