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.apilevels 18 19 import com.android.tools.metalava.SdkExtension 20 import com.android.tools.metalava.apilevels.ApiToExtensionsMap.Companion.ANDROID_PLATFORM_SDK_ID 21 22 /** The set of available SDK extensions. */ 23 class AvailableSdkExtensions(internal val sdkExtensions: Set<SdkExtension>) { 24 25 init { 26 // verify: the predefined Android platform SDK ID is not reused as an extension SDK ID <lambda>null27 if (sdkExtensions.any { it.id == ANDROID_PLATFORM_SDK_ID }) { 28 throw IllegalArgumentException( 29 "bad SDK definition: the ID $ANDROID_PLATFORM_SDK_ID is reserved for the Android platform SDK" 30 ) 31 } 32 33 // verify: no duplicate SDK IDs <lambda>null34 if (sdkExtensions.size != sdkExtensions.distinctBy { it.id }.size) { 35 throw IllegalArgumentException("bad SDK definitions: duplicate SDK IDs") 36 } 37 38 // verify: no duplicate SDK names <lambda>null39 if (sdkExtensions.size != sdkExtensions.distinctBy { it.shortname }.size) { 40 throw IllegalArgumentException("bad SDK definitions: duplicate SDK short names") 41 } 42 43 // verify: no duplicate SDK names <lambda>null44 if (sdkExtensions.size != sdkExtensions.distinctBy { it.name }.size) { 45 throw IllegalArgumentException("bad SDK definitions: duplicate SDK names") 46 } 47 48 // verify: no duplicate SDK references <lambda>null49 if (sdkExtensions.size != sdkExtensions.distinctBy { it.reference }.size) { 50 throw IllegalArgumentException("bad SDK definitions: duplicate SDK references") 51 } 52 } 53 <lambda>null54 private val shortToSdkExtension = sdkExtensions.associateBy { it.shortname } 55 56 /** Check to see if this contains an SDK extension with [shortExtensionName]. */ containsSdkExtensionnull57 fun containsSdkExtension(shortExtensionName: String) = shortExtensionName in shortToSdkExtension 58 59 /** 60 * Retrieve the SDK extension appropriate for the [shortExtensionName], throwing an exception if 61 * it could not be found. 62 */ 63 fun retrieveSdkExtension(shortExtensionName: String): SdkExtension { 64 return shortToSdkExtension[shortExtensionName] 65 ?: throw IllegalStateException("unknown extension SDK \"$shortExtensionName\"") 66 } 67 } 68