1   /*
2    * The baseCode project
3    * 
4    * Copyright (c) 2006 University of British Columbia
5    * 
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   *
18   */
19  package ubic.basecode.dataStructure.graph;
20  
21  import java.util.HashSet;
22  import java.util.Set;
23  
24  /**
25   * @author Paul Pavlidis
26   * 
27   */
28  public class UndirectedGraphNode<K, V> extends AbstractGraphNode<K, V> implements Comparable<UndirectedGraphNode<K, V>> {
29  
30      private Graph<UndirectedGraphNode<K, V>, K, V> graph;
31      private Set<UndirectedGraphNode<K, V>> neighbors;
32  
33      public UndirectedGraphNode( K key ) {
34          super( key );
35          // neighbors = new HashSet(); // FIXME why not?
36      }
37  
38      public UndirectedGraphNode( K key, V value, Graph<UndirectedGraphNode<K, V>, K, V> graph ) {
39          super( key, value );
40          this.graph = graph;
41          this.neighbors = new HashSet<UndirectedGraphNode<K, V>>();
42      }
43  
44      /*
45       * (non-Javadoc)
46       * 
47       * @see java.lang.Comparable#compareTo(java.lang.Object)
48       */
49      @Override
50      public int compareTo( UndirectedGraphNode<K, V> o ) {
51          if ( o.numNeighbors() > this.numNeighbors() ) {
52              return -1;
53          } else if ( o.numNeighbors() < this.numNeighbors() ) {
54              return 1;
55          }
56          return 0;
57      }
58  
59      /*
60       * (non-Javadoc)
61       * 
62       * @see ubic.basecode.dataStructure.graph.GraphNode#getGraph()
63       */
64      @Override
65      public Graph<UndirectedGraphNode<K, V>, K, V> getGraph() {
66          return graph;
67      }
68  
69      public int numNeighbors() {
70          if ( neighbors == null ) return 0; // shouldn't happen, see default constructor.
71          return neighbors.size();
72      }
73  
74  }