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.model; 18 19 import static com.google.common.base.Preconditions.checkState; 20 import static com.google.common.collect.Iterables.getLast; 21 import static java.util.stream.Collectors.joining; 22 23 import com.google.auto.value.AutoValue; 24 import com.google.auto.value.extension.memoized.Memoized; 25 import com.google.common.collect.ImmutableList; 26 import javax.lang.model.element.TypeElement; 27 28 /** A path containing a component and all of its ancestor components. */ 29 @AutoValue 30 public abstract class ComponentPath { 31 /** Returns a new {@link ComponentPath} from {@code components}. */ create(Iterable<TypeElement> components)32 public static ComponentPath create(Iterable<TypeElement> components) { 33 return new AutoValue_ComponentPath(ImmutableList.copyOf(components)); 34 } 35 36 /** 37 * Returns the component types, starting from the {@linkplain #rootComponent() root 38 * component} and ending with the {@linkplain #currentComponent() current component}. 39 */ components()40 public abstract ImmutableList<TypeElement> components(); 41 42 /** 43 * Returns the root {@link dagger.Component}- or {@link 44 * dagger.producers.ProductionComponent}-annotated type 45 */ rootComponent()46 public final TypeElement rootComponent() { 47 return components().get(0); 48 } 49 50 /** Returns the component at the end of the path. */ 51 @Memoized currentComponent()52 public TypeElement currentComponent() { 53 return getLast(components()); 54 } 55 56 /** 57 * Returns the parent of the {@linkplain #currentComponent()} current component}. 58 * 59 * @throws IllegalStateException if the current graph is the {@linkplain #atRoot() root component} 60 */ parentComponent()61 public final TypeElement parentComponent() { 62 checkState(!atRoot()); 63 return components().reverse().get(1); 64 } 65 66 /** 67 * Returns this path's parent path. 68 * 69 * @throws IllegalStateException if the current graph is the {@linkplain #atRoot() root component} 70 */ 71 // TODO(ronshapiro): consider memoizing this parent()72 public final ComponentPath parent() { 73 checkState(!atRoot()); 74 return create(components().subList(0, components().size() - 1)); 75 } 76 77 /** Returns the path from the root component to the {@code child} of the current component. */ childPath(TypeElement child)78 public final ComponentPath childPath(TypeElement child) { 79 return create(ImmutableList.<TypeElement>builder().addAll(components()).add(child).build()); 80 } 81 82 /** 83 * Returns {@code true} if the {@linkplain #currentComponent()} current component} is the 84 * {@linkplain #rootComponent()} root component}. 85 */ atRoot()86 public final boolean atRoot() { 87 return components().size() == 1; 88 } 89 90 @Override toString()91 public final String toString() { 92 return components().stream().map(TypeElement::getQualifiedName).collect(joining(" → ")); 93 } 94 95 @Memoized 96 @Override hashCode()97 public abstract int hashCode(); 98 99 @Override equals(Object obj)100 public abstract boolean equals(Object obj); 101 } 102