View Javadoc

1   package edu.uci.ics.jung.graph.util;
2   
3   /**
4    * A class that is used to link together a graph element and a specific graph.
5    * Provides appropriate implementations of <code>hashCode</code> and <code>equals</code>.
6    */
7   public class Context<G,E> 
8   {
9   	@SuppressWarnings("unchecked")
10  	private static Context instance = new Context();
11  	
12  	/**
13  	 * The graph element which defines this context.
14  	 */
15  	public G graph;
16  
17  	/**
18  	 * The edge element which defines this context.
19  	 */
20  	public E element;
21  	
22  	/**
23  	 * Returns an instance of this type for the specified graph and element.
24  	 * @param <G> the graph type
25  	 * @param <E> the element type
26  	 */
27  	@SuppressWarnings("unchecked")
28  	public static <G,E> Context<G,E> getInstance(G graph, E element) {
29  		instance.graph = graph;
30  		instance.element = element;
31  		return instance;
32  	}
33  	
34  	@Override
35  	public int hashCode() {
36  		return graph.hashCode() ^ element.hashCode();
37  	}
38  	
39  	@SuppressWarnings("unchecked")
40  	@Override
41      public boolean equals(Object o) {
42          if (!(o instanceof Context))
43              return false;
44          Context context = (Context)o;
45          return context.graph.equals(graph) && context.element.equals(element);
46      }
47  }
48