1 /* 2 * Copyright (C) 2024 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.aggregation; 18 19 import android.annotation.NonNull; 20 21 import com.android.adservices.service.measurement.util.Validation; 22 23 import org.json.JSONArray; 24 import org.json.JSONException; 25 import org.json.JSONObject; 26 27 import java.math.BigInteger; 28 import java.util.HashSet; 29 import java.util.Objects; 30 import java.util.Set; 31 32 /** POJO for AggregateDebugReportData. */ 33 public class AggregateDebugReportData { 34 35 private final Set<String> mReportType; 36 private final BigInteger mKeyPiece; 37 private final int mValue; 38 AggregateDebugReportData(@onNull AggregateDebugReportData.Builder builder)39 private AggregateDebugReportData(@NonNull AggregateDebugReportData.Builder builder) { 40 mReportType = builder.mReportType; 41 mKeyPiece = builder.mKeyPiece; 42 mValue = builder.mValue; 43 } 44 45 @Override equals(Object obj)46 public boolean equals(Object obj) { 47 if (!(obj instanceof AggregateDebugReportData aggregateDebugReportData)) { 48 return false; 49 } 50 return Objects.equals(mReportType, aggregateDebugReportData.mReportType) 51 && Objects.equals(mKeyPiece, aggregateDebugReportData.mKeyPiece) 52 && mValue == aggregateDebugReportData.mValue; 53 } 54 55 @Override hashCode()56 public int hashCode() { 57 return Objects.hash(mReportType, mKeyPiece, mValue); 58 } 59 60 /** Returns the value of key report_type. */ getReportType()61 public Set<String> getReportType() { 62 return mReportType; 63 } 64 65 /** Returns the value of key key_piece. */ getKeyPiece()66 public BigInteger getKeyPiece() { 67 return mKeyPiece; 68 } 69 70 /** Returns the value of key value. */ getValue()71 public int getValue() { 72 return mValue; 73 } 74 75 /** Builder for {@link AggregateDebugReportData}. */ 76 public static final class Builder { 77 private Set<String> mReportType; 78 private BigInteger mKeyPiece; 79 private int mValue; 80 Builder(Set<String> reportType, BigInteger keyPiece, int value)81 public Builder(Set<String> reportType, BigInteger keyPiece, int value) { 82 mReportType = reportType; 83 mKeyPiece = keyPiece; 84 mValue = value; 85 } 86 Builder(JSONObject aggregateDebugReportData)87 public Builder(JSONObject aggregateDebugReportData) throws JSONException { 88 if (!aggregateDebugReportData.isNull(AggregateDebugReportDataHeaderContract.TYPES)) { 89 mReportType = new HashSet<>(); 90 JSONArray reportTypeList = 91 aggregateDebugReportData.getJSONArray( 92 AggregateDebugReportDataHeaderContract.TYPES); 93 for (int j = 0; j < reportTypeList.length(); j++) { 94 String reportTypeString = reportTypeList.getString(j); 95 mReportType.add(reportTypeString); 96 } 97 } 98 if (!aggregateDebugReportData.isNull( 99 AggregateDebugReportDataHeaderContract.KEY_PIECE)) { 100 String hexString = 101 aggregateDebugReportData 102 .getString(AggregateDebugReportDataHeaderContract.KEY_PIECE) 103 .substring(2); 104 mKeyPiece = new BigInteger(hexString, 16); 105 } 106 if (!aggregateDebugReportData.isNull(AggregateDebugReportDataHeaderContract.VALUE)) { 107 mValue = 108 aggregateDebugReportData.getInt( 109 AggregateDebugReportDataHeaderContract.VALUE); 110 } 111 } 112 113 /** Build the {@link AggregateDebugReportData} */ 114 @NonNull build()115 public AggregateDebugReportData build() { 116 Validation.validateNonNull(mReportType, mKeyPiece); 117 return new AggregateDebugReportData(this); 118 } 119 } 120 121 public interface AggregateDebugReportDataHeaderContract { 122 String TYPES = "types"; 123 String KEY_PIECE = "key_piece"; 124 String VALUE = "value"; 125 } 126 } 127