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 private static volatile InternalExternalRecordConverter sInternalExternalRecordConverter; 39 40 private final Map<Integer, Class<? extends RecordInternal<?>>> 41 mRecordIdToInternalRecordClassMap; 42 private final Map<Integer, Class<? extends Record>> mRecordIdToExternalRecordClassMap; 43 InternalExternalRecordConverter()44 private InternalExternalRecordConverter() { 45 // Add any new data type here to facilitate its conversion. 46 mRecordIdToInternalRecordClassMap = 47 RecordMapper.getInstance().getRecordIdToInternalRecordClassMap(); 48 mRecordIdToExternalRecordClassMap = 49 RecordMapper.getInstance().getRecordIdToExternalRecordClassMap(); 50 } 51 52 @NonNull getInstance()53 public static synchronized InternalExternalRecordConverter getInstance() { 54 if (sInternalExternalRecordConverter == null) { 55 sInternalExternalRecordConverter = new InternalExternalRecordConverter(); 56 } 57 58 return sInternalExternalRecordConverter; 59 } 60 61 /** Returns a new instance of {@link RecordInternal} for the provided {@code type }. */ 62 @NonNull newInternalRecord(@ecordTypeIdentifier.RecordType int type)63 public RecordInternal<?> newInternalRecord(@RecordTypeIdentifier.RecordType int type) { 64 Class<? extends RecordInternal<?>> recordClass = 65 mRecordIdToInternalRecordClassMap.get(type); 66 Objects.requireNonNull(recordClass); 67 RecordInternal<?> recordInternal; 68 try { 69 recordInternal = recordClass.getConstructor().newInstance(); 70 } catch (InstantiationException 71 | IllegalAccessException 72 | InvocationTargetException 73 | NoSuchMethodException e) { 74 throw new RuntimeException(e); 75 } 76 77 return recordInternal; 78 } 79 80 /** Returns a record for {@param record} */ 81 @NonNull getExternalRecords(@onNull List<RecordInternal<?>> recordInternals)82 public List<Record> getExternalRecords(@NonNull List<RecordInternal<?>> recordInternals) { 83 List<Record> externalRecordList = new ArrayList<>(recordInternals.size()); 84 85 for (RecordInternal<?> recordInternal : recordInternals) { 86 try { 87 externalRecordList.add(recordInternal.toExternalRecord()); 88 } catch (IllegalArgumentException illegalArgumentException) { 89 if (!illegalArgumentException 90 .getMessage() 91 .contains(INTDEF_VALIDATION_ERROR_PREFIX)) { 92 throw illegalArgumentException; 93 } 94 } 95 } 96 97 return externalRecordList; 98 } 99 } 100