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@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}. Only
30    * used in tests.
31    */
32   static boolean ignoreSourceInComparisons = false;
33 
34   private final NodeId id;
35   private final Object source;
36 
Node(NodeId id, Object source)37   protected Node(NodeId id, Object source) {
38     this.id = id;
39     this.source = source;
40   }
41 
getId()42   public NodeId getId() {
43     return id;
44   }
45 
getSource()46   public Object getSource() {
47     return source;
48   }
49 
50   @Override
equals(Object obj)51   public boolean equals(Object obj) {
52     if (!(obj instanceof Node)) {
53       return false;
54     }
55     Node other = (Node) obj;
56     return Objects.equal(id, other.id)
57         && (ignoreSourceInComparisons || Objects.equal(source, other.source));
58   }
59 
60   @Override
hashCode()61   public int hashCode() {
62     return ignoreSourceInComparisons ? id.hashCode() : Objects.hashCode(id, source);
63   }
64 
65   /**
66    * Returns a copy of the node with a new ID.
67    *
68    * @param id new ID of the node
69    * @return copy of the node with a new ID
70    */
copy(NodeId id)71   public abstract Node copy(NodeId id);
72 }
73