1 /* 2 * Copyright (C) 2018 The Dagger Authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package dagger.internal.codegen.binding; 18 19 import static com.google.common.collect.Iterables.getOnlyElement; 20 import static dagger.internal.codegen.extension.DaggerStreams.presentValues; 21 import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet; 22 import static java.util.stream.Collectors.joining; 23 24 import com.google.common.collect.ImmutableSet; 25 import com.squareup.javapoet.ClassName; 26 import dagger.spi.model.BindingGraph.SubcomponentCreatorBindingEdge; 27 import dagger.spi.model.DaggerTypeElement; 28 29 /** An implementation of {@link SubcomponentCreatorBindingEdge}. */ 30 public final class SubcomponentCreatorBindingEdgeImpl implements SubcomponentCreatorBindingEdge { 31 private final ImmutableSet<SubcomponentDeclaration> subcomponentDeclarations; 32 SubcomponentCreatorBindingEdgeImpl( ImmutableSet<SubcomponentDeclaration> subcomponentDeclarations)33 SubcomponentCreatorBindingEdgeImpl( 34 ImmutableSet<SubcomponentDeclaration> subcomponentDeclarations) { 35 this.subcomponentDeclarations = subcomponentDeclarations; 36 } 37 38 @Override declaringModules()39 public ImmutableSet<DaggerTypeElement> declaringModules() { 40 return subcomponentDeclarations.stream() 41 .map(SubcomponentDeclaration::contributingModule) 42 .flatMap(presentValues()) 43 .map(DaggerTypeElement::from) 44 .collect(toImmutableSet()); 45 } 46 47 @Override toString()48 public String toString() { 49 return "subcomponent declared by " 50 + (subcomponentDeclarations.size() == 1 51 ? getOnlyElement(declaringModules()).className().canonicalName() 52 : declaringModules().stream() 53 .map(DaggerTypeElement::className) 54 .map(ClassName::canonicalName) 55 .collect(joining(", ", "{", "}"))); 56 } 57 } 58