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 package android.adservices.topics;
17 
18 import android.annotation.FlaggedApi;
19 import android.annotation.NonNull;
20 
21 import com.android.adservices.flags.Flags;
22 
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Objects;
26 
27 /** Represent the result from the getTopics API. */
28 public final class GetTopicsResponse {
29     /** List of Topic objects returned by getTopics API. */
30     private final List<Topic> mTopics;
31 
32     /** List of EncryptedTopic objects returned by getTopics API. */
33     private final List<EncryptedTopic> mEncryptedTopics;
34 
GetTopicsResponse(List<Topic> topics, List<EncryptedTopic> encryptedTopics)35     private GetTopicsResponse(List<Topic> topics, List<EncryptedTopic> encryptedTopics) {
36         mTopics = topics;
37         mEncryptedTopics = encryptedTopics;
38     }
39 
40     /** Returns a {@link List} of {@link Topic} objects returned by getTopics API. */
41     @NonNull
getTopics()42     public List<Topic> getTopics() {
43         return mTopics;
44     }
45 
46     /** Returns a {@link List} of {@link EncryptedTopic} objects returned by getTopics API. */
47     @NonNull
48     @FlaggedApi(Flags.FLAG_TOPICS_ENCRYPTION_ENABLED)
getEncryptedTopics()49     public List<EncryptedTopic> getEncryptedTopics() {
50         return mEncryptedTopics;
51     }
52 
53     @Override
equals(Object o)54     public boolean equals(Object o) {
55         if (this == o) {
56             return true;
57         }
58         if (!(o instanceof GetTopicsResponse)) {
59             return false;
60         }
61         GetTopicsResponse that = (GetTopicsResponse) o;
62         return mTopics.equals(that.mTopics) && mEncryptedTopics.equals(that.mEncryptedTopics);
63     }
64 
65     @Override
hashCode()66     public int hashCode() {
67         return Objects.hash(mTopics, mEncryptedTopics);
68     }
69 
70     /**
71      * Builder for {@link GetTopicsResponse} objects. This class should be used in test
72      * implementation as expected response from Topics API
73      */
74     public static final class Builder {
75         private List<Topic> mTopics = new ArrayList<>();
76         private List<EncryptedTopic> mEncryptedTopics = new ArrayList<>();
77 
78         /**
79          * Creates a {@link Builder} for {@link GetTopicsResponse} objects.
80          *
81          * @param topics The list of the returned Topics.
82          * @deprecated This function is deprecated.
83          */
84         @Deprecated
Builder(@onNull List<Topic> topics)85         public Builder(@NonNull List<Topic> topics) {
86             mTopics = Objects.requireNonNull(topics);
87         }
88 
89         /**
90          * Creates a {@link Builder} for {@link GetTopicsResponse} objects.
91          *
92          * @param topics The list of the returned Topics.
93          * @param encryptedTopics The list of encrypted Topics.
94          */
95         @FlaggedApi(Flags.FLAG_TOPICS_ENCRYPTION_ENABLED)
Builder(@onNull List<Topic> topics, @NonNull List<EncryptedTopic> encryptedTopics)96         public Builder(@NonNull List<Topic> topics, @NonNull List<EncryptedTopic> encryptedTopics) {
97             mTopics = Objects.requireNonNull(topics);
98             mEncryptedTopics = Objects.requireNonNull(encryptedTopics);
99         }
100 
101         /**
102          * Builds a {@link GetTopicsResponse} instance.
103          *
104          * @throws IllegalArgumentException if any of the params are null.
105          */
106         @NonNull
build()107         public GetTopicsResponse build() {
108             if (mTopics == null || mEncryptedTopics == null) {
109                 throw new IllegalArgumentException("Topics is null");
110             }
111             return new GetTopicsResponse(mTopics, mEncryptedTopics);
112         }
113     }
114 }
115