1 /* 2 * Copyright (C) 2024 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.tools.metalava.model.testing.transformer 18 19 import com.android.tools.metalava.model.Codebase 20 import java.util.ServiceLoader 21 22 interface CodebaseTransformer { 23 24 /** Transform the [codebase] into a different [Codebase] */ transformnull25 fun transform(codebase: Codebase): Codebase 26 27 companion object { 28 29 /** 30 * Find all the [CodebaseTransformer]s and apply as follows: 31 * 1. If none are available return [codebase]. 32 * 2. If more than one are available fail. 33 * 3. Load the available one and apply to [codebase] returning the result. 34 */ 35 fun transformIfAvailable(codebase: Codebase): Codebase { 36 // Try and load CodebaseTransformers. 37 val loader = ServiceLoader.load(CodebaseTransformer::class.java) 38 39 // If there are none then return the codebase unchanged. 40 if (loader.none()) return codebase 41 42 // Get the sole transformer. 43 val transformer = loader.toList().single() 44 45 // Apply it. 46 return transformer.transform(codebase) 47 } 48 } 49 } 50