1 /*
2  * Copyright 2023 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 package androidx.appsearch.compiler.annotationwrapper
17 
18 import androidx.appsearch.compiler.IntrospectionHelper
19 import com.squareup.javapoet.ClassName
20 import javax.lang.model.type.TypeMirror
21 
22 /** An instance of the `@Document.BooleanProperty` annotation. */
23 data class BooleanPropertyAnnotation(
24     override val name: String,
25     override val isRequired: Boolean,
26 ) :
27     DataPropertyAnnotation(
28         className = CLASS_NAME,
29         configClassName = CONFIG_CLASS,
30         genericDocGetterName = "getPropertyBoolean",
31         genericDocArrayGetterName = "getPropertyBooleanArray",
32         genericDocSetterName = "setPropertyBoolean"
33     ) {
34     companion object {
35         val CLASS_NAME: ClassName =
36             IntrospectionHelper.DOCUMENT_ANNOTATION_CLASS.nestedClass("BooleanProperty")
37 
38         val CONFIG_CLASS: ClassName =
39             IntrospectionHelper.APPSEARCH_SCHEMA_CLASS.nestedClass("BooleanPropertyConfig")
40 
41         /**
42          * @param defaultName The name to use for the annotated property in case the annotation
43          *   params do not mention an explicit name.
44          */
parsenull45         fun parse(
46             annotationParams: Map<String, Any?>,
47             defaultName: String
48         ): BooleanPropertyAnnotation {
49             val name = annotationParams["name"] as? String
50             return BooleanPropertyAnnotation(
51                 name = if (name.isNullOrEmpty()) defaultName else name,
52                 isRequired = annotationParams["required"] as Boolean,
53             )
54         }
55     }
56 
57     override val dataPropertyKind
58         get() = Kind.BOOLEAN_PROPERTY
59 
getUnderlyingTypeWithinGenericDocnull60     override fun getUnderlyingTypeWithinGenericDoc(helper: IntrospectionHelper): TypeMirror =
61         helper.booleanPrimitiveType
62 }
63