Rename getObservers & getActiveObservers

Test: refactoring CL.
bug: 37568231
Change-Id: I92dd3e5799c4eac369079bfd35af186a134068e5
diff --git a/lifecycle/extensions/src/main/java/android/arch/lifecycle/ComputableLiveData.java b/lifecycle/extensions/src/main/java/android/arch/lifecycle/ComputableLiveData.java
index 4dc090c..fe18243 100644
--- a/lifecycle/extensions/src/main/java/android/arch/lifecycle/ComputableLiveData.java
+++ b/lifecycle/extensions/src/main/java/android/arch/lifecycle/ComputableLiveData.java
@@ -111,7 +111,7 @@
         @MainThread
         @Override
         public void run() {
-            boolean isActive = mLiveData.getActiveObserverCount() > 0;
+            boolean isActive = mLiveData.hasActiveObservers();
             if (mInvalid.compareAndSet(false, true)) {
                 if (isActive) {
                     // TODO if we make this class public, we should accept an executor.
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 ae71733..a06a300 100644
--- a/lifecycle/extensions/src/main/java/android/arch/lifecycle/LiveData.java
+++ b/lifecycle/extensions/src/main/java/android/arch/lifecycle/LiveData.java
@@ -59,7 +59,7 @@
 @SuppressWarnings({"WeakerAccess", "unused"})
 // TODO: Thread checks are too strict right now, we may consider automatically moving them to main
 // thread.
-public class LiveData<T> {
+public abstract class LiveData<T> {
     private final Object mDataLock = new Object();
     static final int START_VERSION = -1;
     private static final Object NOT_SET = new Object();
@@ -333,32 +333,28 @@
      * lifecycle states aren't {@link Lifecycle.State#STARTED} or {@link Lifecycle.State#RESUMED}
      * (like an Activity in the back stack).
      * <p>
-     * You can get the number of observers via {@link #getObserverCount()}.
+     * You can check if there are observers via {@link #hasObservers()}.
      */
     protected void onInactive() {
 
     }
 
     /**
-     * Returns the number of observers.
-     * <p>
-     * If called on a background thread, the value might be unreliable.
+     * Returns true if this LiveData has observers.
      *
-     * @return The number of observers
+     * @return true if this LiveData has observers
      */
-    public int getObserverCount() {
-        return mObservers.size();
+    public boolean hasObservers() {
+        return mObservers.size() > 0;
     }
 
     /**
-     * Returns the number of active observers.
-     * <p>
-     * If called on a background thread, the value might be unreliable.
+     * Returns true if this LiveData has active observers.
      *
-     * @return The number of active observers
+     * @return true if this LiveData has active observers
      */
-    public int getActiveObserverCount() {
-        return mActiveCount;
+    public boolean hasActiveObservers() {
+        return mActiveCount > 0;
     }
 
     class LifecycleBoundObserver implements LifecycleObserver {
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 6cfb012..cf76b48 100644
--- a/lifecycle/extensions/src/main/java/android/arch/lifecycle/MediatorLiveData.java
+++ b/lifecycle/extensions/src/main/java/android/arch/lifecycle/MediatorLiveData.java
@@ -51,7 +51,7 @@
     public <S> void addSource(LiveData<S> source, Observer<S> onChanged) {
         Source<S> e = new Source<>(source, onChanged);
         mSources.add(e);
-        if (getActiveObserverCount() > 0) {
+        if (hasActiveObservers()) {
             e.plug();
         }
     }
diff --git a/lifecycle/extensions/src/test/java/android/arch/lifecycle/LiveDataTest.java b/lifecycle/extensions/src/test/java/android/arch/lifecycle/LiveDataTest.java
index b80ab99..f401e1c 100644
--- a/lifecycle/extensions/src/test/java/android/arch/lifecycle/LiveDataTest.java
+++ b/lifecycle/extensions/src/test/java/android/arch/lifecycle/LiveDataTest.java
@@ -82,13 +82,13 @@
         mLiveData.observe(mOwner, observer);
 
         verify(mActiveObserversChanged, never()).onCall(anyBoolean());
-        assertThat(mLiveData.getObserverCount(), is(1));
-        assertThat(mLiveData.getActiveObserverCount(), is(0));
+        assertThat(mLiveData.hasObservers(), is(true));
+        assertThat(mLiveData.hasActiveObservers(), is(false));
 
         mLiveData.removeObserver(observer);
         verify(mActiveObserversChanged, never()).onCall(anyBoolean());
-        assertThat(mLiveData.getObserverCount(), is(0));
-        assertThat(mLiveData.getActiveObserverCount(), is(0));
+        assertThat(mLiveData.hasObservers(), is(false));
+        assertThat(mLiveData.hasActiveObservers(), is(false));
     }
 
     @Test
@@ -97,30 +97,30 @@
         mLiveData.observe(mOwner, observer);
 
         verify(mActiveObserversChanged, never()).onCall(anyBoolean());
-        assertThat(mLiveData.getObserverCount(), is(1));
-        assertThat(mLiveData.getActiveObserverCount(), is(0));
+        assertThat(mLiveData.hasObservers(), is(true));
+        assertThat(mLiveData.hasActiveObservers(), is(false));
 
         mRegistry.handleLifecycleEvent(ON_START);
         verify(mActiveObserversChanged).onCall(true);
-        assertThat(mLiveData.getActiveObserverCount(), is(1));
+        assertThat(mLiveData.hasActiveObservers(), is(true));
         reset(mActiveObserversChanged);
 
         mRegistry.handleLifecycleEvent(ON_STOP);
         verify(mActiveObserversChanged).onCall(false);
-        assertThat(mLiveData.getActiveObserverCount(), is(0));
-        assertThat(mLiveData.getObserverCount(), is(1));
+        assertThat(mLiveData.hasActiveObservers(), is(false));
+        assertThat(mLiveData.hasObservers(), is(true));
 
         reset(mActiveObserversChanged);
         mRegistry.handleLifecycleEvent(ON_START);
         verify(mActiveObserversChanged).onCall(true);
-        assertThat(mLiveData.getActiveObserverCount(), is(1));
-        assertThat(mLiveData.getObserverCount(), is(1));
+        assertThat(mLiveData.hasActiveObservers(), is(true));
+        assertThat(mLiveData.hasObservers(), is(true));
 
         reset(mActiveObserversChanged);
         mLiveData.removeObserver(observer);
         verify(mActiveObserversChanged).onCall(false);
-        assertThat(mLiveData.getActiveObserverCount(), is(0));
-        assertThat(mLiveData.getObserverCount(), is(0));
+        assertThat(mLiveData.hasActiveObservers(), is(false));
+        assertThat(mLiveData.hasObservers(), is(false));
 
         verifyNoMoreInteractions(mActiveObserversChanged);
     }
@@ -130,7 +130,7 @@
         Observer<String> observer = (Observer<String>) mock(Observer.class);
         mLiveData.observe(mOwner, observer);
         mLiveData.observe(mOwner, observer);
-        assertThat(mLiveData.getObserverCount(), is(1));
+        assertThat(mLiveData.hasObservers(), is(true));
     }
 
     @Test
@@ -139,7 +139,7 @@
         Observer<String> o2 = (Observer<String>) mock(Observer.class);
         mLiveData.observe(mOwner, o1);
         mLiveData.observe(mOwner, o2);
-        assertThat(mLiveData.getObserverCount(), is(2));
+        assertThat(mLiveData.hasObservers(), is(true));
         verify(mActiveObserversChanged, never()).onCall(anyBoolean());
 
         mRegistry.handleLifecycleEvent(ON_START);
@@ -150,7 +150,7 @@
 
         mLiveData.removeObservers(mOwner);
 
-        assertThat(mLiveData.getObserverCount(), is(0));
+        assertThat(mLiveData.hasObservers(), is(false));
         assertThat(mRegistry.getObserverCount(), is(0));
     }
 
@@ -180,14 +180,14 @@
         mLiveData.observe(mOwner, observer);
         mRegistry.handleLifecycleEvent(ON_START);
         verify(mActiveObserversChanged).onCall(true);
-        assertThat(mLiveData.getObserverCount(), is(1));
-        assertThat(mLiveData.getActiveObserverCount(), is(1));
+        assertThat(mLiveData.hasObservers(), is(true));
+        assertThat(mLiveData.hasActiveObservers(), is(true));
 
         reset(mActiveObserversChanged);
 
         mRegistry.handleLifecycleEvent(ON_DESTROY);
-        assertThat(mLiveData.getObserverCount(), is(0));
-        assertThat(mLiveData.getActiveObserverCount(), is(0));
+        assertThat(mLiveData.hasObservers(), is(false));
+        assertThat(mLiveData.hasActiveObservers(), is(false));
         verify(mActiveObserversChanged).onCall(false);
     }
 
@@ -196,7 +196,7 @@
         Observer<String> observer = (Observer<String>) mock(Observer.class);
         mRegistry.handleLifecycleEvent(ON_DESTROY);
         mLiveData.observe(mOwner, observer);
-        assertThat(mLiveData.getObserverCount(), is(0));
+        assertThat(mLiveData.hasObservers(), is(false));
     }
 
     @Test
@@ -253,8 +253,8 @@
             @Override
             public void onChanged(@Nullable String s) {
                 mRegistry.handleLifecycleEvent(ON_STOP);
-                assertThat(mLiveData.getObserverCount(), is(2));
-                assertThat(mLiveData.getActiveObserverCount(), is(0));
+                assertThat(mLiveData.hasObservers(), is(true));
+                assertThat(mLiveData.hasActiveObservers(), is(false));
             }
         });
         final Observer observer2 = mock(Observer.class);
@@ -263,8 +263,8 @@
         mLiveData.setValue("bla");
         verify(observer1).onChanged(anyString());
         verify(observer2, Mockito.never()).onChanged(anyString());
-        assertThat(mLiveData.getObserverCount(), is(2));
-        assertThat(mLiveData.getActiveObserverCount(), is(0));
+        assertThat(mLiveData.hasObservers(), is(true));
+        assertThat(mLiveData.hasActiveObservers(), is(false));
     }
 
     @Test
@@ -275,7 +275,7 @@
                 assertThat(mInObserver, is(false));
                 mInObserver = true;
                 mRegistry.handleLifecycleEvent(ON_START);
-                assertThat(mLiveData.getActiveObserverCount(), is(2));
+                assertThat(mLiveData.hasActiveObservers(), is(true));
                 mInObserver = false;
             }
         });
