blob: 0e430112c2ea3311812d4bd72e9a514d8a8ba2ad [file] [log] [blame]
ronshapiro7bd173e2017-06-14 11:00:36 -07001/*
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
17package dagger.internal.codegen;
18
19import com.google.common.util.concurrent.Futures;
20import com.squareup.javapoet.ClassName;
21import com.squareup.javapoet.CodeBlock;
22
23/**
24 * A {@link RequestFulfillment} that can fulfill its request with a simple call when possible, and
25 * otherwise delegates to a backing provider field.
26 */
27abstract class SimpleInvocationRequestFulfillment extends RequestFulfillment {
28 private final RequestFulfillment delegate;
29
30 SimpleInvocationRequestFulfillment(BindingKey bindingKey, RequestFulfillment delegate) {
31 super(bindingKey);
32 this.delegate = delegate;
33 }
34
35 abstract CodeBlock getSimpleInvocation(DependencyRequest request, ClassName requestingClass);
36
37 @Override
38 final CodeBlock getSnippetForDependencyRequest(
39 DependencyRequest request, ClassName requestingClass) {
40 switch (request.kind()) {
41 case INSTANCE:
42 return getSimpleInvocation(request, requestingClass);
43 case FUTURE:
44 return CodeBlock.of(
45 "$T.immediateFuture($L)", Futures.class, getSimpleInvocation(request, requestingClass));
46 default:
47 return delegate.getSnippetForDependencyRequest(request, requestingClass);
48 }
49 }
50
51 @Override
52 final CodeBlock getSnippetForFrameworkDependency(
53 FrameworkDependency frameworkDependency, ClassName requestingClass) {
54 return delegate.getSnippetForFrameworkDependency(frameworkDependency, requestingClass);
55 }
56}