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
17package com.google.inject.grapher;
18
19import com.google.common.base.Objects;
20import 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 */
28public class InstanceNode extends Node {
29  private final Object instance;
30  private final Iterable<Member> members;
31
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
38  public Object getInstance() {
39    return instance;
40  }
41
42  public Iterable<Member> getMembers() {
43    return members;
44  }
45
46  @Override public boolean equals(Object obj) {
47    if (!(obj instanceof InstanceNode)) {
48      return false;
49    }
50    InstanceNode other = (InstanceNode) obj;
51    return super.equals(other) && Objects.equal(instance, other.instance)
52        && Objects.equal(members, other.members);
53  }
54
55  @Override public int hashCode() {
56    return 31 * super.hashCode() + Objects.hashCode(instance, members);
57  }
58
59  @Override public String toString() {
60    return "InstanceNode{id=" + getId() + " source=" + getSource() + " instance=" + instance
61        + " members=" + members + "}";
62  }
63
64  @Override public Node copy(NodeId id) {
65    return new InstanceNode(id, getSource(), getInstance(), getMembers());
66  }
67}
68