1 /* 2 * Copyright 2022 Google LLC 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 package com.google.android.libraries.mobiledatadownload.populator; 17 18 import com.google.android.libraries.mobiledatadownload.FileGroupPopulator; 19 import com.google.android.libraries.mobiledatadownload.MobileDataDownload; 20 import com.google.common.base.Supplier; 21 import com.google.common.util.concurrent.ListenableFuture; 22 23 /** 24 * A {@code MigrationProxyPopulator} can be used by a client to run migration experiments from one 25 * file group populator to another. 26 * 27 * <p>{@code flagSupplier} is used to determine whether the control or experiment populator is used. 28 * It is called every time a client calls refreshFileGroups. When it returns true, {@code 29 * experimentFileGroupPopulator} is delegated to refresh file group; when it returns false, {@code 30 * controlFileGroupPopulator} is delegated to refresh file group. This design enables the client to 31 * refresh on the correct populator based on most up-to-date flag value. 32 */ 33 public final class MigrationProxyPopulator implements FileGroupPopulator { 34 private final FileGroupPopulator controlFileGroupPopulator; 35 private final FileGroupPopulator experimentFileGroupPopulator; 36 private final Supplier<Boolean> flagSupplier; 37 MigrationProxyPopulator( FileGroupPopulator controlFileGroupPopulator, FileGroupPopulator experimentFileGroupPopulator, Supplier<Boolean> flagSupplier)38 public MigrationProxyPopulator( 39 FileGroupPopulator controlFileGroupPopulator, 40 FileGroupPopulator experimentFileGroupPopulator, 41 Supplier<Boolean> flagSupplier) { 42 this.controlFileGroupPopulator = controlFileGroupPopulator; 43 this.experimentFileGroupPopulator = experimentFileGroupPopulator; 44 this.flagSupplier = flagSupplier; 45 } 46 47 @Override refreshFileGroups(MobileDataDownload mobileDataDownload)48 public ListenableFuture<Void> refreshFileGroups(MobileDataDownload mobileDataDownload) { 49 if (flagSupplier.get()) { 50 return experimentFileGroupPopulator.refreshFileGroups(mobileDataDownload); 51 } else { 52 return controlFileGroupPopulator.refreshFileGroups(mobileDataDownload); 53 } 54 } 55 } 56