home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (Dependency.java)

This example Java source code file (Dependency.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

dependency, injectionpoint, key, list, override, set, stringbuilder, util

The Dependency.java Java example source code

 * Copyright (C) 2008 Google Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.

package com.google.inject.spi;

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

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;

 * A variable that can be resolved by an injector.
 * <p>Use {@link #get} to build a freestanding dependency, or {@link InjectionPoint} to build one
 * that's attached to a constructor, method or field.
 * @author crazybob@google.com (Bob Lee)
 * @author jessewilson@google.com (Jesse Wilson)
 * @since 2.0
public final class Dependency<T> {
  private final InjectionPoint injectionPoint;
  private final Key<T> key;
  private final boolean nullable;
  private final int parameterIndex;

  Dependency(InjectionPoint injectionPoint, Key<T> 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 <T> Dependency get(Key key) {
    return new Dependency<T>(null, MoreTypes.canonicalizeKey(key), true, -1);

   * Returns the dependencies from the given injection points.
  public static Set<Dependency forInjectionPoints(Set injectionPoints) {
    List<Dependency dependencies = Lists.newArrayList();
    for (InjectionPoint injectionPoint : injectionPoints) {
    return ImmutableSet.copyOf(dependencies);

   * Returns the key to the binding that satisfies this dependency.
  public Key<T> 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();
    if (injectionPoint != null) {
      if (parameterIndex != -1) {
    return builder.toString();

Other Java examples (source code examples)

Here is a short list of links related to this Java Dependency.java source code file:

my book on functional programming


new blog posts


Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.