package com.google.common.eventbus; import com.google.common.base.Preconditions; import com.google.common.collect.Queues; import java.util.Iterator; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public abstract class Dispatcher { /* JADX INFO: Access modifiers changed from: package-private */ public abstract void dispatch(Object obj, Iterator it); Dispatcher() { } /* JADX INFO: Access modifiers changed from: package-private */ public static Dispatcher perThreadDispatchQueue() { return new PerThreadQueuedDispatcher(); } /* JADX INFO: Access modifiers changed from: package-private */ public static Dispatcher legacyAsync() { return new LegacyAsyncDispatcher(); } static Dispatcher immediate() { return ImmediateDispatcher.INSTANCE; } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public static final class PerThreadQueuedDispatcher extends Dispatcher { private final ThreadLocal dispatching; private final ThreadLocal> queue; private PerThreadQueuedDispatcher() { this.queue = new ThreadLocal>(this) { // from class: com.google.common.eventbus.Dispatcher.PerThreadQueuedDispatcher.1 /* JADX INFO: Access modifiers changed from: protected */ @Override // java.lang.ThreadLocal public Queue initialValue() { return Queues.newArrayDeque(); } }; this.dispatching = new ThreadLocal(this) { // from class: com.google.common.eventbus.Dispatcher.PerThreadQueuedDispatcher.2 /* JADX INFO: Access modifiers changed from: protected */ /* JADX WARN: Can't rename method to resolve collision */ @Override // java.lang.ThreadLocal public Boolean initialValue() { return Boolean.FALSE; } }; } @Override // com.google.common.eventbus.Dispatcher final void dispatch(Object obj, Iterator it) { Preconditions.checkNotNull(obj); Preconditions.checkNotNull(it); Queue queue = this.queue.get(); queue.offer(new Event(obj, it)); if (this.dispatching.get().booleanValue()) { return; } this.dispatching.set(Boolean.TRUE); while (true) { try { Event poll = queue.poll(); if (poll == null) { return; } while (poll.subscribers.hasNext()) { ((Subscriber) poll.subscribers.next()).dispatchEvent(poll.event); } } finally { this.dispatching.remove(); this.queue.remove(); } } } /* loaded from: classes2.dex */ static final class Event { private final Object event; private final Iterator subscribers; private Event(Object obj, Iterator it) { this.event = obj; this.subscribers = it; } } } /* loaded from: classes2.dex */ static final class LegacyAsyncDispatcher extends Dispatcher { private final ConcurrentLinkedQueue queue; private LegacyAsyncDispatcher() { this.queue = Queues.newConcurrentLinkedQueue(); } @Override // com.google.common.eventbus.Dispatcher final void dispatch(Object obj, Iterator it) { Preconditions.checkNotNull(obj); while (it.hasNext()) { this.queue.add(new EventWithSubscriber(obj, it.next())); } while (true) { EventWithSubscriber poll = this.queue.poll(); if (poll == null) { return; } else { poll.subscriber.dispatchEvent(poll.event); } } } /* loaded from: classes2.dex */ static final class EventWithSubscriber { private final Object event; private final Subscriber subscriber; private EventWithSubscriber(Object obj, Subscriber subscriber) { this.event = obj; this.subscriber = subscriber; } } } /* loaded from: classes2.dex */ static final class ImmediateDispatcher extends Dispatcher { private static final ImmediateDispatcher INSTANCE = new ImmediateDispatcher(); private ImmediateDispatcher() { } @Override // com.google.common.eventbus.Dispatcher final void dispatch(Object obj, Iterator it) { Preconditions.checkNotNull(obj); while (it.hasNext()) { it.next().dispatchEvent(obj); } } } }