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 java.lang.reflect.Member;
21 
22 /**
23  * Node for instances. Used when a type is bound to an instance.
24  *
25  * @author bojand@google.com (Bojan Djordjevic)
26  * @since 4.0
27  */
28 public class InstanceNode extends Node {
29   private final Object instance;
30   private final Iterable<Member> members;
31 
InstanceNode(NodeId id, Object source, Object instance, Iterable<Member> members)32   public InstanceNode(NodeId id, Object source, Object instance, Iterable<Member> members) {
33     super(id, source);
34     this.instance = instance;
35     this.members = members;
36   }
37 
getInstance()38   public Object getInstance() {
39     return instance;
40   }
41 
getMembers()42   public Iterable<Member> getMembers() {
43     return members;
44   }
45 
46   @Override
equals(Object obj)47   public boolean equals(Object obj) {
48     if (!(obj instanceof InstanceNode)) {
49       return false;
50     }
51     InstanceNode other = (InstanceNode) obj;
52     return super.equals(other)
53         && Objects.equal(instance, other.instance)
54         && Objects.equal(members, other.members);
55   }
56 
57   @Override
hashCode()58   public int hashCode() {
59     return 31 * super.hashCode() + Objects.hashCode(instance, members);
60   }
61 
62   @Override
toString()63   public String toString() {
64     return "InstanceNode{id="
65         + getId()
66         + " source="
67         + getSource()
68         + " instance="
69         + instance
70         + " members="
71         + members
72         + "}";
73   }
74 
75   @Override
copy(NodeId id)76   public Node copy(NodeId id) {
77     return new InstanceNode(id, getSource(), getInstance(), getMembers());
78   }
79 }
80