OptionalBinderBinding<T> optionalBinder = null;
MapBinderBinding<Map<Source, T>> mapbinderBinding = null;
Key<?> defaultKey = null;
Key<?> actualKey = null;
Binding optionalBinding = indexed.get(optionalKey);
optionalBinder = (OptionalBinderBinding<T>) optionalBinding.acceptTargetVisitor(visitor);
Binding mapBinding = indexed.get(sourceMapKey);
mapbinderBinding = (MapBinderBinding<Map<Source, T>>) mapBinding.acceptTargetVisitor(visitor);
// Locate the defaultKey & actualKey
for (Element element : elements) {
if (optionalBinder.containsElement(element) && element instanceof Binding) {
Binding binding = (Binding) element;
if (isSourceEntry(binding, Source.DEFAULT)) {
defaultKey = keyFromOptionalSourceBinding(binding, indexed);
} else if (isSourceEntry(binding, Source.ACTUAL)) {
actualKey = keyFromOptionalSourceBinding(binding, indexed);
}
}
}
assertNotNull(optionalBinder);
assertEquals(expectedDefault == null, defaultKey == null);
assertEquals(expectedActual == null, actualKey == null);
Key<Optional<javax.inject.Provider<T>>> optionalJavaxProviderKey =
keyType.ofType(optionalOfJavaxProvider(keyType.getTypeLiteral()));
Key<Optional<Provider<T>>> optionalProviderKey =
keyType.ofType(optionalOfProvider(keyType.getTypeLiteral()));
boolean keyMatch = false;
boolean optionalKeyMatch = false;
boolean optionalJavaxProviderKeyMatch = false;
boolean optionalProviderKeyMatch = false;
boolean mapBindingMatch = false;
boolean defaultMatch = false;
boolean actualMatch = false;
List<Object> otherOptionalElements = Lists.newArrayList();
List<Element> otherContains = Lists.newArrayList();
List<Element> nonContainedElements = Lists.newArrayList();
for (Element element : elements) {
boolean contains = optionalBinder.containsElement(element);
if (!contains) {
nonContainedElements.add(element);
}
Key key = null;
Binding b = null;
if (element instanceof Binding) {
b = (Binding) element;
key = b.getKey();
Object visited = b.acceptTargetVisitor(visitor);
if (visited instanceof OptionalBinderBinding) {
if (visited.equals(optionalBinder)) {
assertTrue(contains);
} else {
otherOptionalElements.add(visited);
}
}
} else if (element instanceof ProviderLookup) {
key = ((ProviderLookup) element).getKey();
}
if (key != null && key.equals(keyType)) {
// keyType might match because a user bound it
// (which is possible in a purely absent OptionalBinder)
assertEquals(expectedDefault != null || expectedActual != null, contains);
if (contains) {
keyMatch = true;
}
} else if (key != null && key.equals(optionalKey)) {
assertTrue(contains);
optionalKeyMatch = true;
} else if (key != null && key.equals(optionalJavaxProviderKey)) {
assertTrue(contains);
optionalJavaxProviderKeyMatch = true;
} else if (key != null && key.equals(optionalProviderKey)) {
assertTrue(contains);
optionalProviderKeyMatch = true;
} else if (key != null && key.equals(sourceMapKey)) {
assertTrue(contains);
mapBindingMatch = true;
// Validate that this binding is also a MapBinding.
assertEquals(mapbinderBinding, b.acceptTargetVisitor(visitor));
} else if (key != null && key.equals(defaultKey)) {
assertTrue(contains);
if (b != null) { // otherwise it might just be a ProviderLookup into it
assertTrue("expected: " + expectedDefault + ", but was: " + b,
matches(b, expectedDefault));