Skip to content

Commit

Permalink
Convert ProviderMapEntry to an InternalFactory
Browse files Browse the repository at this point in the history
This has been tested on the TAP train, no failures are believed to be caused by this CL
[]

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=136633899
  • Loading branch information
paulcreates authored and sameb committed Dec 12, 2016
1 parent 7329be5 commit 9a532da
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 44 deletions.
67 changes: 25 additions & 42 deletions core/src/com/google/inject/internal/RealMapBinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@
import com.google.inject.spi.BindingTargetVisitor;
import com.google.inject.spi.Dependency;
import com.google.inject.spi.Element;
import com.google.inject.spi.HasDependencies;
import com.google.inject.spi.ProviderInstanceBinding;
import com.google.inject.spi.ProviderLookup;
import com.google.inject.spi.ProviderWithDependencies;
import com.google.inject.spi.ProviderWithExtensionVisitor;
import com.google.inject.util.Types;
import java.lang.annotation.Annotation;
Expand Down Expand Up @@ -283,12 +280,7 @@ Key<V> getKeyForNewValue(K key) {
bindingSelection.getValueType(),
new RealElement(
entrySetBinder.getSetName(), MAPBINDER, bindingSelection.getKeyType().toString()));
// TODO(user): Convert ProviderMapEntry to an InternalFactory
// when this happens, we can remove the ProviderLookup check in
// BindingSelection.containsElement()
entrySetBinder
.addBinding()
.toProvider(new ProviderMapEntry<K, V>(key, binder.getProvider(valueKey), valueKey));
entrySetBinder.addBinding().toProvider(new ProviderMapEntry<K, V>(key, valueKey));
return valueKey;
}

Expand Down Expand Up @@ -551,8 +543,6 @@ private boolean containsElement(Element element) {
Key<?> key;
if (element instanceof Binding) {
key = ((Binding<?>) element).getKey();
} else if (element instanceof ProviderLookup) {
key = ((ProviderLookup<?>) element).getKey();
} else {
return false; // cannot match;
}
Expand Down Expand Up @@ -1178,70 +1168,63 @@ protected Map<K, Set<V>> doProvision(
}
}

/**
* A Provider that Map.Entry that is also a Provider. The key is the entry in the map this
* corresponds to and the value is the provider of the user's binding. This returns itself as the
* Provider.get value.
*/
/** A factory for a {@code Map.Entry<K, Provider<V>>}. */
//VisibleForTesting
static final class ProviderMapEntry<K, V>
implements ProviderWithDependencies<Map.Entry<K, Provider<V>>>, Map.Entry<K, Provider<V>> {
extends InternalProviderInstanceBindingImpl.Factory<Map.Entry<K, Provider<V>>> {
private final K key;
private final Provider<V> provider;
private final Key<V> valueKey;
private Map.Entry<K, Provider<V>> entry;

ProviderMapEntry(K key, Provider<V> provider, Key<V> valueKey) {
ProviderMapEntry(K key, Key<V> valueKey) {
super(InitializationTiming.EAGER);
this.key = key;
this.provider = provider;
this.valueKey = valueKey;
}

@Override
public Map.Entry<K, Provider<V>> get() {
return this;
}

@Override
public Set<Dependency<?>> getDependencies() {
return ((HasDependencies) provider).getDependencies();
// The dependencies are Key<Provider<V>>
return ImmutableSet.<Dependency<?>>of(Dependency.get(getKeyOfProvider(valueKey)));
}

public Key<V> getValueKey() {
return valueKey;
@Override
void initialize(InjectorImpl injector, Errors errors) {
Binding<V> valueBinding = injector.getExistingBinding(valueKey);
entry = Maps.immutableEntry(key, valueBinding.getProvider());
}

@Override
public K getKey() {
return key;
protected Map.Entry<K, Provider<V>> doProvision(
Errors errors, InternalContext context, Dependency<?> dependency) {
return entry;
}

@Override
public Provider<V> getValue() {
return provider;
K getKey() {
return key;
}

@Override
public Provider<V> setValue(Provider<V> value) {
throw new UnsupportedOperationException();
Key<V> getValueKey() {
return valueKey;
}

@Override
public boolean equals(Object obj) {
if (obj instanceof Map.Entry) {
@SuppressWarnings("rawtypes")
Map.Entry o = (Map.Entry) obj;
return Objects.equal(key, o.getKey()) && Objects.equal(provider, o.getValue());
if (obj instanceof ProviderMapEntry) {
ProviderMapEntry<?, ?> o = (ProviderMapEntry<?, ?>) obj;
return key.equals(o.key) && valueKey.equals(o.valueKey);
}
return false;
}

@Override
public int hashCode() {
return key.hashCode() ^ provider.hashCode();
return Objects.hashCode(key, valueKey);
}

@Override
public String toString() {
return "ProviderMapEntry(" + key + ", " + provider + ")";
return "ProviderMapEntry(" + key + ", " + valueKey + ")";
}
}

Expand Down
4 changes: 2 additions & 2 deletions core/test/com/google/inject/internal/SpiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,8 @@ private static <T> void mapModuleTest(
if (allowDuplicates) {
otherMatchesSize--; // allow for 1 duplicate binding
}
// Multiply by 3 because each has a value, ProviderLookup, and Map.Entry
int expectedSize = (mapResults.size() + duplicates) * 3;
// Multiply by 2 because each has a value, and Map.Entry
int expectedSize = (mapResults.size() + duplicates) * 2;
assertEquals(
"incorrect number of contains, leftover matches:\n" + Joiner.on("\n\t").join(otherMatches),
expectedSize,
Expand Down

0 comments on commit 9a532da

Please sign in to comment.