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.IdentityHashMap;
025
026import cascading.flow.FlowElement;
027import cascading.flow.planner.Scope;
028import cascading.util.EnumMultiMap;
029import org.jgrapht.Graphs;
030import org.jgrapht.graph.SimpleDirectedGraph;
031import org.jgrapht.graph.specifics.DirectedEdgeContainer;
032import org.jgrapht.graph.specifics.DirectedSpecifics;
033
034import static cascading.flow.planner.graph.ElementGraphs.directed;
035
036/**
037 *
038 */
039public class ElementDirectedGraph extends BaseAnnotatedElementGraph implements AnnotatedGraph
040  {
041  public ElementDirectedGraph()
042    {
043    this.graph = new DirectedGraph();
044    }
045
046  public ElementDirectedGraph( ElementGraph parent )
047    {
048    if( parent == null )
049      {
050      this.graph = new DirectedGraph();
051      return;
052      }
053
054    this.graph = new DirectedGraph( directed( parent ) );
055
056    addParentAnnotations( parent );
057    }
058
059  public ElementDirectedGraph( ElementGraph parent, EnumMultiMap annotations )
060    {
061    this( parent );
062
063    getAnnotations().addAll( annotations );
064    }
065
066  @Override
067  public ElementGraph copyElementGraph()
068    {
069    return new ElementDirectedGraph( this );
070    }
071
072  private class DirectedGraph extends SimpleDirectedGraph<FlowElement, Scope>
073    {
074    public DirectedGraph()
075      {
076      super( Scope.class );
077      }
078
079    public DirectedGraph( org.jgrapht.Graph<FlowElement, Scope> parent )
080      {
081      this();
082
083      Graphs.addGraph( this, parent );
084      }
085
086    @Override
087    protected DirectedSpecifics createSpecifics( boolean directed )
088      {
089      return new DirectedSpecifics( this, new IdentityHashMap<FlowElement, DirectedEdgeContainer<FlowElement, Scope>>() );
090      }
091    }
092  }