content: package androidx.lifecycle;
  
  import androidx.arch.core.executor.ArchTaskExecutor;
  import androidx.arch.core.internal.SafeIterableMap;
  import androidx.lifecycle.Lifecycle;
  import java.util.Iterator;
  import java.util.Map;
  
  public abstract class LiveData<T> {
      static final Object NOT_SET = new Object();
      static final int START_VERSION = -1;
      int mActiveCount;
      private boolean mChangingActiveState;
      private volatile Object mData;
      final Object mDataLock;
      private boolean mDispatchInvalidated;
      private boolean mDispatchingValue;
      private SafeIterableMap<Observer<? super T>, LiveData<T>.ObserverWrapper> mObservers;
      volatile Object mPendingData;
      private final Runnable mPostValueRunnable;
      private int mVersion;
  
      private class AlwaysActiveObserver extends LiveData<T>.ObserverWrapper {
          AlwaysActiveObserver(Observer<? super T> observer) {
              super(observer);
          }
  
          /* access modifiers changed from: package-private */
          public boolean shouldBeActive() {
              return true;
          }
      }
  
      class LifecycleBoundObserver extends LiveData<T>.ObserverWrapper implements LifecycleEventObserver {
          final LifecycleOwner mOwner;
  
          LifecycleBoundObserver(LifecycleOwner owner, Observer<? super T> observer) {
              super(observer);
              this.mOwner = owner;
          }
  
          /* access modifiers changed from: package-private */
          public void detachObserver() {
              this.mOwner.getLifecycle().removeObserver(this);
          }
  
          /* access modifiers changed from: package-private */
          public boolean isAttachedTo(LifecycleOwner owner) {
              return this.mOwner == owner;
          }
  
          public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {
              Lifecycle.State currentState = this.mOwner.getLifecycle().getCurrentState();
              if (currentState == Lifecycle.State.DESTROYED) {
                  LiveData.this.removeObserver(this.mObserver);
                  return;
              }
              Lifecycle.State state = null;
              while (state != currentState) {
                  state = currentState;
                  activeStateChanged(shouldBeActive());
                  currentState = this.mOwner.getLifecycle().getCurrentState();
              }
          }
  
          /* access modifiers changed from: package-private */
          public boolean shouldBeActive() {
              return this.mOwner.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED);
          }
      }
  
      private abstract class ObserverWrapper {
          boolean mActive;
          int mLastVersion = -1;
          final Observer<? super T> mObserver;
  
          ObserverWrapper(Observer<? super T> observer) {
              this.mObserver = observer;
          }
  
          /* access modifiers changed from: package-private */
          public void activeStateChanged(boolean newActive) {
              if (newActive != this.mActive) {
                  this.mActive = newActive;
                  LiveData.this.changeActiveCounter(newActive ? 1 : -1);
                  if (this.mActive) {
                      LiveData.this.dispatchingValue(this);
                  }
              }
          }
  
          /* access modifiers changed from: package-private */
          public void detachObserver() {
          }
  
          /* access modifiers changed from: package-private */
          public boolean isAttachedTo(LifecycleOwner owner) {
              return false;
          }
  
          /* access modifiers changed from: package-private */
          public abstract boolean shouldBeActive();
      }
  
      public LiveData() {
          this.mDataLock = new Object();
          this.mObservers = new SafeIterableMap<>();
          this.mActiveCount = 0;
          Object obj = NOT_SET;
          this.mPendingData = obj;
          this.mPostValueRunnable = new Runnable() {
              public void run() {
                  Object obj;
                  synchronized (LiveData.this.mDataLock) {
                      obj = LiveData.this.mPendingData;
                      LiveData.this.mPendingData = LiveData.NOT_SET;
                  }
                  LiveData.this.setValue(obj);
              }
          };
          this.mData = obj;
          this.mVersion = -1;
      }
  
      public LiveData(T t) {
          this.mDataLock = new Object();
          this.mObservers = new SafeIterableMap<>();
          this.mActiveCount = 0;
          this.mPendingData = NOT_SET;
          this.mPostValueRunnable = new Runnable() {
              public void run() {
                  Object obj;
                  synchronized (LiveData.this.mDataLock) {
                      obj = LiveData.this.mPendingData;
                      LiveData.this.mPendingData = LiveData.NOT_SET;
                  }
                  LiveData.this.setValue(obj);
              }
          };
          this.mData = t;
          this.mVersion = 0;
      }
  
      static void assertMainThread(String methodName) {
          if (!ArchTaskExecutor.getInstance().isMainThread()) {
              throw new IllegalStateException("Cannot invoke " + methodName + " on a background thread");
          }
      }
  
      private void considerNotify(LiveData<T>.ObserverWrapper observerWrapper) {
          if (observerWrapper.mActive) {
              if (!observerWrapper.shouldBeActive()) {
                  observerWrapper.activeStateChanged(false);
                  return;
              }
              int i = observerWrapper.mLastVersion;
              int i2 = this.mVersion;
              if (i < i2) {
                  observerWrapper.mLastVersion = i2;
                  observerWrapper.mObserver.onChanged(this.mData);
              }
          }
      }
  
      /* access modifiers changed from: package-private */
      public void changeActiveCounter(int change) {
          int i = this.mActiveCount;
          this.mActiveCount += change;
          if (!this.mChangingActiveState) {
              this.mChangingActiveState = true;
              while (true) {
                  try {
                      int i2 = this.mActiveCount;
                      if (i != i2) {
                          boolean z = i == 0 && i2 > 0;
                          boolean z2 = i > 0 && i2 == 0;
                          i = i2;
                          if (z) {
                              onActive();
                          } else if (z2) {
                              onInactive();
                          }
                      } else {
                          return;
                      }
                  } finally {
                      this.mChangingActiveState = false;
                  }
              }
          }
      }
  
      /* access modifiers changed from: package-private */
      public void dispatchingValue(LiveData<T>.ObserverWrapper observerWrapper) {
          if (this.mDispatchingValue) {
              this.mDispatchInvalidated = true;
              return;
          }
          this.mDispatchingValue = true;
          do {
              this.mDispatchInvalidated = false;
              if (observerWrapper == null) {
                  SafeIterableMap<K, V>.IteratorWithAdditions iteratorWithAdditions = this.mObservers.iteratorWithAdditions();
                  while (iteratorWithAdditions.hasNext()) {
                      considerNotify((ObserverWrapper) ((Map.Entry) iteratorWithAdditions.next()).getValue());
                      if (this.mDispatchInvalidated) {
                          break;
                      }
                  }
              } else {
                  considerNotify(observerWrapper);
                  observerWrapper = null;
              }
          } while (this.mDispatchInvalidated);
          this.mDispatchingValue = false;
      }
  
      public T getValue() {
          T t = this.mData;
          if (t != NOT_SET) {
              return t;
          }
          return null;
      }
  
      /* access modifiers changed from: package-private */
      public int getVersion() {
          return this.mVersion;
      }
  
      public boolean hasActiveObservers() {
          return this.mActiveCount > 0;
      }
  
      public boolean hasObservers() {
          return this.mObservers.size() > 0;
      }
  
      public void observe(LifecycleOwner owner, Observer<? super T> observer) {
          assertMainThread("observe");
          if (owner.getLifecycle().getCurrentState() != Lifecycle.State.DESTROYED) {
              LifecycleBoundObserver lifecycleBoundObserver = new LifecycleBoundObserver(owner, observer);
              ObserverWrapper putIfAbsent = this.mObservers.putIfAbsent(observer, lifecycleBoundObserver);
              if (putIfAbsent != null && !putIfAbsent.isAttachedTo(owner)) {
                  throw new IllegalArgumentException("Cannot add the same observer with different lifecycles");
              } else if (putIfAbsent == null) {
                  owner.getLifecycle().addObserver(lifecycleBoundObserver);
              }
          }
      }
  
      public void observeForever(Observer<? super T> observer) {
          assertMainThread("observeForever");
          AlwaysActiveObserver alwaysActiveObserver = new AlwaysActiveObserver(observer);
          ObserverWrapper putIfAbsent = this.mObservers.putIfAbsent(observer, alwaysActiveObserver);
          if (putIfAbsent instanceof LifecycleBoundObserver) {
              throw new IllegalArgumentException("Cannot add the same observer with different lifecycles");
          } else if (putIfAbsent == null) {
              alwaysActiveObserver.activeStateChanged(true);
          }
      }
  
      /* access modifiers changed from: protected */
      public void onActive() {
      }
  
      /* access modifiers changed from: protected */
      public void onInactive() {
      }
  
      /* access modifiers changed from: protected */
      public void postValue(T t) {
          boolean z;
          synchronized (this.mDataLock) {
              z = this.mPendingData == NOT_SET;
              this.mPendingData = t;
          }
          if (z) {
              ArchTaskExecutor.getInstance().postToMainThread(this.mPostValueRunnable);
          }
      }
  
      public void removeObserver(Observer<? super T> observer) {
          assertMainThread("removeObserver");
          ObserverWrapper remove = this.mObservers.remove(observer);
          if (remove != null) {
              remove.detachObserver();
              remove.activeStateChanged(false);
          }
      }
  
      public void removeObservers(LifecycleOwner owner) {
          assertMainThread("removeObservers");
          Iterator<Map.Entry<Observer<? super T>, LiveData<T>.ObserverWrapper>> it = this.mObservers.iterator();
          while (it.hasNext()) {
              Map.Entry next = it.next();
              if (((ObserverWrapper) next.getValue()).isAttachedTo(owner)) {
                  removeObserver((Observer) next.getKey());
              }
          }
      }
  
      /* access modifiers changed from: protected */
      public void setValue(T t) {
          assertMainThread("setValue");
          this.mVersion++;
          this.mData = t;
          dispatchingValue((LiveData<T>.ObserverWrapper) null);
      }
  }
  