1 /*
2  * Copyright (C) 2021 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.car.apps.common.util;
18 
19 import androidx.annotation.Nullable;
20 
21 /**
22  * Class that holds data with a loading state, and optionally the previous version of the data.
23  *
24  * @param <T> the output data type
25  */
26 public class FutureData<T> {
27 
28     private final boolean mIsLoading;
29     private final T mPastData;
30     private final T mData;
31 
32     /** Returns an instance with a null data value and loading set to true. */
newLoadingData()33     public static <T> FutureData<T> newLoadingData() {
34         return new FutureData<>(true, null);
35     }
36 
37     /** Returns a loaded instance with the given data value. */
newLoadedData(T data)38     public static <T> FutureData<T> newLoadedData(T data) {
39         return new FutureData<>(false, data);
40     }
41 
42     /** Returns a loaded instance with the given previous and current data values. */
newLoadedData(T oldData, T newData)43     public static <T> FutureData<T> newLoadedData(T oldData, T newData) {
44         return new FutureData<>(false, oldData, newData);
45     }
46 
47     /** Returns the data contained in the future or null (when loading or no future). */
48     @Nullable
getData(@ullable FutureData<T> future)49     public static <T> T getData(@Nullable FutureData<T> future) {
50         return (future != null) ? future.getData() : null;
51     }
52 
53     /** Returns the past data contained in the future or null (when loading or no future). */
54     @Nullable
getPastData(@ullable FutureData<T> future)55     public static <T> T getPastData(@Nullable FutureData<T> future) {
56         return (future != null) ? future.getPastData() : null;
57     }
58 
59     /**
60      * This should become private.
61      * @deprecated Use {@link #newLoadingData}, and {@link #newLoadedData} instead.
62      */
63     @Deprecated
FutureData(boolean isLoading, T data)64     public FutureData(boolean isLoading, T data) {
65         this(isLoading, null, data);
66     }
67 
FutureData(boolean isLoading, T oldData, T newData)68     private FutureData(boolean isLoading, T oldData, T newData) {
69         mIsLoading = isLoading;
70         mPastData = oldData;
71         mData = newData;
72     }
73 
74     /**
75      * Gets if the data is in the process of being loaded
76      */
isLoading()77     public boolean isLoading() {
78         return mIsLoading;
79     }
80 
81     /**
82      * Gets the data, if done loading. If currently loading, returns null
83      */
getData()84     public T getData() {
85         return mData;
86     }
87 
88     /** If done loading, returns the previous version of the data, otherwise null. */
getPastData()89     public T getPastData() {
90         return mPastData;
91     }
92 }
93