Howard Hao | 76d6225 | 2021-09-16 14:35:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 | |
Howard Hao | 1a04575 | 2021-10-12 17:34:08 -0700 | [diff] [blame^] | 17 | package com.android.car.telemetry.publisher.statsconverters; |
Howard Hao | 76d6225 | 2021-09-16 14:35:01 -0700 | [diff] [blame] | 18 | |
| 19 | import com.google.protobuf.MessageLite; |
| 20 | |
| 21 | import java.util.function.Function; |
| 22 | |
| 23 | /** |
| 24 | * Class that contains metadata and actions for a field of atom data type T. |
| 25 | * |
| 26 | * @param <T> the atom data type. |
| 27 | */ |
| 28 | public class AtomFieldAccessor<T extends MessageLite> { |
| 29 | private final String mFieldName; |
| 30 | private final Function<T, Boolean> mHasField; |
| 31 | private final Function<T, Object> mGetField; |
| 32 | |
| 33 | AtomFieldAccessor( |
| 34 | String fieldName, |
| 35 | Function<T, Boolean> hasField, |
| 36 | Function<T, Object> getField) { |
| 37 | mFieldName = fieldName; |
| 38 | mHasField = hasField; |
| 39 | mGetField = getField; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Gets the field name. |
| 44 | * |
| 45 | * @return field name as string. |
| 46 | */ |
| 47 | String getFieldName() { |
| 48 | return mFieldName; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Checks if the field is set for the provided atom data of type T. |
| 53 | * |
| 54 | * @param atomData the atom data in which to check if the field is set. |
| 55 | * @return whether the field is set. |
| 56 | */ |
| 57 | Boolean hasField(T atomData) { |
| 58 | return mHasField.apply(atomData); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Gets the field value for atom data of type T. |
| 63 | * |
| 64 | * @param atomData the atom data for which to get the field value from. |
| 65 | * @return the field value Object. |
| 66 | */ |
| 67 | Object getField(T atomData) { |
| 68 | return mGetField.apply(atomData); |
| 69 | } |
| 70 | } |