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 package android.health.connect.internal.datatypes.utils; 17 18 import static com.android.healthfitness.flags.AconfigFlagHelper.isPersonalHealthRecordEnabled; 19 20 import android.annotation.NonNull; 21 import android.health.connect.datatypes.FhirResource; 22 import android.health.connect.datatypes.FhirResource.FhirResourceType; 23 24 import java.util.HashMap; 25 import java.util.Locale; 26 import java.util.Map; 27 28 /** @hide */ 29 public final class FhirResourceTypeStringToIntMapper { 30 private static final Map<String, Integer> sFhirResourceTypeStringToIntMap = new HashMap<>(); 31 32 private static final String FHIR_RESOURCE_TYPE_IMMUNIZATION_STR = "IMMUNIZATION"; 33 private static final String FHIR_RESOURCE_TYPE_ALLERGY_INTOLERANCE_STR = "ALLERGYINTOLERANCE"; 34 private static final String FHIR_RESOURCE_TYPE_OBSERVATION_STR = "OBSERVATION"; 35 private static final String FHIR_RESOURCE_TYPE_CONDITION_STR = "CONDITION"; 36 private static final String FHIR_RESOURCE_TYPE_PROCEDURE_STR = "PROCEDURE"; 37 private static final String FHIR_RESOURCE_TYPE_MEDICATION_STR = "MEDICATION"; 38 private static final String FHIR_RESOURCE_TYPE_MEDICATION_REQUEST_STR = "MEDICATIONREQUEST"; 39 private static final String FHIR_RESOURCE_TYPE_MEDICATION_STATEMENT_STR = "MEDICATIONSTATEMENT"; 40 private static final String FHIR_RESOURCE_TYPE_PATIENT_STR = "PATIENT"; 41 private static final String FHIR_RESOURCE_TYPE_PRACTITIONER_STR = "PRACTITIONER"; 42 private static final String FHIR_RESOURCE_TYPE_PRACTITIONER_ROLE_STR = "PRACTITIONERROLE"; 43 private static final String FHIR_RESOURCE_TYPE_ENCOUNTER_STR = "ENCOUNTER"; 44 private static final String FHIR_RESOURCE_TYPE_LOCATION_STR = "LOCATION"; 45 private static final String FHIR_RESOURCE_TYPE_ORGANIZATION_STR = "ORGANIZATION"; 46 47 /** 48 * Returns the corresponding {@code IntDef} {@link FhirResourceType} from a {@code String} 49 * {@code fhirResourceType}. 50 * 51 * @throws IllegalArgumentException if the type is not supported. 52 */ 53 @FhirResourceType getFhirResourceTypeInt(@onNull String fhirResourceType)54 public static int getFhirResourceTypeInt(@NonNull String fhirResourceType) { 55 if (!isPersonalHealthRecordEnabled()) { 56 throw new UnsupportedOperationException("getFhirResourceTypeInt is not supported"); 57 } 58 59 populateFhirResourceTypeStringToIntMap(); 60 61 Integer fhirResourceTypeInt = 62 sFhirResourceTypeStringToIntMap.get(fhirResourceType.toUpperCase(Locale.ROOT)); 63 if (fhirResourceTypeInt == null) { 64 throw new IllegalArgumentException( 65 "Unsupported FHIR resource type: " + fhirResourceType); 66 } 67 68 return fhirResourceTypeInt; 69 } 70 71 @SuppressWarnings("FlaggedApi") // Initial if statement checks flag, but lint can't know that populateFhirResourceTypeStringToIntMap()72 private static void populateFhirResourceTypeStringToIntMap() { 73 if (!isPersonalHealthRecordEnabled()) { 74 throw new UnsupportedOperationException( 75 "populateFhirResourceTypeStringToIntMap is not supported"); 76 } 77 78 if (!sFhirResourceTypeStringToIntMap.isEmpty()) { 79 return; 80 } 81 82 sFhirResourceTypeStringToIntMap.put( 83 FHIR_RESOURCE_TYPE_ALLERGY_INTOLERANCE_STR, 84 FhirResource.FHIR_RESOURCE_TYPE_ALLERGY_INTOLERANCE); 85 sFhirResourceTypeStringToIntMap.put( 86 FHIR_RESOURCE_TYPE_IMMUNIZATION_STR, FhirResource.FHIR_RESOURCE_TYPE_IMMUNIZATION); 87 sFhirResourceTypeStringToIntMap.put( 88 FHIR_RESOURCE_TYPE_OBSERVATION_STR, FhirResource.FHIR_RESOURCE_TYPE_OBSERVATION); 89 sFhirResourceTypeStringToIntMap.put( 90 FHIR_RESOURCE_TYPE_CONDITION_STR, FhirResource.FHIR_RESOURCE_TYPE_CONDITION); 91 sFhirResourceTypeStringToIntMap.put( 92 FHIR_RESOURCE_TYPE_PROCEDURE_STR, FhirResource.FHIR_RESOURCE_TYPE_PROCEDURE); 93 sFhirResourceTypeStringToIntMap.put( 94 FHIR_RESOURCE_TYPE_MEDICATION_STR, FhirResource.FHIR_RESOURCE_TYPE_MEDICATION); 95 sFhirResourceTypeStringToIntMap.put( 96 FHIR_RESOURCE_TYPE_MEDICATION_REQUEST_STR, 97 FhirResource.FHIR_RESOURCE_TYPE_MEDICATION_REQUEST); 98 sFhirResourceTypeStringToIntMap.put( 99 FHIR_RESOURCE_TYPE_MEDICATION_STATEMENT_STR, 100 FhirResource.FHIR_RESOURCE_TYPE_MEDICATION_STATEMENT); 101 sFhirResourceTypeStringToIntMap.put( 102 FHIR_RESOURCE_TYPE_PATIENT_STR, FhirResource.FHIR_RESOURCE_TYPE_PATIENT); 103 sFhirResourceTypeStringToIntMap.put( 104 FHIR_RESOURCE_TYPE_PRACTITIONER_STR, FhirResource.FHIR_RESOURCE_TYPE_PRACTITIONER); 105 sFhirResourceTypeStringToIntMap.put( 106 FHIR_RESOURCE_TYPE_PRACTITIONER_ROLE_STR, 107 FhirResource.FHIR_RESOURCE_TYPE_PRACTITIONER_ROLE); 108 sFhirResourceTypeStringToIntMap.put( 109 FHIR_RESOURCE_TYPE_ENCOUNTER_STR, FhirResource.FHIR_RESOURCE_TYPE_ENCOUNTER); 110 sFhirResourceTypeStringToIntMap.put( 111 FHIR_RESOURCE_TYPE_LOCATION_STR, FhirResource.FHIR_RESOURCE_TYPE_LOCATION); 112 sFhirResourceTypeStringToIntMap.put( 113 FHIR_RESOURCE_TYPE_ORGANIZATION_STR, FhirResource.FHIR_RESOURCE_TYPE_ORGANIZATION); 114 } 115 } 116