package androidx.lifecycle; import androidx.arch.core.internal.SafeIterableMap; import java.util.Iterator; import java.util.Map; /* loaded from: classes2.dex */ public class MediatorLiveData extends MutableLiveData { private SafeIterableMap, Source> mSources; public MediatorLiveData() { this.mSources = new SafeIterableMap<>(); } public MediatorLiveData(T t) { super(t); this.mSources = new SafeIterableMap<>(); } public void addSource(LiveData liveData, Observer observer) { if (liveData == null) { throw new NullPointerException("source cannot be null"); } Source source = new Source<>(liveData, observer); Source putIfAbsent = this.mSources.putIfAbsent(liveData, source); if (putIfAbsent != null && putIfAbsent.mObserver != observer) { throw new IllegalArgumentException("This source was already added with the different observer"); } if (putIfAbsent == null && hasActiveObservers()) { source.plug(); } } public void removeSource(LiveData liveData) { Source remove = this.mSources.remove(liveData); if (remove != null) { remove.unplug(); } } /* JADX INFO: Access modifiers changed from: protected */ @Override // androidx.lifecycle.LiveData public void onActive() { Iterator, Source>> it = this.mSources.iterator(); while (it.hasNext()) { it.next().getValue().plug(); } } /* JADX INFO: Access modifiers changed from: protected */ @Override // androidx.lifecycle.LiveData public void onInactive() { Iterator, Source>> it = this.mSources.iterator(); while (it.hasNext()) { it.next().getValue().unplug(); } } /* loaded from: classes2.dex */ private static class Source implements Observer { final LiveData mLiveData; final Observer mObserver; int mVersion = -1; Source(LiveData liveData, Observer observer) { this.mLiveData = liveData; this.mObserver = observer; } void plug() { this.mLiveData.observeForever(this); } void unplug() { this.mLiveData.removeObserver(this); } @Override // androidx.lifecycle.Observer public void onChanged(V v) { if (this.mVersion != this.mLiveData.getVersion()) { this.mVersion = this.mLiveData.getVersion(); this.mObserver.onChanged(v); } } } }