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("rawtypes")
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  	 * @param <G> the graph type
24  	 * @param <E> the element type
25  	 * @param graph the graph for which the instance is created
26  	 * @param element the element for which the instance is created
27  	 * @return an instance of this type for the specified graph and element
28  	 */
29  	@SuppressWarnings("unchecked")
30  	public static <G,E> Context<G,E> getInstance(G graph, E element) {
31  		instance.graph = graph;
32  		instance.element = element;
33  		return instance;
34  	}
35  	
36  	@Override
37  	public int hashCode() {
38  		return graph.hashCode() ^ element.hashCode();
39  	}
40  	
41  	@SuppressWarnings("rawtypes")
42  	@Override
43      public boolean equals(Object o) {
44          if (!(o instanceof Context))
45              return false;
46          Context context = (Context) o;
47          return context.graph.equals(graph) && context.element.equals(element);
48      }
49  }
50