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; 17 18 import android.net.Uri; 19 import com.google.protobuf.ByteString; 20 import javax.annotation.Nullable; 21 import javax.annotation.concurrent.Immutable; 22 23 /** Either a URI or a ByteString. */ 24 // TODO(b/219765048) use AutoOneOf once that's available in Android 25 @Immutable 26 public abstract class FileSource { 27 /** The possible types of source. */ 28 public enum Kind { 29 BYTESTRING, 30 URI 31 } 32 33 /** The type of this source. */ getKind()34 public abstract Kind getKind(); 35 byteString()36 public abstract ByteString byteString(); 37 uri()38 public abstract Uri uri(); 39 40 /** Create a FileSource from a ByteString. */ ofByteString(ByteString byteString)41 public static FileSource ofByteString(ByteString byteString) { 42 if (byteString == null) { 43 throw new NullPointerException(); 44 } 45 return new ImplByteString(byteString); 46 } 47 48 /** Create a FileSource from a URI. */ ofUri(Uri uri)49 public static FileSource ofUri(Uri uri) { 50 if (uri == null) { 51 throw new NullPointerException(); 52 } 53 return new ImplUri(uri); 54 } 55 56 // Parent class that each implementation will inherit from. 57 private abstract static class Parent extends FileSource { 58 @Override byteString()59 public ByteString byteString() { 60 throw new UnsupportedOperationException(getKind().toString()); 61 } 62 63 @Override uri()64 public Uri uri() { 65 throw new UnsupportedOperationException(getKind().toString()); 66 } 67 } 68 69 // Implementation when the contained property is "byteString". 70 private static final class ImplByteString extends Parent { 71 private final ByteString byteString; 72 ImplByteString(ByteString byteString)73 ImplByteString(ByteString byteString) { 74 this.byteString = byteString; 75 } 76 77 @Override byteString()78 public ByteString byteString() { 79 return byteString; 80 } 81 82 @Override getKind()83 public FileSource.Kind getKind() { 84 return FileSource.Kind.BYTESTRING; 85 } 86 87 @Override equals(@ullable Object x)88 public boolean equals(@Nullable Object x) { 89 if (x instanceof FileSource) { 90 FileSource that = (FileSource) x; 91 return this.getKind() == that.getKind() && this.byteString.equals(that.byteString()); 92 } else { 93 return false; 94 } 95 } 96 97 @Override hashCode()98 public int hashCode() { 99 return byteString.hashCode(); 100 } 101 } 102 103 // Implementation when the contained property is "uri". 104 private static final class ImplUri extends Parent { 105 private final Uri uri; 106 ImplUri(Uri uri)107 ImplUri(Uri uri) { 108 this.uri = uri; 109 } 110 111 @Override uri()112 public Uri uri() { 113 return uri; 114 } 115 116 @Override getKind()117 public FileSource.Kind getKind() { 118 return FileSource.Kind.URI; 119 } 120 121 @Override equals(@ullable Object x)122 public boolean equals(@Nullable Object x) { 123 if (x instanceof FileSource) { 124 FileSource that = (FileSource) x; 125 return this.getKind() == that.getKind() && this.uri.equals(that.uri()); 126 } else { 127 return false; 128 } 129 } 130 131 @Override hashCode()132 public int hashCode() { 133 return uri.hashCode(); 134 } 135 } 136 } 137