@@ -285,8 +285,8 @@
         mLiveData.setValue("bla");
         verify(observer1).onChanged(anyString());
         verify(observer2).onChanged(anyString());
-        assertThat(mLiveData.getObserverCount(), is(2));
-        assertThat(mLiveData.getActiveObserverCount(), is(2));
+        assertThat(mLiveData.hasObservers(), is(true));
+        assertThat(mLiveData.hasActiveObservers(), is(true));
     }
 
     @Test
@@ -295,15 +295,15 @@
         Observer<String> observer = spy(new Observer<String>() {
             @Override
             public void onChanged(@Nullable String s) {
-                assertThat(mLiveData.getObserverCount(), is(1));
+                assertThat(mLiveData.hasObservers(), is(true));
                 mLiveData.removeObserver(this);
-                assertThat(mLiveData.getObserverCount(), is(0));
+                assertThat(mLiveData.hasObservers(), is(false));
             }
         });
         mLiveData.observe(mOwner, observer);
         mLiveData.setValue("bla");
         verify(observer).onChanged(anyString());
-        assertThat(mLiveData.getObserverCount(), is(0));
+        assertThat(mLiveData.hasObservers(), is(false));
     }
 
     @Test
@@ -316,8 +316,8 @@
                 assertThat(mInObserver, is(false));
                 mInObserver = true;
                 mLiveData.observe(mOwner, observer2);
