View Javadoc
1   /*
2    * Created on Oct 18, 2005
3    *
4    * Copyright (c) 2005, The JUNG Authors 
5    *
6    * All rights reserved.
7    *
8    * This software is open-source under the BSD license; see either
9    * "license.txt" or
10   * https://github.com/jrtom/jung/blob/master/LICENSE for a description.
11   */
12  package edu.uci.ics.jung.graph;
13  
14  import java.util.Comparator;
15  import java.util.Map;
16  import java.util.Set;
17  import java.util.TreeMap;
18  import java.util.TreeSet;
19  
20  import com.google.common.base.Supplier;
21  import com.google.common.collect.Ordering;
22  
23  import edu.uci.ics.jung.graph.util.Pair;
24  
25  /**
26   * An implementation of <code>Graph</code> that is suitable for sparse graphs,
27   * orders its vertex and edge collections according to either specified <code>Comparator</code>
28   * instances or the natural ordering of their elements, and permits directed, undirected,
29   * and parallel edges. 
30   * 
31   * @author Joshua O'Madadhain
32   */
33  @SuppressWarnings("serial")
34  public class SortedSparseMultigraph<V,E> 
35      extends OrderedSparseMultigraph<V,E>
36      implements MultiGraph<V,E> 
37  {
38      /**
39       * @param <V> the vertex type for the graph Supplier
40       * @param <E> the edge type for the graph Supplier
41       * @return a {@code Supplier} that creates an instance of this graph type.
42       */
43  	public static <V,E> Supplier<Graph<V,E>> getFactory() 
44  	{ 
45  		return new Supplier<Graph<V,E>> () 
46  		{
47  			public Graph<V,E> get() 
48  			{
49  				return new SortedSparseMultigraph<V,E>();
50  			}
51  		};
52  	}
53      
54      /**
55       * <code>Comparator</code> used in ordering vertices.  Defaults to <code>util.ComparableComparator</code>
56       * if no comparators are specified in the constructor.
57       */
58      protected Comparator<V> vertex_comparator;
59  
60      /**
61       * <code>Comparator</code> used in ordering edges.  Defaults to <code>util.ComparableComparator</code>
62       * if no comparators are specified in the constructor.
63       */
64      protected Comparator<E> edge_comparator;
65      
66      /**
67       * Creates a new instance which sorts its vertices and edges according to the 
68       * specified {@code Comparator}s.
69       * @param vertex_comparator specifies how the vertices are to be compared 
70       * @param edge_comparator specifies how the edges are to be compared
71       */
72      public SortedSparseMultigraph(Comparator<V> vertex_comparator, Comparator<E> edge_comparator)
73      {
74          this.vertex_comparator = vertex_comparator;
75          this.edge_comparator = edge_comparator;
76          vertices = new TreeMap<V, Pair<Set<E>>>(vertex_comparator);
77          edges = new TreeMap<E, Pair<V>>(edge_comparator);
78          directedEdges = new TreeSet<E>(edge_comparator);
79      }
80      
81      /**
82       * Creates a new instance which sorts its vertices and edges according to 
83       * their natural ordering.
84       */
85      public SortedSparseMultigraph()
86      {
87          this(new Ordering<V>(){
88  			@SuppressWarnings("unchecked")
89  			public int compare(V v1, V v2) {
90  				return ((Comparable<V>) v1).compareTo(v2);
91  			}},
92          		new Ordering<E>(){
93  					@SuppressWarnings("unchecked")
94  					public int compare(E e1, E e2) {
95  						return ((Comparable<E>) e1).compareTo(e2);
96  					}});
97      }
98  
99      /**
100      * Provides a new {@code Comparator} to be used in sorting the vertices.
101      * @param vertex_comparator the comparator that defines the new ordering
102      */
103     public void setVertexComparator(Comparator<V> vertex_comparator)
104     {
105         this.vertex_comparator = vertex_comparator;
106         Map<V, Pair<Set<E>>> tmp_vertices = new TreeMap<V, Pair<Set<E>>>(vertex_comparator);
107         for (Map.Entry<V, Pair<Set<E>>> entry : vertices.entrySet())
108             tmp_vertices.put(entry.getKey(), entry.getValue());
109         this.vertices = tmp_vertices;
110     }
111     
112     @Override
113     public boolean addVertex(V vertex) {
114         if(vertex == null) {
115             throw new IllegalArgumentException("vertex may not be null");
116         }
117         if (!containsVertex(vertex)) 
118         {
119             vertices.put(vertex, new Pair<Set<E>>(new TreeSet<E>(edge_comparator), 
120                 new TreeSet<E>(edge_comparator)));
121             return true;
122         } 
123         else 
124         {
125         	return false;
126         }
127     }
128 }