1 /* 2 * Copyright 2018 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.arch.common.switching; 18 19 import androidx.annotation.NonNull; 20 import androidx.annotation.Nullable; 21 import androidx.lifecycle.LiveData; 22 23 /** 24 * Interface for a LiveData that emits the value of a given source. The LiveData can change sources 25 * at any time, and the value emitted will represent only the most recent source. Older sources will 26 * be forgotten. 27 * 28 * @param <T> The type this SwitchingLiveData will emit. 29 */ 30 public interface SwitchingLiveData<T> { 31 32 /** 33 * Returns this SwitchingLiveData as a LiveData. This method is needed due to limitations in 34 * Java syntax. 35 */ 36 @NonNull asLiveData()37 LiveData<T> asLiveData(); 38 39 /** 40 * Returns the current source as set by {@link #setSource(LiveData)} 41 */ 42 @Nullable getSource()43 LiveData<? extends T> getSource(); 44 45 /** 46 * Sets which LiveData acts as the source for this SwitchingLiveData. If {@code null}, this 47 * SwitchingLiveData will emit {@code null}. 48 */ setSource(@ullable LiveData<? extends T> source)49 void setSource(@Nullable LiveData<? extends T> source); 50 51 /** Returns a new instance of SwitchingLiveData */ newInstance()52 static <T> SwitchingLiveData<T> newInstance() { 53 return new SwitchingLiveDataImpl<>(); 54 } 55 } 56