• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import com.google.inject.Key;
21 
22 /**
23  * ID of a node in the graph. An ID is given by a {@link Key} and a node type, which is used to
24  * distinguish instances and implementation classes for the same key. For example
25  * {@code bind(Integer.class).toInstance(42)} produces two nodes: an
26  * interface node with the key of {@code Key<Integer>} and an instance node with the same
27  * {@link Key} and value of 42.
28  *
29  * @author bojand@google.com (Bojan Djordjevic)
30  * @since 4.0
31  */
32 public final class NodeId {
33 
34   /**
35    * Type of node.
36    *
37    * @since 4.0
38    */
39   public enum NodeType {
40     /** Type or class node. */
41     TYPE,
42 
43     /** Instance node, used when something is bound to an instance. */
44     INSTANCE
45   }
46 
47   private final Key<?> key;
48   private final NodeType nodeType;
49 
NodeId(Key<?> key, NodeType nodeType)50   private NodeId(Key<?> key, NodeType nodeType) {
51     this.key = key;
52     this.nodeType = nodeType;
53   }
54 
newTypeId(Key<?> key)55   public static NodeId newTypeId(Key<?> key) {
56     return new NodeId(key, NodeType.TYPE);
57   }
58 
newInstanceId(Key<?> key)59   public static NodeId newInstanceId(Key<?> key) {
60     return new NodeId(key, NodeType.INSTANCE);
61   }
62 
getKey()63   public Key<?> getKey() {
64     return key;
65   }
66 
hashCode()67   @Override public int hashCode() {
68     return Objects.hashCode(key, nodeType);
69   }
70 
equals(Object obj)71   @Override public boolean equals(Object obj) {
72     if (!(obj.getClass().equals(NodeId.class))) {
73       return false;
74     }
75     NodeId other = (NodeId) obj;
76     return Objects.equal(key, other.key) && Objects.equal(nodeType, other.nodeType);
77   }
78 
toString()79   @Override public String toString() {
80     return "NodeId{nodeType=" + nodeType + " key=" + key + "}";
81   }
82 }
83