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