View Javadoc
1   /**
2    * Copyright (c) 2008, The JUNG Authors 
3    *
4    * All rights reserved.
5    *
6    * This software is open-source under the BSD license; see either
7    * "license.txt" or
8    * https://github.com/jrtom/jung/blob/master/LICENSE for a description.
9    * Created on Sep 1, 2008
10   * 
11   */
12  package edu.uci.ics.jung.graph;
13  
14  import java.util.Collection;
15  import java.util.Collections;
16  
17  import edu.uci.ics.jung.graph.util.EdgeType;
18  
19  /**
20   * An abstract class for graphs whose edges all have the same {@code EdgeType}.
21   * Intended to simplify the implementation of such graph classes.
22   */
23  @SuppressWarnings("serial")
24  public abstract class AbstractTypedGraph<V,E> extends AbstractGraph<V, E> 
25  {
26  	/**
27  	 * The edge type for all edges in this graph.
28  	 */
29  	protected final EdgeType edge_type;
30  	
31  	/**
32  	 * Creates an instance with the specified edge type.
33  	 * @param edge_type the type of edges that this graph accepts
34  	 */
35  	public AbstractTypedGraph(EdgeType edge_type)
36  	{
37  		this.edge_type = edge_type;
38  	}
39  	
40  	/**
41  	 * Returns this graph's edge type.
42  	 */
43  	public EdgeType getDefaultEdgeType()
44  	{
45  		return this.edge_type;
46  	}
47  	
48  	/**
49  	 * Returns this graph's edge type, or {@code null} if {@code e} is not
50  	 * in this graph.
51  	 */
52  	public EdgeType getEdgeType(E e)
53  	{
54  		return hasEqualEdgeType(edge_type) ? this.edge_type : null;
55  	}
56  	
57  	/**
58  	 * Returns the edge set for this graph if {@code edgeType} matches the 
59  	 * edge type for this graph, and an empty set otherwise.
60  	 */
61  	public Collection<E> getEdges(EdgeType edge_type) 
62  	{
63  		return hasEqualEdgeType(edge_type) ? this.getEdges() : Collections.<E>emptySet();
64  	}
65  
66  	/**
67  	 * Returns the edge count for this graph if {@code edge_type} matches
68  	 * the edge type for this graph, and 0 otherwise.
69  	 */
70      public int getEdgeCount(EdgeType edge_type)
71      {
72      	return hasEqualEdgeType(edge_type) ? this.getEdgeCount() : 0;
73      }
74      
75      /**
76       * @param edge_type the edge type to compare to this instance's default edge type
77       * @return {@code true} if {@code edge_type} matches the default edge type for 
78       * this graph, and {@code false} otherwise
79       */
80      protected boolean hasEqualEdgeType(EdgeType edge_type)
81      {
82      	return this.edge_type.equals(edge_type);
83      }
84  
85      /**
86       * Throws an {@code IllegalArgumentException} if {@code edge_type} does not
87       * match the default edge type for this graph.
88       * @param edge_type the edge type to compare to this instance's default edge type
89       */
90      protected void validateEdgeType(EdgeType edge_type)
91      {
92      	if (!hasEqualEdgeType(edge_type))
93      		throw new IllegalArgumentException("Edge type '" + edge_type + 
94      				"' does not match the default edge type for this graph: '" + 
95      				this.edge_type + "'");
96      }
97  }