com.google.inject.util.Providers Maven / Gradle / Ivy
package com.google.inject.util;
import com.google.common.base.Objects;
import com.google.inject.Provider;
/**
* Static utility methods for creating and working with instances of
* {@link Provider}.
*/
public final class Providers {
private Providers() {
}
/**
* Returns a provider which always provides {@code instance}. This should not
* be necessary to use in your application, but is helpful for several types
* of unit tests.
*
* @param instance the instance that should always be provided. This is also
* permitted to be null, to enable aggressive testing, although in real
* life a Guice-supplied Provider will never return null.
*/
public static Provider of(final T instance) {
return new ConstantProvider(instance);
}
private static final class ConstantProvider implements Provider {
private final T instance;
private ConstantProvider(T instance) {
this.instance = instance;
}
public T get() {
return instance;
}
@Override
public String toString() {
return "of(" + instance + ")";
}
@Override
public boolean equals(Object obj) {
return (obj instanceof ConstantProvider)
&& Objects.equal(instance, ((ConstantProvider>) obj).instance);
}
@Override
public int hashCode() {
return Objects.hashCode(instance);
}
}
}