1 /* 2 * Copyright (C) 2019 The Android Open Source Project 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.android.tradefed.device.metric; 18 19 import com.android.tradefed.config.OptionCopier; 20 import com.android.tradefed.testtype.IRemoteTest; 21 22 import java.util.ArrayList; 23 import java.util.List; 24 25 /** Helper to do some {@link IMetricCollector} operations needed in several places. */ 26 public class CollectorHelper { 27 28 /** 29 * Helper to clone {@link IMetricCollector}s in order for each {@link IRemoteTest} to get a 30 * different instance, and avoid internal state and multi-init issues. 31 * 32 * @param originalCollectors the list of original collectors to be cloned. 33 * @return The list of cloned {@link IMetricCollector}. 34 */ cloneCollectors( List<IMetricCollector> originalCollectors)35 public static List<IMetricCollector> cloneCollectors( 36 List<IMetricCollector> originalCollectors) { 37 List<IMetricCollector> cloneList = new ArrayList<>(); 38 if (originalCollectors == null) { 39 return cloneList; 40 } 41 for (IMetricCollector collector : originalCollectors) { 42 try { 43 // TF object should all have a constructore with no args, so this should be safe. 44 IMetricCollector clone = collector.getClass().newInstance(); 45 OptionCopier.copyOptionsNoThrow(collector, clone); 46 cloneList.add(clone); 47 } catch (InstantiationException | IllegalAccessException e) { 48 throw new RuntimeException(e); 49 } 50 } 51 return cloneList; 52 } 53 } 54