All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.inject.spi.Dependency Maven / Gradle / Ivy

package com.google.inject.spi;

import com.google.common.base.Objects;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.inject.Key;
import com.google.inject.internal.MoreTypes;

import java.util.List;
import java.util.Set;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * A variable that can be resolved by an injector.
 *
 * 

Use {@link #get} to build a freestanding dependency, or {@link InjectionPoint} to build one * that's attached to a constructor, method or field. */ public final class Dependency { private final InjectionPoint injectionPoint; private final Key key; private final boolean nullable; private final int parameterIndex; Dependency(InjectionPoint injectionPoint, Key key, boolean nullable, int parameterIndex) { this.injectionPoint = injectionPoint; this.key = checkNotNull(key, "key"); this.nullable = nullable; this.parameterIndex = parameterIndex; } /** * Returns a new dependency that is not attached to an injection point. The returned dependency is * nullable. */ public static Dependency get(Key key) { return new Dependency<>(null, MoreTypes.canonicalizeKey(key), true, -1); } /** * Returns the dependencies from the given injection points. */ public static Set> forInjectionPoints(Set injectionPoints) { List> dependencies = Lists.newArrayList(); for (InjectionPoint injectionPoint : injectionPoints) { dependencies.addAll(injectionPoint.getDependencies()); } return ImmutableSet.copyOf(dependencies); } /** * Returns the key to the binding that satisfies this dependency. */ public Key getKey() { return this.key; } /** * Returns true if null is a legal value for this dependency. */ public boolean isNullable() { return nullable; } /** * Returns the injection point to which this dependency belongs, or null if this dependency isn't * attached to a particular injection point. */ public InjectionPoint getInjectionPoint() { return injectionPoint; } /** * Returns the index of this dependency in the injection point's parameter list, or {@code -1} if * this dependency does not belong to a parameter list. Only method and constuctor dependencies * are elements in a parameter list. */ public int getParameterIndex() { return parameterIndex; } @Override public int hashCode() { return Objects.hashCode(injectionPoint, parameterIndex, key); } @Override public boolean equals(Object o) { if (o instanceof Dependency) { Dependency dependency = (Dependency) o; return Objects.equal(injectionPoint, dependency.injectionPoint) && Objects.equal(parameterIndex, dependency.parameterIndex) && Objects.equal(key, dependency.key); } else { return false; } } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append(key); if (injectionPoint != null) { builder.append("@").append(injectionPoint); if (parameterIndex != -1) { builder.append("[").append(parameterIndex).append("]"); } } return builder.toString(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy