package com.google.firebase.components; import com.google.firebase.events.Event; import com.google.firebase.events.EventHandler; import com.google.firebase.events.Publisher; import com.google.firebase.events.Subscriber; import java.util.ArrayDeque; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Queue; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Executor; /* loaded from: classes.dex */ class EventBus implements Subscriber, Publisher { private final Executor defaultExecutor; private final Map, ConcurrentHashMap, Executor>> handlerMap = new HashMap(); private Queue> pendingEvents = new ArrayDeque(); /* JADX INFO: Access modifiers changed from: package-private */ public EventBus(Executor executor) { this.defaultExecutor = executor; } public void publish(final Event event) { Preconditions.checkNotNull(event); synchronized (this) { Queue> queue = this.pendingEvents; if (queue != null) { queue.add(event); return; } for (final Map.Entry, Executor> entry : getHandlers(event)) { entry.getValue().execute(new Runnable(entry, event) { // from class: com.google.firebase.components.EventBus$$ExternalSyntheticLambda0 public final Map.Entry f$0; public final Event f$1; @Override // java.lang.Runnable public final void run() { ((EventHandler) this.f$0.getKey()).handle(this.f$1); } { this.f$0 = entry; this.f$1 = event; } }); } } } private Set, Executor>> getHandlers(Event event) { Set, Executor>> emptySet; synchronized (this) { ConcurrentHashMap, Executor> concurrentHashMap = this.handlerMap.get(event.getType()); emptySet = concurrentHashMap == null ? Collections.emptySet() : concurrentHashMap.entrySet(); } return emptySet; } @Override // com.google.firebase.events.Subscriber public void subscribe(Class cls, Executor executor, EventHandler eventHandler) { synchronized (this) { Preconditions.checkNotNull(cls); Preconditions.checkNotNull(eventHandler); Preconditions.checkNotNull(executor); if (!this.handlerMap.containsKey(cls)) { this.handlerMap.put(cls, new ConcurrentHashMap<>()); } this.handlerMap.get(cls).put(eventHandler, executor); } } @Override // com.google.firebase.events.Subscriber public void subscribe(Class cls, EventHandler eventHandler) { subscribe(cls, this.defaultExecutor, eventHandler); } /* JADX INFO: Access modifiers changed from: package-private */ public void enablePublishingAndFlushPending() { Queue> queue; synchronized (this) { queue = this.pendingEvents; if (queue != null) { this.pendingEvents = null; } else { queue = null; } } if (queue != null) { Iterator> it = queue.iterator(); while (it.hasNext()) { publish(it.next()); } } } }