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