xref: /aosp_15_r20/external/dagger2/java/dagger/model/ComponentPath.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
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 {@code Component}- or {@code ProductionComponent}-annotated type
44    */
rootComponent()45   public final TypeElement rootComponent() {
46     return components().get(0);
47   }
48 
49   /** Returns the component at the end of the path. */
50   @Memoized
currentComponent()51   public TypeElement currentComponent() {
52     return getLast(components());
53   }
54 
55   /**
56    * Returns the parent of the {@linkplain #currentComponent()} current component}.
57    *
58    * @throws IllegalStateException if the current graph is the {@linkplain #atRoot() root component}
59    */
parentComponent()60   public final TypeElement parentComponent() {
61     checkState(!atRoot());
62     return components().reverse().get(1);
63   }
64 
65   /**
66    * Returns this path's parent path.
67    *
68    * @throws IllegalStateException if the current graph is the {@linkplain #atRoot() root component}
69    */
70   // TODO(ronshapiro): consider memoizing this
parent()71   public final ComponentPath parent() {
72     checkState(!atRoot());
73     return create(components().subList(0, components().size() - 1));
74   }
75 
76   /** Returns the path from the root component to the {@code child} of the current component. */
childPath(TypeElement child)77   public final ComponentPath childPath(TypeElement child) {
78     return create(ImmutableList.<TypeElement>builder().addAll(components()).add(child).build());
79   }
80 
81   /**
82    * Returns {@code true} if the {@linkplain #currentComponent()} current component} is the
83    * {@linkplain #rootComponent()} root component}.
84    */
atRoot()85   public final boolean atRoot() {
86     return components().size() == 1;
87   }
88 
89   @Override
toString()90   public final String toString() {
91     return components().stream().map(TypeElement::getQualifiedName).collect(joining(" → "));
92   }
93 
94   @Memoized
95   @Override
hashCode()96   public abstract int hashCode();
97 
98   @Override
equals(Object obj)99   public abstract boolean equals(Object obj);
100 }
101