• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.base.Preconditions.checkArgument;
20 
21 import androidx.room.compiler.processing.XMethodElement;
22 import androidx.room.compiler.processing.XTypeElement;
23 import com.google.auto.value.AutoValue;
24 import com.google.auto.value.extension.memoized.Memoized;
25 import dagger.BindsOptionalOf;
26 import dagger.internal.codegen.javapoet.TypeNames;
27 import dagger.spi.model.Key;
28 import java.util.Optional;
29 import javax.inject.Inject;
30 
31 /** A {@link BindsOptionalOf} declaration. */
32 @AutoValue
33 abstract class OptionalBindingDeclaration extends BindingDeclaration {
34 
35   /**
36    * {@inheritDoc}
37    *
38    * <p>The key's type is the method's return type, even though the synthetic bindings will be for
39    * {@code Optional} of derived types.
40    */
41   @Override
key()42   public abstract Key key();
43 
44   @Memoized
45   @Override
hashCode()46   public abstract int hashCode();
47 
48   @Override
equals(Object obj)49   public abstract boolean equals(Object obj);
50 
51   static class Factory {
52     private final KeyFactory keyFactory;
53 
54     @Inject
Factory(KeyFactory keyFactory)55     Factory(KeyFactory keyFactory) {
56       this.keyFactory = keyFactory;
57     }
58 
forMethod(XMethodElement method, XTypeElement contributingModule)59     OptionalBindingDeclaration forMethod(XMethodElement method, XTypeElement contributingModule) {
60       checkArgument(method.hasAnnotation(TypeNames.BINDS_OPTIONAL_OF));
61       return new AutoValue_OptionalBindingDeclaration(
62           Optional.of(method),
63           Optional.of(contributingModule),
64           keyFactory.forBindsOptionalOfMethod(method, contributingModule));
65     }
66   }
67 }
68