• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.reporting;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.net.Uri;
22 
23 import org.json.JSONArray;
24 
25 import java.util.ArrayList;
26 import java.util.Comparator;
27 import java.util.List;
28 import java.util.stream.Collectors;
29 
30 /** Report related utility methods. */
31 public class ReportUtil {
32     /**
33      * Prepares an alphabetical ordered list of attribution destinations for report JSON. If any
34      * elements are found to be null, they are added at last.
35      *
36      * @param destinations a list of attribution destinations
37      * @return an Object that is either String or JSONArray
38      */
39     @Nullable
serializeAttributionDestinations(@onNull List<Uri> destinations)40     public static Object serializeAttributionDestinations(@NonNull List<Uri> destinations) {
41         if (destinations.size() == 0) {
42             throw new IllegalArgumentException("Destinations list is empty");
43         }
44 
45         if (destinations.size() == 1) {
46             return destinations.get(0).toString();
47         } else {
48             List<Uri> sortedDestinations = new ArrayList<>(destinations);
49             sortedDestinations.sort(Comparator.comparing(Uri::toString));
50             return new JSONArray(
51                     sortedDestinations.stream().map(Uri::toString).collect(Collectors.toList()));
52         }
53     }
54 }
55