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.reporter 18 19 import java.nio.file.Path 20 21 /** Key that can be used to identify an API component for use in the baseline. */ 22 sealed interface BaselineKey { 23 /** 24 * Get the element id for this key. 25 * 26 * @param pathTransformer if the key contains a path then it will be passed to this to transform 27 * it into a form suitable for its use. 28 */ elementIdnull29 fun elementId(pathTransformer: (String) -> String = { it }): String 30 31 companion object { 32 val UNKNOWN = forElementId("?") 33 34 /** 35 * Get a [BaselineKey] that for the supplied element id. 36 * 37 * An element id is something that can uniquely identify an API element over a long period 38 * of time, e.g. a class name, class name plus method signature. 39 */ forElementIdnull40 fun forElementId(elementId: String): BaselineKey { 41 return ElementIdBaselineKey(elementId) 42 } 43 44 /** Get a [BaselineKey] for the supplied path. */ forPathnull45 fun forPath(path: Path): BaselineKey { 46 return PathBaselineKey(path) 47 } 48 } 49 50 /** 51 * A [BaselineKey] for an element id (which is simply a string that identifies a specific API 52 * element). 53 */ 54 private data class ElementIdBaselineKey(val elementId: String) : BaselineKey { elementIdnull55 override fun elementId(pathTransformer: (String) -> String): String { 56 return elementId 57 } 58 } 59 60 /** A [BaselineKey] for a [Path]. */ 61 private data class PathBaselineKey(val path: Path) : BaselineKey { elementIdnull62 override fun elementId(pathTransformer: (String) -> String): String { 63 return pathTransformer(path.toString()) 64 } 65 } 66 } 67