• 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.internal;
17 
18 import com.google.common.util.concurrent.ListenableFuture;
19 import com.google.mobiledatadownload.ClientConfigProto.ClientFileGroup;
20 import com.google.mobiledatadownload.internal.MetadataProto.DataFileGroupInternal;
21 import javax.annotation.Nullable;
22 import javax.annotation.concurrent.Immutable;
23 
24 /** A helper class that includes information about the state of a file group download. */
25 @Immutable
26 public abstract class DownloadGroupState {
27   /** The kind of {@link DownloadGroupState}. */
28   public enum Kind {
29     /** A pending that hasn't been downloaded yet. */
30     PENDING_GROUP,
31 
32     /** A pending group whose download has already stated. */
33     IN_PROGRESS_FUTURE,
34 
35     /** A group that has already been downloaded. */
36     DOWNLOADED_GROUP,
37   }
38 
getKind()39   public abstract Kind getKind();
40 
pendingGroup()41   public abstract DataFileGroupInternal pendingGroup();
42 
inProgressFuture()43   public abstract ListenableFuture<ClientFileGroup> inProgressFuture();
44 
downloadedGroup()45   public abstract ClientFileGroup downloadedGroup();
46 
ofPendingGroup(DataFileGroupInternal dataFileGroup)47   public static DownloadGroupState ofPendingGroup(DataFileGroupInternal dataFileGroup) {
48     return new ImplPendingGroup(dataFileGroup);
49   }
50 
ofInProgressFuture( ListenableFuture<ClientFileGroup> clientFileGroupFuture)51   public static DownloadGroupState ofInProgressFuture(
52       ListenableFuture<ClientFileGroup> clientFileGroupFuture) {
53     return new ImplInProgressFuture(clientFileGroupFuture);
54   }
55 
ofDownloadedGroup(ClientFileGroup clientFileGroup)56   public static DownloadGroupState ofDownloadedGroup(ClientFileGroup clientFileGroup) {
57     return new ImplDownloadedGroup(clientFileGroup);
58   }
59 
DownloadGroupState()60   private DownloadGroupState() {}
61 
62   // Parent class that each implementation will inherit from.
63   private abstract static class Parent extends DownloadGroupState {
64     @Override
pendingGroup()65     public DataFileGroupInternal pendingGroup() {
66       throw new UnsupportedOperationException(getKind().toString());
67     }
68 
69     @Override
inProgressFuture()70     public ListenableFuture<ClientFileGroup> inProgressFuture() {
71       throw new UnsupportedOperationException(getKind().toString());
72     }
73 
74     @Override
downloadedGroup()75     public ClientFileGroup downloadedGroup() {
76       throw new UnsupportedOperationException(getKind().toString());
77     }
78   }
79 
80   // Implementation when the contained property is "pendingGroup".
81   private static final class ImplPendingGroup extends Parent {
82     private final DataFileGroupInternal pendingGroup;
83 
ImplPendingGroup(DataFileGroupInternal pendingGroup)84     ImplPendingGroup(DataFileGroupInternal pendingGroup) {
85       this.pendingGroup = pendingGroup;
86     }
87 
88     @Override
pendingGroup()89     public DataFileGroupInternal pendingGroup() {
90       return pendingGroup;
91     }
92 
93     @Override
getKind()94     public DownloadGroupState.Kind getKind() {
95       return DownloadGroupState.Kind.PENDING_GROUP;
96     }
97 
98     @Override
equals(@ullable Object x)99     public boolean equals(@Nullable Object x) {
100       if (x instanceof DownloadGroupState) {
101         DownloadGroupState that = (DownloadGroupState) x;
102         return this.getKind() == that.getKind() && this.pendingGroup.equals(that.pendingGroup());
103       } else {
104         return false;
105       }
106     }
107 
108     @Override
hashCode()109     public int hashCode() {
110       return pendingGroup.hashCode();
111     }
112   }
113 
114   // Implementation when the contained property is "inProgressFuture".
115   private static final class ImplInProgressFuture extends Parent {
116     private final ListenableFuture<ClientFileGroup> inProgressFuture;
117 
ImplInProgressFuture(ListenableFuture<ClientFileGroup> inProgressFuture)118     ImplInProgressFuture(ListenableFuture<ClientFileGroup> inProgressFuture) {
119       this.inProgressFuture = inProgressFuture;
120     }
121 
122     @Override
inProgressFuture()123     public ListenableFuture<ClientFileGroup> inProgressFuture() {
124       return inProgressFuture;
125     }
126 
127     @Override
getKind()128     public DownloadGroupState.Kind getKind() {
129       return DownloadGroupState.Kind.IN_PROGRESS_FUTURE;
130     }
131 
132     @Override
equals(@ullable Object x)133     public boolean equals(@Nullable Object x) {
134       if (x instanceof DownloadGroupState) {
135         DownloadGroupState that = (DownloadGroupState) x;
136         return this.getKind() == that.getKind()
137             && this.inProgressFuture.equals(that.inProgressFuture());
138       } else {
139         return false;
140       }
141     }
142 
143     @Override
hashCode()144     public int hashCode() {
145       return inProgressFuture.hashCode();
146     }
147   }
148 
149   // Implementation when the contained property is "downloadedGroup".
150   private static final class ImplDownloadedGroup extends Parent {
151     private final ClientFileGroup downloadedGroup;
152 
ImplDownloadedGroup(ClientFileGroup downloadedGroup)153     ImplDownloadedGroup(ClientFileGroup downloadedGroup) {
154       this.downloadedGroup = downloadedGroup;
155     }
156 
157     @Override
downloadedGroup()158     public ClientFileGroup downloadedGroup() {
159       return downloadedGroup;
160     }
161 
162     @Override
getKind()163     public DownloadGroupState.Kind getKind() {
164       return DownloadGroupState.Kind.DOWNLOADED_GROUP;
165     }
166 
167     @Override
equals(@ullable Object x)168     public boolean equals(@Nullable Object x) {
169       if (x instanceof DownloadGroupState) {
170         DownloadGroupState that = (DownloadGroupState) x;
171         return this.getKind() == that.getKind()
172             && this.downloadedGroup.equals(that.downloadedGroup());
173       } else {
174         return false;
175       }
176     }
177 
178     @Override
hashCode()179     public int hashCode() {
180       return downloadedGroup.hashCode();
181     }
182   }
183 }
184