1 /* 2 * Copyright (C) 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 17 package android.health.connect.internal.datatypes.utils; 18 19 import static android.health.connect.datatypes.validation.ValidationUtils.INTDEF_VALIDATION_ERROR_PREFIX; 20 21 import android.annotation.NonNull; 22 import android.health.connect.datatypes.Record; 23 import android.health.connect.datatypes.RecordTypeIdentifier; 24 import android.health.connect.internal.datatypes.RecordInternal; 25 26 import java.lang.reflect.InvocationTargetException; 27 import java.util.ArrayList; 28 import java.util.List; 29 import java.util.Map; 30 import java.util.Objects; 31 32 /** 33 * A helper class used to convert internal and external data types. 34 * 35 * @hide 36 */ 37 public final class InternalExternalRecordConverter { 38 @SuppressWarnings("NullAway.Init") // TODO(b/317029272): fix this suppression 39 private static volatile InternalExternalRecordConverter sInternalExternalRecordConverter; 40 41 private final Map<Integer, Class<? extends RecordInternal<?>>> 42 mRecordIdToInternalRecordClassMap; 43 private final Map<Integer, Class<? extends Record>> mRecordIdToExternalRecordClassMap; 44 InternalExternalRecordConverter()45 private InternalExternalRecordConverter() { 46 // Add any new data type here to facilitate its conversion. 47 mRecordIdToInternalRecordClassMap = 48 RecordMapper.getInstance().getRecordIdToInternalRecordClassMap(); 49 mRecordIdToExternalRecordClassMap = 50 RecordMapper.getInstance().getRecordIdToExternalRecordClassMap(); 51 } 52 53 @NonNull getInstance()54 public static synchronized InternalExternalRecordConverter getInstance() { 55 if (sInternalExternalRecordConverter == null) { 56 sInternalExternalRecordConverter = new InternalExternalRecordConverter(); 57 } 58 59 return sInternalExternalRecordConverter; 60 } 61 62 /** Returns a new instance of {@link RecordInternal} for the provided {@code type }. */ 63 @NonNull newInternalRecord(@ecordTypeIdentifier.RecordType int type)64 public RecordInternal<?> newInternalRecord(@RecordTypeIdentifier.RecordType int type) { 65 Class<? extends RecordInternal<?>> recordClass = 66 mRecordIdToInternalRecordClassMap.get(type); 67 Objects.requireNonNull(recordClass); 68 RecordInternal<?> recordInternal; 69 try { 70 recordInternal = recordClass.getConstructor().newInstance(); 71 } catch (InstantiationException 72 | IllegalAccessException 73 | InvocationTargetException 74 | NoSuchMethodException e) { 75 throw new RuntimeException(e); 76 } 77 78 return recordInternal; 79 } 80 81 /** Returns a record for {@param record} */ 82 @SuppressWarnings("NullAway") // TODO(b/317029272): fix this suppression 83 @NonNull getExternalRecords(@onNull List<RecordInternal<?>> recordInternals)84 public List<Record> getExternalRecords(@NonNull List<RecordInternal<?>> recordInternals) { 85 List<Record> externalRecordList = new ArrayList<>(recordInternals.size()); 86 87 for (RecordInternal<?> recordInternal : recordInternals) { 88 try { 89 externalRecordList.add(recordInternal.toExternalRecord()); 90 } catch (IllegalArgumentException illegalArgumentException) { 91 if (!illegalArgumentException 92 .getMessage() 93 .contains(INTDEF_VALIDATION_ERROR_PREFIX)) { 94 throw illegalArgumentException; 95 } 96 } 97 } 98 99 return externalRecordList; 100 } 101 } 102