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