• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 Google LLC
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 com.google.android.libraries.mobiledatadownload.file.backends;
17 
18 import android.net.Uri;
19 import android.text.TextUtils;
20 import com.google.android.libraries.mobiledatadownload.file.common.MalformedUriException;
21 import com.google.android.libraries.mobiledatadownload.file.common.internal.LiteTransformFragments;
22 import com.google.android.libraries.mobiledatadownload.file.transforms.TransformProtos;
23 import com.google.common.collect.ImmutableList;
24 import com.google.errorprone.annotations.CanIgnoreReturnValue;
25 import com.google.mobiledatadownload.TransformProto;
26 
27 /**
28  * Helper class for "memory" scheme Uris. The scheme is opaque, with the opaque part simply used as
29  * a key to identify the file; it is non-hierarchical.
30  */
31 public final class MemoryUri {
32 
33   /** Returns an empty "memory" scheme Uri builder. */
builder()34   public static Builder builder() {
35     return new Builder();
36   }
37 
MemoryUri()38   private MemoryUri() {}
39 
40   /** A builder for "memory" scheme Uris. */
41   public static final class Builder {
42 
43     private String key = "";
44     private final ImmutableList.Builder<String> encodedSpecs = ImmutableList.builder();
45 
Builder()46     private Builder() {}
47 
48     /** Sets the non-empty key to be used as a file identifier. */
49     @CanIgnoreReturnValue
setKey(String key)50     public Builder setKey(String key) {
51       this.key = key;
52       return this;
53     }
54 
55     /**
56      * Appends a transform to the Uri. Calling twice with the same transform replaces the original.
57      */
58     @CanIgnoreReturnValue
withTransform(TransformProto.Transform spec)59     public Builder withTransform(TransformProto.Transform spec) {
60       encodedSpecs.add(TransformProtos.toEncodedSpec(spec));
61       return this;
62     }
63 
build()64     public Uri build() throws MalformedUriException {
65       if (TextUtils.isEmpty(key)) {
66         throw new MalformedUriException("Key must be non-empty");
67       }
68       String fragment = LiteTransformFragments.joinTransformSpecs(encodedSpecs.build());
69       return new Uri.Builder()
70           .scheme(MemoryBackend.URI_SCHEME)
71           .opaquePart(key)
72           .encodedFragment(fragment)
73           .build();
74     }
75   }
76 }
77