blob: c51d87c212ad70ab9812319c5263cafa0d38cfbf [file] [log] [blame]
limpbizkita98bc7a2008-08-29 16:52:44 +00001/**
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
17package com.google.inject.spi;
18
sberlinb7a02b02011-07-08 00:34:16 +000019import static com.google.common.base.Preconditions.checkNotNull;
20
21import com.google.common.base.Objects;
sberlind9c913a2011-06-26 21:02:54 +000022import com.google.common.collect.ImmutableSet;
23import com.google.common.collect.Lists;
sberlinb7a02b02011-07-08 00:34:16 +000024import com.google.inject.Key;
25
limpbizkit76c24b12008-12-25 04:32:41 +000026import java.util.List;
27import java.util.Set;
limpbizkita98bc7a2008-08-29 16:52:44 +000028
29/**
30 * A variable that can be resolved by an injector.
31 *
32 * <p>Use {@link #get} to build a freestanding dependency, or {@link InjectionPoint} to build one
33 * that's attached to a constructor, method or field.
34 *
35 * @author crazybob@google.com (Bob Lee)
36 * @author jessewilson@google.com (Jesse Wilson)
limpbizkitc489adf2008-11-18 07:01:33 +000037 * @since 2.0
limpbizkita98bc7a2008-08-29 16:52:44 +000038 */
limpbizkit6caa8dc2009-05-19 00:40:02 +000039public final class Dependency<T> {
limpbizkita98bc7a2008-08-29 16:52:44 +000040 private final InjectionPoint injectionPoint;
41 private final Key<T> key;
42 private final boolean nullable;
43 private final int parameterIndex;
44
limpbizkit0afda9a2009-06-23 00:29:43 +000045 Dependency(InjectionPoint injectionPoint, Key<T> key, boolean nullable, int parameterIndex) {
limpbizkita98bc7a2008-08-29 16:52:44 +000046 this.injectionPoint = injectionPoint;
limpbizkit0afda9a2009-06-23 00:29:43 +000047 this.key = checkNotNull(key, "key");
limpbizkita98bc7a2008-08-29 16:52:44 +000048 this.nullable = nullable;
49 this.parameterIndex = parameterIndex;
50 }
51
52 /**
53 * Returns a new dependency that is not attached to an injection point. The returned dependency is
54 * nullable.
55 */
56 public static <T> Dependency<T> get(Key<T> key) {
57 return new Dependency<T>(null, key, true, -1);
58 }
59
60 /**
limpbizkit76c24b12008-12-25 04:32:41 +000061 * Returns the dependencies from the given injection points.
62 */
63 public static Set<Dependency<?>> forInjectionPoints(Set<InjectionPoint> injectionPoints) {
64 List<Dependency<?>> dependencies = Lists.newArrayList();
65 for (InjectionPoint injectionPoint : injectionPoints) {
66 dependencies.addAll(injectionPoint.getDependencies());
67 }
68 return ImmutableSet.copyOf(dependencies);
69 }
70
71 /**
limpbizkita98bc7a2008-08-29 16:52:44 +000072 * Returns the key to the binding that satisfies this dependency.
73 */
74 public Key<T> getKey() {
75 return this.key;
76 }
77
78 /**
79 * Returns true if null is a legal value for this dependency.
80 */
81 public boolean isNullable() {
82 return nullable;
83 }
84
85 /**
86 * Returns the injection point to which this dependency belongs, or null if this dependency isn't
87 * attached to a particular injection point.
88 */
89 public InjectionPoint getInjectionPoint() {
90 return injectionPoint;
91 }
92
93 /**
94 * Returns the index of this dependency in the injection point's parameter list, or {@code -1} if
95 * this dependency does not belong to a parameter list. Only method and constuctor dependencies
96 * are elements in a parameter list.
97 */
98 public int getParameterIndex() {
99 return parameterIndex;
100 }
101
102 @Override public int hashCode() {
103 return Objects.hashCode(injectionPoint, parameterIndex, key);
104 }
105
106 @Override public boolean equals(Object o) {
107 if (o instanceof Dependency) {
108 Dependency dependency = (Dependency) o;
109 return Objects.equal(injectionPoint, dependency.injectionPoint)
110 && Objects.equal(parameterIndex, dependency.parameterIndex)
111 && Objects.equal(key, dependency.key);
112 } else {
113 return false;
114 }
115 }
116
117 @Override public String toString() {
118 StringBuilder builder = new StringBuilder();
119 builder.append(key);
120 if (injectionPoint != null) {
121 builder.append("@").append(injectionPoint);
122 if (parameterIndex != -1) {
123 builder.append("[").append(parameterIndex).append("]");
124 }
125 }
126 return builder.toString();
127 }
limpbizkita98bc7a2008-08-29 16:52:44 +0000128}