package com.facebook.bolts; import java.io.Closeable; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.concurrent.CancellationException; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import kotlin.Unit; import kotlin.jvm.internal.Intrinsics; import kotlin.jvm.internal.StringCompanionObject; /* loaded from: classes2.dex */ public final class CancellationTokenSource implements Closeable { private boolean cancellationRequested; private boolean closed; private ScheduledFuture scheduledCancellation; private final Object lock = new Object(); private final List registrations = new ArrayList(); private final ScheduledExecutorService executor = BoltsExecutors.Companion.scheduled$facebook_bolts_release(); public final boolean isCancellationRequested() { boolean z; synchronized (this.lock) { throwIfClosed(); z = this.cancellationRequested; } return z; } public final CancellationToken getToken() { CancellationToken cancellationToken; synchronized (this.lock) { throwIfClosed(); cancellationToken = new CancellationToken(this); } return cancellationToken; } public final void cancel() { synchronized (this.lock) { throwIfClosed(); if (this.cancellationRequested) { return; } cancelScheduledCancellation(); this.cancellationRequested = true; ArrayList arrayList = new ArrayList(this.registrations); Unit unit = Unit.INSTANCE; notifyListeners(arrayList); } } public final void cancelAfter(long j) { cancelAfter(j, TimeUnit.MILLISECONDS); } private final void cancelAfter(long j, TimeUnit timeUnit) { if (!(j >= -1)) { throw new IllegalArgumentException("Delay must be >= -1".toString()); } if (j == 0) { cancel(); return; } synchronized (this.lock) { try { if (this.cancellationRequested) { return; } cancelScheduledCancellation(); if (j != -1) { this.scheduledCancellation = this.executor.schedule(new Runnable() { // from class: com.facebook.bolts.CancellationTokenSource$$ExternalSyntheticLambda0 @Override // java.lang.Runnable public final void run() { CancellationTokenSource.m525cancelAfter$lambda6$lambda5(CancellationTokenSource.this); } }, j, timeUnit); } Unit unit = Unit.INSTANCE; } catch (Throwable th) { throw th; } } } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: cancelAfter$lambda-6$lambda-5, reason: not valid java name */ public static final void m525cancelAfter$lambda6$lambda5(CancellationTokenSource this$0) { Intrinsics.checkNotNullParameter(this$0, "this$0"); synchronized (this$0.lock) { this$0.scheduledCancellation = null; Unit unit = Unit.INSTANCE; } this$0.cancel(); } @Override // java.io.Closeable, java.lang.AutoCloseable public void close() { synchronized (this.lock) { try { if (this.closed) { return; } cancelScheduledCancellation(); Iterator it = this.registrations.iterator(); while (it.hasNext()) { it.next().close(); } this.registrations.clear(); this.closed = true; Unit unit = Unit.INSTANCE; } catch (Throwable th) { throw th; } } } public final CancellationTokenRegistration register$facebook_bolts_release(Runnable runnable) { CancellationTokenRegistration cancellationTokenRegistration; synchronized (this.lock) { try { throwIfClosed(); cancellationTokenRegistration = new CancellationTokenRegistration(this, runnable); if (this.cancellationRequested) { cancellationTokenRegistration.runAction$facebook_bolts_release(); Unit unit = Unit.INSTANCE; } else { this.registrations.add(cancellationTokenRegistration); } } catch (Throwable th) { throw th; } } return cancellationTokenRegistration; } public final void throwIfCancellationRequested$facebook_bolts_release() throws CancellationException { synchronized (this.lock) { throwIfClosed(); if (this.cancellationRequested) { throw new CancellationException(); } Unit unit = Unit.INSTANCE; } } public final void unregister$facebook_bolts_release(CancellationTokenRegistration registration) { Intrinsics.checkNotNullParameter(registration, "registration"); synchronized (this.lock) { throwIfClosed(); this.registrations.remove(registration); } } private final void notifyListeners(List list) { Iterator it = list.iterator(); while (it.hasNext()) { it.next().runAction$facebook_bolts_release(); } } public String toString() { StringCompanionObject stringCompanionObject = StringCompanionObject.INSTANCE; String format = String.format(Locale.US, "%s@%s[cancellationRequested=%s]", Arrays.copyOf(new Object[]{CancellationTokenSource.class.getName(), Integer.toHexString(hashCode()), Boolean.toString(isCancellationRequested())}, 3)); Intrinsics.checkNotNullExpressionValue(format, "java.lang.String.format(locale, format, *args)"); return format; } private final void throwIfClosed() { if (!(!this.closed)) { throw new IllegalStateException("Object already closed".toString()); } } private final void cancelScheduledCancellation() { ScheduledFuture scheduledFuture = this.scheduledCancellation; if (scheduledFuture == null) { return; } scheduledFuture.cancel(true); this.scheduledCancellation = null; } }