1 /* 2 * Copyright (C) 2022 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 com.android.adservices.service.measurement.util.UnsignedLong; 20 21 import org.json.JSONException; 22 import org.json.JSONObject; 23 24 import java.math.BigInteger; 25 import java.util.Objects; 26 27 /** 28 * POJO for AggregateReportPayload, the result for Aggregate API. 29 */ 30 public class AggregateHistogramContribution { 31 public static final String BUCKET = "bucket"; 32 public static final String VALUE = "value"; 33 public static final String ID = "id"; 34 private BigInteger mKey; // Equivalent to uint128 in C++. 35 private int mValue; 36 private UnsignedLong mId; 37 AggregateHistogramContribution()38 private AggregateHistogramContribution() { 39 mKey = BigInteger.valueOf(0L); 40 } 41 42 @Override equals(Object obj)43 public boolean equals(Object obj) { 44 if (!(obj instanceof AggregateHistogramContribution)) { 45 return false; 46 } 47 AggregateHistogramContribution aggregateHistogramContribution = 48 (AggregateHistogramContribution) obj; 49 return Objects.equals(mKey, aggregateHistogramContribution.mKey) 50 && mValue == aggregateHistogramContribution.mValue 51 && Objects.equals(mId, aggregateHistogramContribution.mId); 52 } 53 54 @Override hashCode()55 public int hashCode() { 56 return Objects.hash(mKey, mValue, mId); 57 } 58 59 /** 60 * Creates JSONObject for this histogram contribution. 61 */ toJSONObject()62 public JSONObject toJSONObject() throws JSONException { 63 JSONObject jsonObject = new JSONObject(); 64 jsonObject.put(BUCKET, mKey.toString()); 65 jsonObject.put(VALUE, mValue); 66 if (mId != null) { 67 jsonObject.put(ID, mId.toString()); 68 } 69 return jsonObject; 70 } 71 72 /** 73 * Encrypted Key for the aggregate histogram contribution. 74 */ getKey()75 public BigInteger getKey() { 76 return mKey; 77 } 78 79 /** 80 * Value for the aggregate histogram contribution. 81 */ getValue()82 public int getValue() { 83 return mValue; 84 } 85 86 /** Id for the aggregate histogram contribution. */ getId()87 public UnsignedLong getId() { 88 return mId; 89 } 90 91 /** 92 * Builder for {@link AggregateHistogramContribution}. 93 */ 94 public static final class Builder { 95 private final AggregateHistogramContribution mAggregateHistogramContribution; 96 Builder()97 public Builder() { 98 mAggregateHistogramContribution = new AggregateHistogramContribution(); 99 } 100 101 /** 102 * See {@link AggregateHistogramContribution#getKey()}. 103 */ setKey(BigInteger key)104 public Builder setKey(BigInteger key) { 105 mAggregateHistogramContribution.mKey = key; 106 return this; 107 } 108 109 /** 110 * See {@link AggregateHistogramContribution#getValue()}. 111 */ setValue(int value)112 public Builder setValue(int value) { 113 mAggregateHistogramContribution.mValue = value; 114 return this; 115 } 116 117 /** See {@link AggregateHistogramContribution#getId()}. */ setId(UnsignedLong id)118 public Builder setId(UnsignedLong id) { 119 mAggregateHistogramContribution.mId = id; 120 return this; 121 } 122 123 /** 124 * Builds a {@link AggregateHistogramContribution} from the provided json object. 125 * 126 * @param jsonObject json to deserialize 127 * @return {@link AggregateHistogramContribution} 128 * @throws JSONException if the json deserialization fails 129 */ fromJsonObject(JSONObject jsonObject)130 public AggregateHistogramContribution fromJsonObject(JSONObject jsonObject) 131 throws JSONException { 132 AggregateHistogramContribution aggregateHistogramContribution = 133 new AggregateHistogramContribution(); 134 aggregateHistogramContribution.mKey = new BigInteger(jsonObject.getString(BUCKET)); 135 aggregateHistogramContribution.mValue = jsonObject.getInt(VALUE); 136 if (!jsonObject.isNull(ID)) { 137 aggregateHistogramContribution.mId = new UnsignedLong(jsonObject.getString(ID)); 138 } 139 return aggregateHistogramContribution; 140 } 141 142 /** 143 * Return a builder that builds an empty (key = 0x0, value = 0) histogram contribution. Used 144 * for padding. 145 * 146 * @return {@link AggregateHistogramContribution.Builder} 147 */ setPaddingContribution()148 public Builder setPaddingContribution() { 149 mAggregateHistogramContribution.mKey = BigInteger.valueOf(0L); 150 mAggregateHistogramContribution.mValue = 0; 151 152 return this; 153 } 154 155 /** 156 * Return a builder that builds an empty (key = 0x0, value = 0) histogram contribution. Used 157 * for padding. 158 * 159 * @return {@link AggregateHistogramContribution.Builder} 160 */ setPaddingContributionWithFilteringId()161 public Builder setPaddingContributionWithFilteringId() { 162 mAggregateHistogramContribution.mKey = BigInteger.ZERO; 163 mAggregateHistogramContribution.mValue = 0; 164 mAggregateHistogramContribution.mId = UnsignedLong.ZERO; 165 return this; 166 } 167 168 /** Build the {@link AggregateHistogramContribution}. */ build()169 public AggregateHistogramContribution build() { 170 return mAggregateHistogramContribution; 171 } 172 } 173 } 174