1 /* 2 * Copyright (C) 2015 Google Inc. 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 com.google.caliper.runner; 18 19 import com.google.caliper.platform.Platform; 20 import com.google.caliper.platform.dalvik.DalvikModule; 21 import com.google.caliper.platform.dalvik.DalvikPlatform; 22 import com.google.caliper.platform.jvm.JvmModule; 23 import com.google.caliper.platform.jvm.JvmPlatform; 24 import com.google.common.base.Optional; 25 26 import dagger.Module; 27 import dagger.Provides; 28 29 import javax.inject.Provider; 30 31 /** 32 * Provider of a {@link Platform} instance appropriate for the current platform. 33 */ 34 @Module(includes = {JvmModule.class, DalvikModule.class}) 35 public final class PlatformModule { 36 37 /** 38 * Chooses the {@link DalvikPlatform} if available, otherwise uses the default 39 * {@link JvmPlatform}. 40 */ 41 @Provides providePlatform( Optional<DalvikPlatform> optionalDalvikPlatform, Provider<JvmPlatform> jvmPlatformProvider)42 static Platform providePlatform( 43 Optional<DalvikPlatform> optionalDalvikPlatform, 44 Provider<JvmPlatform> jvmPlatformProvider) { 45 if (optionalDalvikPlatform.isPresent()) { 46 return optionalDalvikPlatform.get(); 47 } else { 48 return jvmPlatformProvider.get(); 49 } 50 } 51 } 52