• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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.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 com.squareup.javapoet.ClassName;
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<DaggerTypeElement> components)32   public static ComponentPath create(Iterable<DaggerTypeElement> 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<DaggerTypeElement> components();
41 
42   /**
43    * Returns the root {@link dagger.Component}- or {@link
44    * dagger.producers.ProductionComponent}-annotated type
45    */
rootComponent()46   public final DaggerTypeElement rootComponent() {
47     return components().get(0);
48   }
49 
50   /** Returns the component at the end of the path. */
51   @Memoized
currentComponent()52   public DaggerTypeElement 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 DaggerTypeElement 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(DaggerTypeElement child)78   public final ComponentPath childPath(DaggerTypeElement child) {
79     return create(
80         ImmutableList.<DaggerTypeElement>builder().addAll(components()).add(child).build());
81   }
82 
83   /**
84    * Returns {@code true} if the {@linkplain #currentComponent()} current component} is the
85    * {@linkplain #rootComponent()} root component}.
86    */
atRoot()87   public final boolean atRoot() {
88     return components().size() == 1;
89   }
90 
91   @Override
toString()92   public final String toString() {
93     return components().stream()
94         .map(DaggerTypeElement::className)
95         .map(ClassName::canonicalName)
96         .collect(joining(" → "));
97   }
98 
99   @Memoized
100   @Override
hashCode()101   public abstract int hashCode();
102 
103   @Override
equals(Object obj)104   public abstract boolean equals(Object obj);
105 }
106