All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.javagimmicks.graph.AbstractGraph Maven / Gradle / Ivy

There is a newer version: 0.99-alpha1
Show newest version
package net.sf.javagimmicks.graph;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import net.sf.javagimmicks.collections.AbstractMap2;
import net.sf.javagimmicks.collections.transformer.TransformerUtils;
import net.sf.javagimmicks.lang.LangUtils;
import net.sf.javagimmicks.util.Function;
import net.sf.javagimmicks.util.Supplier;

/**
 * An abstract {@link Graph} implementation taking care about many basic
 * operations.
 */
public abstract class AbstractGraph> implements Graph
{
   protected final Supplier> _edgeSetFactory;

   protected AbstractGraph(final Supplier> edgeSetFactory)
   {
      _edgeSetFactory = edgeSetFactory;
   }

   @Override
   public Map> edgeMap()
   {
      return new EdgeMap();
   }

   @Override
   public boolean addVertex(final V vertex)
   {
      throw new UnsupportedOperationException();
   }

   @Override
   public E addEdge(final V source, final V target)
   {
      throw new UnsupportedOperationException();
   }

   @Override
   public Set addEdges(final V source, final Collection targets)
   {
      final Set result = createEdgeSet();

      for (final V target : targets)
      {
         final E edge = addEdge(source, target);
         result.add(edge);
      }

      return result;
   }

   @Override
   public boolean containsVertex(final V vertex)
   {
      return vertexSet().contains(vertex);
   }

   @Override
   public Set targetsOf(final V source)
   {
      return TransformerUtils.decorate(edgesOf(source), new EdgeToTargetTransformer(source));
   }

   @Override
   public Set getEdges(final V source, final V target)
   {
      final Set result = createEdgeSet();
      for (final E edge : edgesOf(source))
      {
         final V currentTarget = edge.getOutgoingVertex(source);

         if (LangUtils.equalsNullSafe(target, currentTarget))
         {
            result.add(edge);
         }
      }

      return result;
   }

   @Override
   public E getEdge(final V source, final V target)
   {
      for (final E edge : edgesOf(source))
      {
         final V currentTarget = edge.getOutgoingVertex(source);

         if (LangUtils.equalsNullSafe(target, currentTarget))
         {
            return edge;
         }
      }

      return null;
   }

   @Override
   public boolean isConnected(final V source, final V target)
   {
      return getEdge(source, target) != null;
   }

   @Override
   public Set removeEdges(final V source, final V target)
   {
      final Set result = createEdgeSet();

      for (final Iterator edges = edgesOf(source).iterator(); edges.hasNext();)
      {
         final E edge = edges.next();

         final V currentTarget = edge.getOutgoingVertex(source);

         if (LangUtils.equalsNullSafe(target, currentTarget))
         {
            edges.remove();

            result.add(edge);
         }
      }

      return result;
   }

   @Override
   public E removeEdge(final V source, final V target)
   {
      for (final Iterator edges = edgesOf(source).iterator(); edges.hasNext();)
      {
         final E edge = edges.next();

         final V currentTarget = edge.getOutgoingVertex(source);

         if (target == null)
         {
            if (currentTarget == null)
            {
               edges.remove();
               return edge;
            }
         }
         else if (target.equals(currentTarget))
         {
            edges.remove();
            return edge;
         }
      }

      return null;
   }

   @Override
   public Set removeEdges(final V source, final Collection targets)
   {
      final Set result = createEdgeSet();

      for (final Iterator edges = edgesOf(source).iterator(); edges.hasNext();)
      {
         final E edge = edges.next();

         if (targets.contains(edge.getOutgoingVertex(source)))
         {
            edges.remove();
            result.add(edge);
         }
      }

      return result;
   }

   @Override
   public Set removeVertex(final V vertex)
   {
      return edgeMap().remove(vertex);
   }

   protected Set createEdgeSet()
   {
      return _edgeSetFactory.get();
   }

   protected class EdgeMap extends AbstractMap2>
   {
      @Override
      public Set keySet()
      {
         return vertexSet();
      }

      @Override
      protected Set getValue(final V key)
      {
         return edgesOf(key);
      }
   }

   protected static class EdgeToTargetTransformer, V> implements Function
   {
      protected final V _source;

      public EdgeToTargetTransformer(final V source)
      {
         _source = source;
      }

      @Override
      public V apply(final E edge)
      {
         return edge.getOutgoingVertex(_source);
      }
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy