1 /* GENERATED SOURCE. DO NOT MODIFY. */ 2 /* 3 * Copyright (C) 2015 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package tests.util; 18 /** 19 * Runner which executes the provided code under test (via a callback) for each provided input 20 * value. 21 * @hide This class is not part of the Android public SDK API 22 */ 23 public final class ForEachRunner { 24 /** 25 * Callback parameterized with a value. 26 * @hide This class is not part of the Android public SDK API 27 */ 28 public interface Callback<T> { 29 /** 30 * Invokes the callback for the provided value. 31 */ run(T value)32 void run(T value) throws Exception; 33 } ForEachRunner()34 private ForEachRunner() {} 35 /** 36 * Invokes the provided callback for each of the provided named values. 37 * 38 * @param namesAndValues named values represented as name-value pairs. 39 * 40 * @param <T> type of value. 41 */ runNamed(Callback<T> callback, Iterable<Pair<String, T>> namesAndValues)42 public static <T> void runNamed(Callback<T> callback, Iterable<Pair<String, T>> namesAndValues) 43 throws Exception { 44 for (Pair<String, T> nameAndValue : namesAndValues) { 45 try { 46 callback.run(nameAndValue.getSecond()); 47 } catch (Throwable e) { 48 throw new Exception("Failed for " + nameAndValue.getFirst() + ": " + e.getMessage(), e); 49 } 50 } 51 } 52 } 53