Use Map.computeIfAbsent instead of c.g.c.cache.Cache. Drop the dependency on [][]
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=158017864
diff --git a/java/dagger/internal/codegen/BindingGraph.java b/java/dagger/internal/codegen/BindingGraph.java
index 8b695fd..5cdc040 100644
--- a/java/dagger/internal/codegen/BindingGraph.java
+++ b/java/dagger/internal/codegen/BindingGraph.java
@@ -36,8 +36,6 @@
import com.google.auto.value.AutoValue;
import com.google.auto.value.extension.memoized.Memoized;
import com.google.common.base.VerifyException;
-import com.google.common.cache.Cache;
-import com.google.common.cache.CacheBuilder;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
@@ -59,6 +57,7 @@
import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Deque;
+import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
@@ -66,7 +65,6 @@
import java.util.Optional;
import java.util.Queue;
import java.util.Set;
-import java.util.concurrent.ExecutionException;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import javax.inject.Inject;
@@ -369,10 +367,8 @@
final ImmutableSetMultimap<Key, DelegateDeclaration> delegateMultibindingDeclarations;
final Map<BindingKey, ResolvedBindings> resolvedBindings;
final Deque<BindingKey> cycleStack = new ArrayDeque<>();
- final Cache<BindingKey, Boolean> bindingKeyDependsOnLocalBindingsCache =
- CacheBuilder.newBuilder().build();
- final Cache<Binding, Boolean> bindingDependsOnLocalBindingsCache =
- CacheBuilder.newBuilder().build();
+ final Map<BindingKey, Boolean> bindingKeyDependsOnLocalBindingsCache = new HashMap<>();
+ final Map<Binding, Boolean> bindingDependsOnLocalBindingsCache = new HashMap<>();
final Queue<ComponentDescriptor> subcomponentsToResolve = new ArrayDeque<>();
Resolver(
@@ -1027,37 +1023,34 @@
* empty
*/
boolean dependsOnLocalBindings(BindingKey bindingKey) {
- checkArgument(
- getPreviouslyResolvedBindings(bindingKey).isPresent(),
- "no previously resolved bindings in %s for %s",
- Resolver.this,
- bindingKey);
// Don't recur infinitely if there are valid cycles in the dependency graph.
// http://b/23032377
if (!cycleChecker.add(bindingKey)) {
return false;
}
- try {
- return bindingKeyDependsOnLocalBindingsCache.get(
- bindingKey,
- () -> {
- ResolvedBindings previouslyResolvedBindings =
- getPreviouslyResolvedBindings(bindingKey).get();
- if (hasLocalMultibindingContributions(previouslyResolvedBindings)
- || hasLocallyPresentOptionalBinding(previouslyResolvedBindings)) {
- return true;
- }
+ return bindingKeyDependsOnLocalBindingsCache.computeIfAbsent(
+ bindingKey, this::dependsOnLocalBindingsUncached);
+ }
- for (Binding binding : previouslyResolvedBindings.bindings()) {
- if (dependsOnLocalBindings(binding)) {
- return true;
- }
- }
- return false;
- });
- } catch (ExecutionException e) {
- throw new AssertionError(e);
+ private boolean dependsOnLocalBindingsUncached(BindingKey bindingKey) {
+ checkArgument(
+ getPreviouslyResolvedBindings(bindingKey).isPresent(),
+ "no previously resolved bindings in %s for %s",
+ Resolver.this,
+ bindingKey);
+ ResolvedBindings previouslyResolvedBindings =
+ getPreviouslyResolvedBindings(bindingKey).get();
+ if (hasLocalMultibindingContributions(previouslyResolvedBindings)
+ || hasLocallyPresentOptionalBinding(previouslyResolvedBindings)) {
+ return true;
}
+
+ for (Binding binding : previouslyResolvedBindings.bindings()) {
+ if (dependsOnLocalBindings(binding)) {
+ return true;
+ }
+ }
+ return false;
}
/**
@@ -1073,25 +1066,22 @@
if (!cycleChecker.add(binding)) {
return false;
}
- try {
- return bindingDependsOnLocalBindingsCache.get(
- binding,
- () -> {
- if ((!binding.scope().isPresent()
- || binding.scope().get().equals(reusableScope(elements)))
- // TODO(beder): Figure out what happens with production subcomponents.
- && !binding.bindingType().equals(BindingType.PRODUCTION)) {
- for (DependencyRequest dependency : binding.dependencies()) {
- if (dependsOnLocalBindings(dependency.bindingKey())) {
- return true;
- }
- }
- }
- return false;
- });
- } catch (ExecutionException e) {
- throw new AssertionError(e);
+ return bindingDependsOnLocalBindingsCache.computeIfAbsent(
+ binding, this::dependsOnLocalBindingsUncached);
+ }
+
+ private boolean dependsOnLocalBindingsUncached(Binding binding) {
+ if ((!binding.scope().isPresent()
+ || binding.scope().get().equals(reusableScope(elements)))
+ // TODO(beder): Figure out what happens with production subcomponents.
+ && !binding.bindingType().equals(BindingType.PRODUCTION)) {
+ for (DependencyRequest dependency : binding.dependencies()) {
+ if (dependsOnLocalBindings(dependency.bindingKey())) {
+ return true;
+ }
+ }
}
+ return false;
}
/**