package o; import java.lang.reflect.InvocationTargetException; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.CopyOnWriteArraySet; /* renamed from: o.fNn, reason: case insensitive filesystem */ /* loaded from: classes5.dex */ public class C13001fNn { public static final String DEFAULT_IDENTIFIER = "default"; private final fNA enforcer; private final ThreadLocal> eventsToDispatch; private final ConcurrentMap, Set>> flattenHierarchyCache; private final InterfaceC13008fNu handlerFinder; private final ConcurrentMap, Set> handlersByType; private final String identifier; private final ThreadLocal isDispatching; private final ConcurrentMap, C13007fNt> producersByType; public C13001fNn() { this(DEFAULT_IDENTIFIER); } public C13001fNn(String str) { this(fNA.a, str); } public C13001fNn(fNA fna) { this(fna, DEFAULT_IDENTIFIER); } public C13001fNn(fNA fna, String str) { this(fna, str, InterfaceC13008fNu.a); } C13001fNn(fNA fna, String str, InterfaceC13008fNu interfaceC13008fNu) { this.handlersByType = new ConcurrentHashMap(); this.producersByType = new ConcurrentHashMap(); this.eventsToDispatch = new ThreadLocal>(this) { // from class: o.fNn.4 private C13001fNn a; { this.a = this; } @Override // java.lang.ThreadLocal protected final /* synthetic */ ConcurrentLinkedQueue initialValue() { return new ConcurrentLinkedQueue<>(); } }; this.isDispatching = new ThreadLocal(this) { // from class: o.fNn.1 private C13001fNn b; { this.b = this; } @Override // java.lang.ThreadLocal protected final /* bridge */ /* synthetic */ Boolean initialValue() { return Boolean.FALSE; } }; this.flattenHierarchyCache = new ConcurrentHashMap(); this.enforcer = fna; this.identifier = str; this.handlerFinder = interfaceC13008fNu; } public String toString() { StringBuilder sb = new StringBuilder("[Bus \""); sb.append(this.identifier); sb.append("\"]"); return sb.toString(); } public void register(Object obj) { Set putIfAbsent; if (obj == null) { throw new NullPointerException("Object to register must not be null."); } this.enforcer.b(this); Map, C13007fNt> b = this.handlerFinder.b(obj); for (Class cls : b.keySet()) { C13007fNt c13007fNt = b.get(cls); C13007fNt putIfAbsent2 = this.producersByType.putIfAbsent(cls, c13007fNt); if (putIfAbsent2 != null) { StringBuilder sb = new StringBuilder("Producer method for type "); sb.append(cls); sb.append(" found on type "); sb.append(c13007fNt.d.getClass()); sb.append(", but already registered by type "); sb.append(putIfAbsent2.d.getClass()); sb.append("."); throw new IllegalArgumentException(sb.toString()); } Set set = this.handlersByType.get(cls); if (set != null && !set.isEmpty()) { Iterator it = set.iterator(); while (it.hasNext()) { dispatchProducerResultToHandler(it.next(), c13007fNt); } } } Map, Set> c = this.handlerFinder.c(obj); for (Class cls2 : c.keySet()) { Set set2 = this.handlersByType.get(cls2); if (set2 == null && (putIfAbsent = this.handlersByType.putIfAbsent(cls2, (set2 = new CopyOnWriteArraySet<>()))) != null) { set2 = putIfAbsent; } if (!set2.addAll(c.get(cls2))) { throw new IllegalArgumentException("Object already registered."); } } for (Map.Entry, Set> entry : c.entrySet()) { C13007fNt c13007fNt2 = this.producersByType.get(entry.getKey()); if (c13007fNt2 != null && c13007fNt2.b) { for (C13005fNr c13005fNr : entry.getValue()) { if (c13007fNt2.b) { if (c13005fNr.a) { dispatchProducerResultToHandler(c13005fNr, c13007fNt2); } } } } } } private void dispatchProducerResultToHandler(C13005fNr c13005fNr, C13007fNt c13007fNt) { Object obj; try { obj = c13007fNt.b(); } catch (InvocationTargetException e) { StringBuilder sb = new StringBuilder("Producer "); sb.append(c13007fNt); sb.append(" threw an exception."); throwRuntimeException(sb.toString(), e); obj = null; } if (obj == null) { return; } dispatch(obj, c13005fNr); } public void unregister(Object obj) { if (obj == null) { throw new NullPointerException("Object to unregister must not be null."); } this.enforcer.b(this); for (Map.Entry, C13007fNt> entry : this.handlerFinder.b(obj).entrySet()) { Class key = entry.getKey(); C13007fNt producerForEventType = getProducerForEventType(key); C13007fNt value = entry.getValue(); if (value == null || !value.equals(producerForEventType)) { StringBuilder sb = new StringBuilder("Missing event producer for an annotated method. Is "); sb.append(obj.getClass()); sb.append(" registered?"); throw new IllegalArgumentException(sb.toString()); } this.producersByType.remove(key).b = false; } for (Map.Entry, Set> entry2 : this.handlerFinder.c(obj).entrySet()) { Set handlersForEventType = getHandlersForEventType(entry2.getKey()); Set value2 = entry2.getValue(); if (handlersForEventType == null || !handlersForEventType.containsAll(value2)) { StringBuilder sb2 = new StringBuilder("Missing event handler for an annotated method. Is "); sb2.append(obj.getClass()); sb2.append(" registered?"); throw new IllegalArgumentException(sb2.toString()); } for (C13005fNr c13005fNr : handlersForEventType) { if (value2.contains(c13005fNr)) { c13005fNr.a = false; } } handlersForEventType.removeAll(value2); } } public void post(Object obj) { if (obj == null) { throw new NullPointerException("Event to post must not be null."); } this.enforcer.b(this); Iterator> it = flattenHierarchy(obj.getClass()).iterator(); boolean z = false; while (it.hasNext()) { Set handlersForEventType = getHandlersForEventType(it.next()); if (handlersForEventType != null && !handlersForEventType.isEmpty()) { Iterator it2 = handlersForEventType.iterator(); while (it2.hasNext()) { enqueueEvent(obj, it2.next()); } z = true; } } if (!z && !(obj instanceof C12999fNl)) { post(new C12999fNl(this, obj)); } dispatchQueuedEvents(); } protected void enqueueEvent(Object obj, C13005fNr c13005fNr) { this.eventsToDispatch.get().offer(new IeS(obj, c13005fNr)); } protected void dispatchQueuedEvents() { if (this.isDispatching.get().booleanValue()) { return; } this.isDispatching.set(Boolean.TRUE); while (true) { try { IeS poll = this.eventsToDispatch.get().poll(); if (poll == null) { return; } if (poll.c.a) { dispatch(poll.e, poll.c); } } finally { this.isDispatching.set(Boolean.FALSE); } } } protected void dispatch(Object obj, C13005fNr c13005fNr) { try { if (!c13005fNr.a) { StringBuilder sb = new StringBuilder(); sb.append(c13005fNr.toString()); sb.append(" has been invalidated and can no longer handle events."); throw new IllegalStateException(sb.toString()); } try { try { c13005fNr.e.invoke(c13005fNr.c, obj); } catch (InvocationTargetException e) { if (e.getCause() instanceof Error) { throw ((Error) e.getCause()); } throw e; } } catch (IllegalAccessException e2) { throw new AssertionError(e2); } } catch (InvocationTargetException e3) { StringBuilder sb2 = new StringBuilder("Could not dispatch event: "); sb2.append(obj.getClass()); sb2.append(" to handler "); sb2.append(c13005fNr); throwRuntimeException(sb2.toString(), e3); } } C13007fNt getProducerForEventType(Class cls) { return this.producersByType.get(cls); } Set getHandlersForEventType(Class cls) { return this.handlersByType.get(cls); } Set> flattenHierarchy(Class cls) { Set> set = this.flattenHierarchyCache.get(cls); if (set != null) { return set; } Set> classesFor = getClassesFor(cls); Set> putIfAbsent = this.flattenHierarchyCache.putIfAbsent(cls, classesFor); return putIfAbsent != null ? putIfAbsent : classesFor; } private Set> getClassesFor(Class cls) { LinkedList linkedList = new LinkedList(); HashSet hashSet = new HashSet(); linkedList.add(cls); while (!linkedList.isEmpty()) { Class cls2 = (Class) linkedList.remove(0); hashSet.add(cls2); Class superclass = cls2.getSuperclass(); if (superclass != null) { linkedList.add(superclass); } } return hashSet; } private static void throwRuntimeException(String str, InvocationTargetException invocationTargetException) { Throwable cause = invocationTargetException.getCause(); if (cause != null) { StringBuilder sb = new StringBuilder(); sb.append(str); sb.append(": "); sb.append(cause.getMessage()); throw new RuntimeException(sb.toString(), cause); } StringBuilder sb2 = new StringBuilder(); sb2.append(str); sb2.append(": "); sb2.append(invocationTargetException.getMessage()); throw new RuntimeException(sb2.toString(), invocationTargetException); } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: o.fNn$IeS */ /* loaded from: classes5.dex */ public static class IeS { final C13005fNr c; final Object e; public IeS(Object obj, C13005fNr c13005fNr) { this.e = obj; this.c = c13005fNr; } } }