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 com.android.car.internal.property; 18 19 import static com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport.PRIVATE_CONSTRUCTOR; 20 21 import android.annotation.Nullable; 22 import android.car.VehiclePropertyIds; 23 24 import com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport; 25 import com.android.car.internal.util.ConstantDebugUtils; 26 27 /** 28 * Utility class to convert {@link VehiclePropertyIds} to and from their name and ID. 29 * 30 * @hide 31 */ 32 public final class VehiclePropertyIdDebugUtils { 33 /** 34 * VehiclePropertyIdDebugUtils only contains static fields and methods and must never be 35 * instantiated. 36 */ 37 @ExcludeFromCodeCoverageGeneratedReport(reason = PRIVATE_CONSTRUCTOR) VehiclePropertyIdDebugUtils()38 private VehiclePropertyIdDebugUtils() { 39 throw new UnsupportedOperationException("Must never be called"); 40 } 41 42 /** 43 * Gets the property's name based on the ID. 44 */ 45 @Nullable toName(int propertyId)46 public static String toName(int propertyId) { 47 return ConstantDebugUtils.toName(VehiclePropertyIds.class, propertyId); 48 } 49 50 /** 51 * Gets the property's ID based on the passed name 52 */ 53 @Nullable toId(String propertyName)54 public static Integer toId(String propertyName) { 55 return ConstantDebugUtils.toValue(VehiclePropertyIds.class, propertyName); 56 } 57 58 /** 59 * Gets a user-friendly representation string representation of {@code propertyId}. 60 */ toDebugString(int propertyId)61 public static String toDebugString(int propertyId) { 62 if (isDefined(propertyId)) { 63 return toName(propertyId); 64 } else if (CarPropertyHelper.isVendorProperty(propertyId)) { 65 return "VENDOR_PROPERTY(0x" + Integer.toHexString(propertyId) + ")"; 66 } else if (CarPropertyHelper.isBackportedProperty(propertyId)) { 67 return "BACKPORTED_PROPERTY(0x" + Integer.toHexString(propertyId) + ")"; 68 } 69 return "0x" + Integer.toHexString(propertyId); 70 } 71 72 /** 73 * Returns {@code true} if {@code propertyId} is defined in {@link VehiclePropertyIds}. 74 * {@code false} otherwise. 75 */ isDefined(int propertyId)76 public static boolean isDefined(int propertyId) { 77 return toName(propertyId) != null; 78 } 79 } 80