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