Home | History | Annotate | Download | only in grapher
      1 /**
      2  * Copyright (C) 2011 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.inject.grapher;
     18 
     19 import com.google.common.base.Objects;
     20 
     21 /**
     22  * Node in a guice dependency graph.
     23  *
     24  * @author bojand (at) google.com (Bojan Djordjevic)
     25  * @since 4.0
     26  */
     27 public abstract class Node {
     28   /**
     29    * When set to true, the source object is ignored in {@link #equals} and {@link #hashCode}.
     30    * Only used in tests.
     31    */
     32   static boolean ignoreSourceInComparisons = false;
     33 
     34   private final NodeId id;
     35   private final Object source;
     36 
     37   protected Node(NodeId id, Object source) {
     38     this.id = id;
     39     this.source = source;
     40   }
     41 
     42   public NodeId getId() {
     43     return id;
     44   }
     45 
     46   public Object getSource() {
     47     return source;
     48   }
     49 
     50   @Override public boolean equals(Object obj) {
     51     if (!(obj instanceof Node)) {
     52       return false;
     53     }
     54     Node other = (Node) obj;
     55     return Objects.equal(id, other.id)
     56         && (ignoreSourceInComparisons || Objects.equal(source, other.source));
     57   }
     58 
     59   @Override public int hashCode() {
     60     return ignoreSourceInComparisons ? id.hashCode() : Objects.hashCode(id, source);
     61   }
     62 
     63   /**
     64    * Returns a copy of the node with a new ID.
     65    *
     66    * @param id new ID of the node
     67    * @return copy of the node with a new ID
     68    */
     69   public abstract Node copy(NodeId id);
     70 }
     71