    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.stream.duct;
    023
    024import org.jgrapht.EdgeFactory;
    025import org.jgrapht.graph.DirectedMultigraph;
    026
    027/**
    028 *
    029 */
    030public class DuctGraph extends DirectedMultigraph<Duct, DuctGraph.Ordinal>
    031  {
    032  private static class DuctOrdinalEdgeFactory implements EdgeFactory<Duct, Ordinal>
    033    {
    034    int count = 0;
    035
    036    @Override
    037    public DuctGraph.Ordinal createEdge( Duct lhs, Duct rhs )
    038      {
    039      return makeOrdinal( 0 );
    040      }
    041
    042    public DuctGraph.Ordinal makeOrdinal( int ordinal )
    043      {
    044      return new DuctGraph.Ordinal( count++, ordinal );
    045      }
    046    }
    047
    048  public static class Ordinal
    049    {
    050    int count;
    051    int ordinal;
    052
    053    public Ordinal( int count, int ordinal )
    054      {
    055      this.count = count;
    056      this.ordinal = ordinal;
    057      }
    058
    059    public int getOrdinal()
    060      {
    061      return ordinal;
    062      }
    063
    064    @Override
    065    public boolean equals( Object object )
    066      {
    067      if( this == object )
    068        return true;
    069
    070      Ordinal ordinal = (Ordinal) object;
    071
    072      if( count != ordinal.count )
    073        return false;
    074
    075      return true;
    076      }
    077
    078    @Override
    079    public int hashCode()
    080      {
    081      return count;
    082      }
    083
    084    @Override
    085    public String toString()
    086      {
    087      final StringBuilder sb = new StringBuilder( "Ordinal{" );
    088      sb.append( "count=" ).append( count );
    089      sb.append( ", ordinal=" ).append( ordinal );
    090      sb.append( '}' );
    091      return sb.toString();
    092      }
    093    }
    094
    095  public DuctGraph()
    096    {
    097    super( new DuctOrdinalEdgeFactory() );
    098    }
    099
    100  public synchronized DuctGraph.Ordinal makeOrdinal( int ordinal )
    101    {
    102    return ( (DuctOrdinalEdgeFactory) getEdgeFactory() ).makeOrdinal( ordinal );
    103    }
    104  }