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.adservices.service.measurement; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 22 import java.util.Objects; 23 24 /** 25 * POJO for key-value data. 26 * 27 * <p>This class is useful for creating & storing arbitrary key-value-dataType combinations. The 28 * corresponding table {@code 29 * com.android.adservices.data.measurement.MeasurementTables.KeyValueDataContract} is used to 30 * persist these objects. 31 */ 32 public class KeyValueData { 33 public enum DataType { 34 REGISTRATION_REDIRECT_COUNT 35 } 36 37 private DataType mDataType = null; 38 private String mKey = null; 39 private String mValue = null; 40 KeyValueData(DataType dataType, String key, String value)41 private KeyValueData(DataType dataType, String key, String value) { 42 mDataType = dataType; 43 mKey = key; 44 mValue = value; 45 } 46 47 /** Returns the data type. */ getDataType()48 public DataType getDataType() { 49 return mDataType; 50 } 51 52 /** Returns the key. */ getKey()53 public String getKey() { 54 return mKey; 55 } 56 57 /** Returns the raw value. */ getValue()58 public String getValue() { 59 return mValue; 60 } 61 KeyValueData()62 private KeyValueData() {} 63 64 /** Builder class for {@link KeyValueData} */ 65 public static class Builder { 66 private DataType mDataType = null; 67 private String mKey = null; 68 private String mValue = null; 69 70 /** See {@link KeyValueData#getDataType()} ()} */ setDataType(@onNull DataType dataType)71 public Builder setDataType(@NonNull DataType dataType) { 72 mDataType = dataType; 73 return this; 74 } 75 76 /** See {@link KeyValueData#getKey()} */ setKey(@onNull String key)77 public Builder setKey(@NonNull String key) { 78 mKey = key; 79 return this; 80 } 81 82 /** See {@link KeyValueData#getValue()} */ setValue(@ullable String value)83 public Builder setValue(@Nullable String value) { 84 mValue = value; 85 return this; 86 } 87 88 /** Build the {@link KeyValueData} */ build()89 public KeyValueData build() { 90 Objects.requireNonNull(mDataType); 91 Objects.requireNonNull(mKey); 92 return new KeyValueData(mDataType, mKey, mValue); 93 } 94 } 95 96 /** Get the Registration Count value */ getRegistrationRedirectCount()97 public int getRegistrationRedirectCount() { 98 if (mDataType != DataType.REGISTRATION_REDIRECT_COUNT) { 99 throw new IllegalStateException("Illegal method call"); 100 } 101 if (mValue == null) { 102 // Default value is 1, because the first registration will be the only case when value 103 // can be null. 104 return 1; 105 } 106 return Integer.parseInt(mValue); 107 } 108 109 /** Set the Registration Count value */ setRegistrationRedirectCount(int value)110 public void setRegistrationRedirectCount(int value) { 111 if (mDataType != DataType.REGISTRATION_REDIRECT_COUNT) { 112 throw new IllegalStateException("Illegal method call"); 113 } 114 mValue = String.valueOf(value); 115 } 116 } 117