1 /* 2 * Copyright (C) 2014 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 dagger.internal.codegen.binding.BindingType.PRODUCTION; 20 21 import dagger.model.RequestKind; 22 import dagger.producers.Producer; 23 import javax.inject.Provider; 24 25 /** 26 * A mapper for associating a {@link RequestKind} to a {@link FrameworkType}, dependent on the type 27 * of code to be generated (e.g., for {@link Provider} or {@link Producer}). 28 */ 29 public enum FrameworkTypeMapper { FOR_PROVIDER()30 FOR_PROVIDER() { 31 @Override 32 public FrameworkType getFrameworkType(RequestKind requestKind) { 33 switch (requestKind) { 34 case INSTANCE: 35 case PROVIDER: 36 case PROVIDER_OF_LAZY: 37 case LAZY: 38 return FrameworkType.PROVIDER; 39 case PRODUCED: 40 case PRODUCER: 41 throw new IllegalArgumentException(requestKind.toString()); 42 default: 43 throw new AssertionError(requestKind); 44 } 45 } 46 }, FOR_PRODUCER()47 FOR_PRODUCER() { 48 @Override 49 public FrameworkType getFrameworkType(RequestKind requestKind) { 50 switch (requestKind) { 51 case INSTANCE: 52 case PRODUCED: 53 case PRODUCER: 54 return FrameworkType.PRODUCER_NODE; 55 case PROVIDER: 56 case PROVIDER_OF_LAZY: 57 case LAZY: 58 return FrameworkType.PROVIDER; 59 default: 60 throw new AssertionError(requestKind); 61 } 62 } 63 }; 64 forBindingType(BindingType bindingType)65 public static FrameworkTypeMapper forBindingType(BindingType bindingType) { 66 return bindingType.equals(PRODUCTION) ? FOR_PRODUCER : FOR_PROVIDER; 67 } 68 getFrameworkType(RequestKind requestKind)69 public abstract FrameworkType getFrameworkType(RequestKind requestKind); 70 } 71