1 /*
2  * Copyright (C) 2008 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  * Edge that connects an interface to the type or instance that is bound to implement it.
23  *
24  * @author phopkins@gmail.com (Pete Hopkins)
25  * @since 4.0 (since 2.0 as an interface)
26  */
27 public class BindingEdge extends Edge {
28   /** Classification for what kind of binding this edge represents. */
29   public enum Type {
30     /** Binding is to an instance or class of the binding's same type. */
31     NORMAL,
32     /** Binding is to an instance or class that provides the binding's type. */
33     PROVIDER,
34     /** Binding is to the interface for a constant of a different type. */
35     CONVERTED_CONSTANT
36   }
37 
38   private final Type type;
39 
BindingEdge(NodeId fromId, NodeId toId, Type type)40   public BindingEdge(NodeId fromId, NodeId toId, Type type) {
41     super(fromId, toId);
42     this.type = type;
43   }
44 
getType()45   public Type getType() {
46     return type;
47   }
48 
49   @Override
equals(Object obj)50   public boolean equals(Object obj) {
51     if (!(obj instanceof BindingEdge)) {
52       return false;
53     }
54     BindingEdge other = (BindingEdge) obj;
55     return super.equals(other) && Objects.equal(type, other.type);
56   }
57 
58   @Override
hashCode()59   public int hashCode() {
60     return 31 * super.hashCode() + Objects.hashCode(type);
61   }
62 
63   @Override
toString()64   public String toString() {
65     return "BindingEdge{fromId=" + getFromId() + " toId=" + getToId() + " type=" + type + "}";
66   }
67 
68   @Override
copy(NodeId fromId, NodeId toId)69   public Edge copy(NodeId fromId, NodeId toId) {
70     return new BindingEdge(fromId, toId, type);
71   }
72 }
73