Merge "@NonNull in lifecycle modules" into oc-mr1-support-27.0-dev
diff --git a/lifecycle/common/src/main/java/android/arch/lifecycle/Lifecycle.java b/lifecycle/common/src/main/java/android/arch/lifecycle/Lifecycle.java
index 02db5ff..c0a2090 100644
--- a/lifecycle/common/src/main/java/android/arch/lifecycle/Lifecycle.java
+++ b/lifecycle/common/src/main/java/android/arch/lifecycle/Lifecycle.java
@@ -17,6 +17,7 @@
package android.arch.lifecycle;
import android.support.annotation.MainThread;
+import android.support.annotation.NonNull;
/**
* Defines an object that has an Android Lifecycle. {@link android.support.v4.app.Fragment Fragment}
@@ -83,7 +84,7 @@
* @param observer The observer to notify.
*/
@MainThread
- public abstract void addObserver(LifecycleObserver observer);
+ public abstract void addObserver(@NonNull LifecycleObserver observer);
/**
* Removes the given observer from the observers list.
@@ -99,7 +100,7 @@
* @param observer The observer to be removed.
*/
@MainThread
- public abstract void removeObserver(LifecycleObserver observer);
+ public abstract void removeObserver(@NonNull LifecycleObserver observer);
/**
* Returns the current state of the Lifecycle.
@@ -193,7 +194,7 @@
* @param state State to compare with
* @return true if this State is greater or equal to the given {@code state}
*/
- public boolean isAtLeast(State state) {
+ public boolean isAtLeast(@NonNull State state) {
return compareTo(state) >= 0;
}
}
diff --git a/lifecycle/common/src/main/java/android/arch/lifecycle/LifecycleOwner.java b/lifecycle/common/src/main/java/android/arch/lifecycle/LifecycleOwner.java
index 934cf3a..068bac1 100644
--- a/lifecycle/common/src/main/java/android/arch/lifecycle/LifecycleOwner.java
+++ b/lifecycle/common/src/main/java/android/arch/lifecycle/LifecycleOwner.java
@@ -16,6 +16,8 @@
package android.arch.lifecycle;
+import android.support.annotation.NonNull;
+
/**
* A class that has an Android lifecycle. These events can be used by custom components to
* handle lifecycle changes without implementing any code inside the Activity or the Fragment.
@@ -29,5 +31,6 @@
*
* @return The lifecycle of the provider.
*/
+ @NonNull
Lifecycle getLifecycle();
}
diff --git a/lifecycle/extensions/src/main/java/android/arch/lifecycle/AndroidViewModel.java b/lifecycle/extensions/src/main/java/android/arch/lifecycle/AndroidViewModel.java
index 2c7e173..106b2ef 100644
--- a/lifecycle/extensions/src/main/java/android/arch/lifecycle/AndroidViewModel.java
+++ b/lifecycle/extensions/src/main/java/android/arch/lifecycle/AndroidViewModel.java
@@ -16,7 +16,9 @@
package android.arch.lifecycle;
+import android.annotation.SuppressLint;
import android.app.Application;
+import android.support.annotation.NonNull;
/**
* Application context aware {@link ViewModel}.
@@ -25,16 +27,19 @@
* <p>
*/
public class AndroidViewModel extends ViewModel {
+ @SuppressLint("StaticFieldLeak")
private Application mApplication;
- public AndroidViewModel(Application application) {
+ public AndroidViewModel(@NonNull Application application) {
mApplication = application;
}
/**
* Return the application.
*/
+ @NonNull
public <T extends Application> T getApplication() {
+ //noinspection unchecked
return (T) mApplication;
}
}
diff --git a/lifecycle/extensions/src/main/java/android/arch/lifecycle/LiveData.java b/lifecycle/extensions/src/main/java/android/arch/lifecycle/LiveData.java
index 4c0b7a4..c01d352 100644
--- a/lifecycle/extensions/src/main/java/android/arch/lifecycle/LiveData.java
+++ b/lifecycle/extensions/src/main/java/android/arch/lifecycle/LiveData.java
@@ -23,6 +23,7 @@
import android.arch.core.internal.SafeIterableMap;
import android.arch.lifecycle.Lifecycle.State;
import android.support.annotation.MainThread;
+import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.util.Iterator;
@@ -182,7 +183,7 @@
* @param observer The observer that will receive the events
*/
@MainThread
- public void observe(LifecycleOwner owner, Observer<T> observer) {
+ public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<T> observer) {
if (owner.getLifecycle().getCurrentState() == DESTROYED) {
// ignore
return;
@@ -214,7 +215,7 @@
* @param observer The observer that will receive the events
*/
@MainThread
- public void observeForever(Observer<T> observer) {
+ public void observeForever(@NonNull Observer<T> observer) {
observe(ALWAYS_ON, observer);
}
@@ -224,7 +225,7 @@
* @param observer The Observer to receive events.
*/
@MainThread
- public void removeObserver(final Observer<T> observer) {
+ public void removeObserver(@NonNull final Observer<T> observer) {
assertMainThread("removeObserver");
LifecycleBoundObserver removed = mObservers.remove(observer);
if (removed == null) {
@@ -240,7 +241,7 @@
* @param owner The {@code LifecycleOwner} scope for the observers to be removed.
*/
@MainThread
- public void removeObservers(final LifecycleOwner owner) {
+ public void removeObservers(@NonNull final LifecycleOwner owner) {
assertMainThread("removeObservers");
for (Map.Entry<Observer<T>, LifecycleBoundObserver> entry : mObservers) {
if (entry.getValue().owner == owner) {
diff --git a/lifecycle/extensions/src/main/java/android/arch/lifecycle/MediatorLiveData.java b/lifecycle/extensions/src/main/java/android/arch/lifecycle/MediatorLiveData.java
index 718c79e..5864739 100644
--- a/lifecycle/extensions/src/main/java/android/arch/lifecycle/MediatorLiveData.java
+++ b/lifecycle/extensions/src/main/java/android/arch/lifecycle/MediatorLiveData.java
@@ -19,6 +19,7 @@
import android.arch.core.internal.SafeIterableMap;
import android.support.annotation.CallSuper;
import android.support.annotation.MainThread;
+import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.util.Map;
@@ -81,7 +82,7 @@
* @param <S> The type of data hold by {@code source} LiveData
*/
@MainThread
- public <S> void addSource(LiveData<S> source, Observer<S> onChanged) {
+ public <S> void addSource(@NonNull LiveData<S> source, @NonNull Observer<S> onChanged) {
Source<S> e = new Source<>(source, onChanged);
Source<?> existing = mSources.putIfAbsent(source, e);
if (existing != null && existing.mObserver != onChanged) {
@@ -103,7 +104,7 @@
* @param <S> the type of data hold by {@code source} LiveData
*/
@MainThread
- public <S> void removeSource(LiveData<S> toRemote) {
+ public <S> void removeSource(@NonNull LiveData<S> toRemote) {
Source<?> source = mSources.remove(toRemote);
if (source != null) {
source.unplug();
diff --git a/lifecycle/extensions/src/main/java/android/arch/lifecycle/ProcessLifecycleOwner.java b/lifecycle/extensions/src/main/java/android/arch/lifecycle/ProcessLifecycleOwner.java
index e2a1256..ec8274c 100644
--- a/lifecycle/extensions/src/main/java/android/arch/lifecycle/ProcessLifecycleOwner.java
+++ b/lifecycle/extensions/src/main/java/android/arch/lifecycle/ProcessLifecycleOwner.java
@@ -22,6 +22,7 @@
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
+import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;
/**
@@ -171,6 +172,7 @@
});
}
+ @NonNull
@Override
public Lifecycle getLifecycle() {
return mRegistry;
diff --git a/lifecycle/extensions/src/main/java/android/arch/lifecycle/Transformations.java b/lifecycle/extensions/src/main/java/android/arch/lifecycle/Transformations.java
index 9ce9cbb..c735f8b 100644
--- a/lifecycle/extensions/src/main/java/android/arch/lifecycle/Transformations.java
+++ b/lifecycle/extensions/src/main/java/android/arch/lifecycle/Transformations.java
@@ -18,6 +18,7 @@
import android.arch.core.util.Function;
import android.support.annotation.MainThread;
+import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
/**
@@ -60,7 +61,8 @@
* @return a LiveData which emits resulting values
*/
@MainThread
- public static <X, Y> LiveData<Y> map(LiveData<X> source, final Function<X, Y> func) {
+ public static <X, Y> LiveData<Y> map(@NonNull LiveData<X> source,
+ @NonNull final Function<X, Y> func) {
final MediatorLiveData<Y> result = new MediatorLiveData<>();
result.addSource(source, new Observer<X>() {
@Override
@@ -120,8 +122,8 @@
* @param <Y> a type of resulting LiveData
*/
@MainThread
- public static <X, Y> LiveData<Y> switchMap(LiveData<X> trigger,
- final Function<X, LiveData<Y>> func) {
+ public static <X, Y> LiveData<Y> switchMap(@NonNull LiveData<X> trigger,
+ @NonNull final Function<X, LiveData<Y>> func) {
final MediatorLiveData<Y> result = new MediatorLiveData<>();
result.addSource(trigger, new Observer<X>() {
LiveData<Y> mSource;
diff --git a/lifecycle/extensions/src/main/java/android/arch/lifecycle/ViewModelProvider.java b/lifecycle/extensions/src/main/java/android/arch/lifecycle/ViewModelProvider.java
index 7ef591f..29cbab8 100644
--- a/lifecycle/extensions/src/main/java/android/arch/lifecycle/ViewModelProvider.java
+++ b/lifecycle/extensions/src/main/java/android/arch/lifecycle/ViewModelProvider.java
@@ -43,7 +43,8 @@
* @param <T> The type parameter for the ViewModel.
* @return a newly created ViewModel
*/
- <T extends ViewModel> T create(Class<T> modelClass);
+ @NonNull
+ <T extends ViewModel> T create(@NonNull Class<T> modelClass);
}
private final Factory mFactory;
@@ -70,7 +71,7 @@
* @param factory factory a {@code Factory} which will be used to instantiate
* new {@code ViewModels}
*/
- public ViewModelProvider(ViewModelStore store, Factory factory) {
+ public ViewModelProvider(@NonNull ViewModelStore store, @NonNull Factory factory) {
mFactory = factory;
this.mViewModelStore = store;
}
@@ -88,7 +89,8 @@
* @param <T> The type parameter for the ViewModel.
* @return A ViewModel that is an instance of the given type {@code T}.
*/
- public <T extends ViewModel> T get(Class<T> modelClass) {
+ @NonNull
+ public <T extends ViewModel> T get(@NonNull Class<T> modelClass) {
String canonicalName = modelClass.getCanonicalName();
if (canonicalName == null) {
throw new IllegalArgumentException("Local and anonymous classes can not be ViewModels");
@@ -136,8 +138,9 @@
*/
public static class NewInstanceFactory implements Factory {
+ @NonNull
@Override
- public <T extends ViewModel> T create(Class<T> modelClass) {
+ public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
//noinspection TryWithIdenticalCatches
try {
return modelClass.newInstance();
diff --git a/lifecycle/extensions/src/main/java/android/arch/lifecycle/ViewModelProviders.java b/lifecycle/extensions/src/main/java/android/arch/lifecycle/ViewModelProviders.java
index 746162a..b4b20aa 100644
--- a/lifecycle/extensions/src/main/java/android/arch/lifecycle/ViewModelProviders.java
+++ b/lifecycle/extensions/src/main/java/android/arch/lifecycle/ViewModelProviders.java
@@ -139,8 +139,9 @@
mApplication = application;
}
+ @NonNull
@Override
- public <T extends ViewModel> T create(Class<T> modelClass) {
+ public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
if (AndroidViewModel.class.isAssignableFrom(modelClass)) {
//noinspection TryWithIdenticalCatches
try {
diff --git a/lifecycle/extensions/src/main/java/android/arch/lifecycle/ViewModelStoreOwner.java b/lifecycle/extensions/src/main/java/android/arch/lifecycle/ViewModelStoreOwner.java
index 5058305..e26fa32 100644
--- a/lifecycle/extensions/src/main/java/android/arch/lifecycle/ViewModelStoreOwner.java
+++ b/lifecycle/extensions/src/main/java/android/arch/lifecycle/ViewModelStoreOwner.java
@@ -16,6 +16,8 @@
package android.arch.lifecycle;
+import android.support.annotation.NonNull;
+
/**
* A scope that owns {@link ViewModelStore}.
* <p>
@@ -30,5 +32,6 @@
*
* @return a {@code ViewModelStore}
*/
+ @NonNull
ViewModelStore getViewModelStore();
}
diff --git a/lifecycle/extensions/src/main/java/android/arch/lifecycle/ViewModelStores.java b/lifecycle/extensions/src/main/java/android/arch/lifecycle/ViewModelStores.java
index 8c17dd9..d7d769d 100644
--- a/lifecycle/extensions/src/main/java/android/arch/lifecycle/ViewModelStores.java
+++ b/lifecycle/extensions/src/main/java/android/arch/lifecycle/ViewModelStores.java
@@ -19,6 +19,7 @@
import static android.arch.lifecycle.HolderFragment.holderFragmentFor;
import android.support.annotation.MainThread;
+import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
@@ -38,7 +39,7 @@
* @return a {@code ViewModelStore}
*/
@MainThread
- public static ViewModelStore of(FragmentActivity activity) {
+ public static ViewModelStore of(@NonNull FragmentActivity activity) {
return holderFragmentFor(activity).getViewModelStore();
}
@@ -49,7 +50,7 @@
* @return a {@code ViewModelStore}
*/
@MainThread
- public static ViewModelStore of(Fragment fragment) {
+ public static ViewModelStore of(@NonNull Fragment fragment) {
return holderFragmentFor(fragment).getViewModelStore();
}
}
diff --git a/lifecycle/runtime/src/main/java/android/arch/lifecycle/LifecycleRegistry.java b/lifecycle/runtime/src/main/java/android/arch/lifecycle/LifecycleRegistry.java
index b83e6b8..7936aee 100644
--- a/lifecycle/runtime/src/main/java/android/arch/lifecycle/LifecycleRegistry.java
+++ b/lifecycle/runtime/src/main/java/android/arch/lifecycle/LifecycleRegistry.java
@@ -97,7 +97,7 @@
* @param state new state
*/
@SuppressWarnings("WeakerAccess")
- public void markState(State state) {
+ public void markState(@NonNull State state) {
mState = state;
}
@@ -109,7 +109,7 @@
*
* @param event The event that was received
*/
- public void handleLifecycleEvent(Lifecycle.Event event) {
+ public void handleLifecycleEvent(@NonNull Lifecycle.Event event) {
mState = getStateAfter(event);
if (mHandlingEvent || mAddingObserverCounter != 0) {
mNewEventOccurred = true;
@@ -140,7 +140,7 @@
}
@Override
- public void addObserver(LifecycleObserver observer) {
+ public void addObserver(@NonNull LifecycleObserver observer) {
State initialState = mState == DESTROYED ? DESTROYED : INITIALIZED;
ObserverWithState statefulObserver = new ObserverWithState(observer, initialState);
ObserverWithState previous = mObserverMap.putIfAbsent(observer, statefulObserver);
@@ -178,7 +178,7 @@
}
@Override
- public void removeObserver(LifecycleObserver observer) {
+ public void removeObserver(@NonNull LifecycleObserver observer) {
// we consciously decided not to send destruction events here in opposition to addObserver.
// Our reasons for that:
// 1. These events haven't yet happened at all. In contrast to events in addObservers, that