1 /*
2  * Copyright 2020 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.example.androidx.widget.selection.fancy;
18 
19 import android.net.Uri;
20 
21 import androidx.core.util.Preconditions;
22 
23 import org.jspecify.annotations.NonNull;
24 
25 final class Uris {
26 
Uris()27     private Uris() {}
28 
29     static final String SCHEME = "content";
30     static final String AUTHORITY = "CheeseWorld";
31     static final String PARAM_GROUP = "g";
32     static final String PARAM_CHEESE = "c";
33 
forGroup(@onNull String group)34     static @NonNull Uri forGroup(@NonNull String group) {
35         return new Uri.Builder()
36                 .scheme(SCHEME)
37                 .encodedAuthority(AUTHORITY)
38                 .appendQueryParameter(PARAM_GROUP, group)
39                 .build();
40     }
41 
forCheese(@onNull String group, @NonNull String cheese)42     static @NonNull Uri forCheese(@NonNull String group, @NonNull String cheese) {
43         return new Uri.Builder()
44                 .scheme(SCHEME)
45                 .encodedAuthority(AUTHORITY)
46                 .appendQueryParameter(PARAM_GROUP, group)
47                 .appendQueryParameter(PARAM_CHEESE, cheese)
48                 .build();
49     }
50 
isGroup(@onNull Uri uri)51     static boolean isGroup(@NonNull Uri uri) {
52         return !isCheese(uri);
53     }
54 
isCheese(@onNull Uri uri)55     static boolean isCheese(@NonNull Uri uri) {
56         return uri.getQueryParameter(PARAM_GROUP) != null
57                 && uri.getQueryParameter(PARAM_CHEESE) != null;
58     }
59 
getGroup(@onNull Uri uri)60     static @NonNull String getGroup(@NonNull Uri uri) {
61         String group = uri.getQueryParameter(PARAM_GROUP);
62         Preconditions.checkArgument(group != null);
63         return group;
64     }
65 
getCheese(@onNull Uri uri)66     static @NonNull String getCheese(@NonNull Uri uri) {
67         Preconditions.checkArgument(isCheese(uri));
68         return uri.getQueryParameter(PARAM_CHEESE);
69     }
70 }
71