-                assertThat(mLiveData.getObserverCount(), is(2));
-                assertThat(mLiveData.getActiveObserverCount(), is(2));
+                assertThat(mLiveData.hasObservers(), is(true));
+                assertThat(mLiveData.hasActiveObservers(), is(true));
                 mInObserver = false;
             }
         });
@@ -325,8 +325,8 @@
         mLiveData.setValue("bla");
         verify(observer1).onChanged(anyString());
         verify(observer2).onChanged(anyString());
-        assertThat(mLiveData.getObserverCount(), is(2));
-        assertThat(mLiveData.getActiveObserverCount(), is(2));
+        assertThat(mLiveData.hasObservers(), is(true));
+        assertThat(mLiveData.hasActiveObservers(), is(true));
     }
 
     @Test
diff --git a/lifecycle/extensions/src/test/java/android/arch/lifecycle/MediatorLiveDataTest.java b/lifecycle/extensions/src/test/java/android/arch/lifecycle/MediatorLiveDataTest.java
index 5b07e1f..5c1f1c8 100644
--- a/lifecycle/extensions/src/test/java/android/arch/lifecycle/MediatorLiveDataTest.java
+++ b/lifecycle/extensions/src/test/java/android/arch/lifecycle/MediatorLiveDataTest.java
@@ -146,14 +146,14 @@
         // Imitates a destruction of a ViewModel: a listener of LiveData is destroyed,
         // a reference to MediatorLiveData is cleaned up. In this case we shouldn't leak
         // MediatorLiveData as an observer of mSource.
-        assertThat(mSource.getObserverCount(), is(0));
+        assertThat(mSource.hasObservers(), is(false));
         Observer observer = mock(Observer.class);
         mMediator.addSource(mSource, observer);
-        assertThat(mSource.getObserverCount(), is(1));
+        assertThat(mSource.hasObservers(), is(true));
         mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
         mRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
         mMediator = null;
-        assertThat(mSource.getObserverCount(), is(0));
+        assertThat(mSource.hasObservers(), is(false));
     }
 
     @Test
diff --git a/lifecycle/extensions/src/test/java/android/arch/lifecycle/ThreadedLiveDataTest.java b/lifecycle/extensions/src/test/java/android/arch/lifecycle/ThreadedLiveDataTest.java
index 49473a1..e578546 100644
--- a/lifecycle/extensions/src/test/java/android/arch/lifecycle/ThreadedLiveDataTest.java
+++ b/lifecycle/extensions/src/test/java/android/arch/lifecycle/ThreadedLiveDataTest.java
@@ -49,7 +49,7 @@
 
     @Before
     public void init() {
-        mLiveData = new LiveData<>();
+        mLiveData = new MutableLiveData<>();
         mLifecycleOwner = mock(LifecycleOwner.class);
         mRegistry = new LifecycleRegistry(mLifecycleOwner);
         when(mLifecycleOwner.getLifecycle()).thenReturn(mRegistry);
diff --git a/lifecycle/extensions/src/test/java/android/arch/lifecycle/TransformationsTest.java b/lifecycle/extensions/src/test/java/android/arch/lifecycle/TransformationsTest.java
index c5861be..9f3f933 100644
--- a/lifecycle/extensions/src/test/java/android/arch/lifecycle/TransformationsTest.java
+++ b/lifecycle/extensions/src/test/java/android/arch/lifecycle/TransformationsTest.java
@@ -56,7 +56,7 @@
 
     @Test
     public void testMap() {
-        LiveData<String> source = new LiveData<>();
+        LiveData<String> source = new MutableLiveData<>();
         LiveData<Integer> mapped = Transformations.map(source, new Function<String, Integer>() {
             @Override
             public Integer apply(String input) {
@@ -71,9 +71,9 @@
 
     @Test
     public void testSwitchMap() {
-        LiveData<Integer> trigger = new LiveData<>();
-        final LiveData<String> first = new LiveData<>();
-        final LiveData<String> second = new LiveData<>();
+        LiveData<Integer> trigger = new MutableLiveData<>();
+        final LiveData<String> first = new MutableLiveData<>();
+        final LiveData<String> second = new MutableLiveData<>();
         LiveData<String> result = Transformations.switchMap(trigger,
                 new Function<Integer, LiveData<String>>() {
                     @Override
@@ -104,9 +104,9 @@
 
     @Test
     public void testSwitchMap2() {
-        LiveData<Integer> trigger = new LiveData<>();
-        final LiveData<String> first = new LiveData<>();
-        final LiveData<String> second = new LiveData<>();
+        LiveData<Integer> trigger = new MutableLiveData<>();
+        final LiveData<String> first = new MutableLiveData<>();
+        final LiveData<String> second = new MutableLiveData<>();
         LiveData<String> result = Transformations.switchMap(trigger,
                 new Function<Integer, LiveData<String>>() {
                     @Override
@@ -142,8 +142,8 @@
 
     @Test
     public void testNoRedispatchSwitchMap() {
-        LiveData<Integer> trigger = new LiveData<>();
-        final LiveData<String> first = new LiveData<>();
+        LiveData<Integer> trigger = new MutableLiveData<>();
+        final LiveData<String> first = new MutableLiveData<>();
         LiveData<String> result = Transformations.switchMap(trigger,
                 new Function<Integer, LiveData<String>>() {
                     @Override
diff --git a/lifecycle/reactivestreams/src/main/java/android/arch/lifecycle/LiveDataReactiveStreams.java b/lifecycle/reactivestreams/src/main/java/android/arch/lifecycle/LiveDataReactiveStreams.java
index a9c6ca4..0be0149 100644
--- a/lifecycle/reactivestreams/src/main/java/android/arch/lifecycle/LiveDataReactiveStreams.java
+++ b/lifecycle/reactivestreams/src/main/java/android/arch/lifecycle/LiveDataReactiveStreams.java
@@ -135,9 +135,9 @@
      * Creates an Observable {@link LiveData} stream from a ReactiveStreams publisher.
      */
     public static <T> LiveData<T> fromPublisher(final Publisher<T> publisher) {
-        LiveData<T> liveData = new LiveData<>();
+        MutableLiveData<T> liveData = new MutableLiveData<>();
         // Since we don't have a way to directly observe cancels, weakly hold the live data.
-        final WeakReference<LiveData<T>> liveDataRef = new WeakReference<>(liveData);
+        final WeakReference<MutableLiveData<T>> liveDataRef = new WeakReference<>(liveData);
 
         publisher.subscribe(new Subscriber<T>() {
             @Override
diff --git a/lifecycle/reactivestreams/src/test/java/android/arch/lifecycle/LiveDataReactiveStreamsTest.java b/lifecycle/reactivestreams/src/test/java/android/arch/lifecycle/LiveDataReactiveStreamsTest.java
index ba4a61b..87fba27 100644
--- a/lifecycle/reactivestreams/src/test/java/android/arch/lifecycle/LiveDataReactiveStreamsTest.java
+++ b/lifecycle/reactivestreams/src/test/java/android/arch/lifecycle/LiveDataReactiveStreamsTest.java
@@ -166,7 +166,7 @@
 
     @Test
     public void convertsToPublisherWithSyncData() {
-        LiveData<String> liveData = new LiveData<>();
+        MutableLiveData<String> liveData = new MutableLiveData<>();
         liveData.setValue("foo");
         assertThat(liveData.getValue(), is("foo"));
 
@@ -183,7 +183,7 @@
 
     @Test
     public void convertingToPublisherIsCancelable() {
-        LiveData<String> liveData = new LiveData<>();
+        MutableLiveData<String> liveData = new MutableLiveData<>();
         liveData.setValue("foo");
         assertThat(liveData.getValue(), is("foo"));
 
@@ -199,7 +199,7 @@
         liveData.setValue("bar");
         liveData.setValue("baz");
 
-        assertThat(liveData.getObserverCount(), is(1));
+        assertThat(liveData.hasObservers(), is(true));
         disposable.dispose();
 
         liveData.setValue("fizz");
@@ -207,12 +207,12 @@
 
         assertThat(mLiveDataOutput, is(Arrays.asList("foo", "bar", "baz")));
         // Canceling disposable should also remove livedata mObserver.
-        assertThat(liveData.getObserverCount(), is(0));
+        assertThat(liveData.hasObservers(), is(false));
     }
 
     @Test
     public void convertsToPublisherWithBackpressure() {
-        LiveData<String> liveData = new LiveData<>();
+        MutableLiveData<String> liveData = new MutableLiveData<>();
 
         final AsyncSubject<Subscription> subscriptionSubject = AsyncSubject.create();
 
@@ -273,7 +273,7 @@
 
     @Test
     public void convertsToPublisherWithAsyncData() {
-        LiveData<String> liveData = new LiveData<>();
+        MutableLiveData<String> liveData = new MutableLiveData<>();
 
         Flowable.fromPublisher(LiveDataReactiveStreams.toPublisher(S_LIFECYCLE_OWNER, liveData))
                 .observeOn(sBackgroundScheduler)