package com.google.common.util.concurrent; import com.google.common.base.Preconditions; import com.google.common.util.concurrent.ListenerCallQueue; import com.google.common.util.concurrent.Monitor; import com.google.common.util.concurrent.Service; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; /* loaded from: classes2.dex */ public abstract class AbstractService implements Service { private static final ListenerCallQueue.Event STARTING_EVENT = new ListenerCallQueue.Event() { // from class: com.google.common.util.concurrent.AbstractService.1 @Override // com.google.common.util.concurrent.ListenerCallQueue.Event public void call(Service.Listener listener) { listener.starting(); } public String toString() { return "starting()"; } }; private static final ListenerCallQueue.Event RUNNING_EVENT = new ListenerCallQueue.Event() { // from class: com.google.common.util.concurrent.AbstractService.2 @Override // com.google.common.util.concurrent.ListenerCallQueue.Event public void call(Service.Listener listener) { listener.running(); } public String toString() { return "running()"; } }; private static final ListenerCallQueue.Event STOPPING_FROM_STARTING_EVENT = stoppingEvent(Service.State.STARTING); private static final ListenerCallQueue.Event STOPPING_FROM_RUNNING_EVENT = stoppingEvent(Service.State.RUNNING); private static final ListenerCallQueue.Event TERMINATED_FROM_NEW_EVENT = terminatedEvent(Service.State.NEW); private static final ListenerCallQueue.Event TERMINATED_FROM_STARTING_EVENT = terminatedEvent(Service.State.STARTING); private static final ListenerCallQueue.Event TERMINATED_FROM_RUNNING_EVENT = terminatedEvent(Service.State.RUNNING); private static final ListenerCallQueue.Event TERMINATED_FROM_STOPPING_EVENT = terminatedEvent(Service.State.STOPPING); private final Monitor monitor = new Monitor(); private final Monitor.Guard isStartable = new IsStartableGuard(this); private final Monitor.Guard isStoppable = new IsStoppableGuard(this); private final Monitor.Guard hasReachedRunning = new HasReachedRunningGuard(this); private final Monitor.Guard isStopped = new IsStoppedGuard(this); private final ListenerCallQueue listeners = new ListenerCallQueue<>(); private volatile StateSnapshot snapshot = new StateSnapshot(Service.State.NEW); protected void doCancelStart() { } protected abstract void doStart(); protected abstract void doStop(); private static ListenerCallQueue.Event terminatedEvent(Service.State state) { return new ListenerCallQueue.Event(state) { // from class: com.google.common.util.concurrent.AbstractService.3 final Service.State val$from; { this.val$from = state; } @Override // com.google.common.util.concurrent.ListenerCallQueue.Event public void call(Service.Listener listener) { listener.terminated(this.val$from); } public String toString() { String valueOf = String.valueOf(this.val$from); StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 21); sb.append("terminated({from = "); sb.append(valueOf); sb.append("})"); return sb.toString(); } }; } private static ListenerCallQueue.Event stoppingEvent(Service.State state) { return new ListenerCallQueue.Event(state) { // from class: com.google.common.util.concurrent.AbstractService.4 final Service.State val$from; { this.val$from = state; } @Override // com.google.common.util.concurrent.ListenerCallQueue.Event public void call(Service.Listener listener) { listener.stopping(this.val$from); } public String toString() { String valueOf = String.valueOf(this.val$from); StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 19); sb.append("stopping({from = "); sb.append(valueOf); sb.append("})"); return sb.toString(); } }; } /* loaded from: classes2.dex */ final class IsStartableGuard extends Monitor.Guard { final AbstractService this$0; /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */ IsStartableGuard(AbstractService abstractService) { super(abstractService.monitor); this.this$0 = abstractService; } @Override // com.google.common.util.concurrent.Monitor.Guard public final boolean isSatisfied() { return this.this$0.state() == Service.State.NEW; } } /* loaded from: classes2.dex */ final class IsStoppableGuard extends Monitor.Guard { final AbstractService this$0; /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */ IsStoppableGuard(AbstractService abstractService) { super(abstractService.monitor); this.this$0 = abstractService; } @Override // com.google.common.util.concurrent.Monitor.Guard public final boolean isSatisfied() { return this.this$0.state().compareTo(Service.State.RUNNING) <= 0; } } /* loaded from: classes2.dex */ final class HasReachedRunningGuard extends Monitor.Guard { final AbstractService this$0; /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */ HasReachedRunningGuard(AbstractService abstractService) { super(abstractService.monitor); this.this$0 = abstractService; } @Override // com.google.common.util.concurrent.Monitor.Guard public final boolean isSatisfied() { return this.this$0.state().compareTo(Service.State.RUNNING) >= 0; } } /* loaded from: classes2.dex */ final class IsStoppedGuard extends Monitor.Guard { final AbstractService this$0; /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */ IsStoppedGuard(AbstractService abstractService) { super(abstractService.monitor); this.this$0 = abstractService; } @Override // com.google.common.util.concurrent.Monitor.Guard public final boolean isSatisfied() { return this.this$0.state().isTerminal(); } } @Override // com.google.common.util.concurrent.Service public final Service startAsync() { if (this.monitor.enterIf(this.isStartable)) { try { this.snapshot = new StateSnapshot(Service.State.STARTING); enqueueStartingEvent(); doStart(); } finally { try { return this; } finally { } } return this; } String valueOf = String.valueOf(this); StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 33); sb.append("Service "); sb.append(valueOf); sb.append(" has already been started"); throw new IllegalStateException(sb.toString()); } @Override // com.google.common.util.concurrent.Service public final Service stopAsync() { if (this.monitor.enterIf(this.isStoppable)) { try { Service.State state = state(); switch (AnonymousClass6.$SwitchMap$com$google$common$util$concurrent$Service$State[state.ordinal()]) { case 1: this.snapshot = new StateSnapshot(Service.State.TERMINATED); enqueueTerminatedEvent(Service.State.NEW); break; case 2: this.snapshot = new StateSnapshot(Service.State.STARTING, true, null); enqueueStoppingEvent(Service.State.STARTING); doCancelStart(); break; case 3: this.snapshot = new StateSnapshot(Service.State.STOPPING); enqueueStoppingEvent(Service.State.RUNNING); doStop(); break; case 4: case 5: case 6: String valueOf = String.valueOf(state); StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 45); sb.append("isStoppable is incorrectly implemented, saw: "); sb.append(valueOf); throw new AssertionError(sb.toString()); } } finally { try { } finally { } } } return this; } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: com.google.common.util.concurrent.AbstractService$6, reason: invalid class name */ /* loaded from: classes2.dex */ public static /* synthetic */ class AnonymousClass6 { static final int[] $SwitchMap$com$google$common$util$concurrent$Service$State; static { int[] iArr = new int[Service.State.values().length]; $SwitchMap$com$google$common$util$concurrent$Service$State = iArr; try { iArr[Service.State.NEW.ordinal()] = 1; } catch (NoSuchFieldError unused) { } try { $SwitchMap$com$google$common$util$concurrent$Service$State[Service.State.STARTING.ordinal()] = 2; } catch (NoSuchFieldError unused2) { } try { $SwitchMap$com$google$common$util$concurrent$Service$State[Service.State.RUNNING.ordinal()] = 3; } catch (NoSuchFieldError unused3) { } try { $SwitchMap$com$google$common$util$concurrent$Service$State[Service.State.STOPPING.ordinal()] = 4; } catch (NoSuchFieldError unused4) { } try { $SwitchMap$com$google$common$util$concurrent$Service$State[Service.State.TERMINATED.ordinal()] = 5; } catch (NoSuchFieldError unused5) { } try { $SwitchMap$com$google$common$util$concurrent$Service$State[Service.State.FAILED.ordinal()] = 6; } catch (NoSuchFieldError unused6) { } } } @Override // com.google.common.util.concurrent.Service public final void awaitRunning() { this.monitor.enterWhenUninterruptibly(this.hasReachedRunning); try { checkCurrentState(Service.State.RUNNING); } finally { this.monitor.leave(); } } @Override // com.google.common.util.concurrent.Service public final void awaitRunning(long j, TimeUnit timeUnit) throws TimeoutException { if (this.monitor.enterWhenUninterruptibly(this.hasReachedRunning, j, timeUnit)) { try { checkCurrentState(Service.State.RUNNING); } finally { this.monitor.leave(); } } else { String valueOf = String.valueOf(this); StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 50); sb.append("Timed out waiting for "); sb.append(valueOf); sb.append(" to reach the RUNNING state."); throw new TimeoutException(sb.toString()); } } @Override // com.google.common.util.concurrent.Service public final void awaitTerminated() { this.monitor.enterWhenUninterruptibly(this.isStopped); try { checkCurrentState(Service.State.TERMINATED); } finally { this.monitor.leave(); } } @Override // com.google.common.util.concurrent.Service public final void awaitTerminated(long j, TimeUnit timeUnit) throws TimeoutException { if (this.monitor.enterWhenUninterruptibly(this.isStopped, j, timeUnit)) { try { checkCurrentState(Service.State.TERMINATED); return; } finally { this.monitor.leave(); } } String valueOf = String.valueOf(this); String valueOf2 = String.valueOf(state()); StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 65 + String.valueOf(valueOf2).length()); sb.append("Timed out waiting for "); sb.append(valueOf); sb.append(" to reach a terminal state. Current state: "); sb.append(valueOf2); throw new TimeoutException(sb.toString()); } private void checkCurrentState(Service.State state) { Service.State state2 = state(); if (state2 != state) { if (state2 == Service.State.FAILED) { String valueOf = String.valueOf(this); String valueOf2 = String.valueOf(state); StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 56 + String.valueOf(valueOf2).length()); sb.append("Expected the service "); sb.append(valueOf); sb.append(" to be "); sb.append(valueOf2); sb.append(", but the service has FAILED"); throw new IllegalStateException(sb.toString(), failureCause()); } String valueOf3 = String.valueOf(this); String valueOf4 = String.valueOf(state); String valueOf5 = String.valueOf(state2); StringBuilder sb2 = new StringBuilder(String.valueOf(valueOf3).length() + 38 + String.valueOf(valueOf4).length() + String.valueOf(valueOf5).length()); sb2.append("Expected the service "); sb2.append(valueOf3); sb2.append(" to be "); sb2.append(valueOf4); sb2.append(", but was "); sb2.append(valueOf5); throw new IllegalStateException(sb2.toString()); } } /* JADX INFO: Access modifiers changed from: protected */ public final void notifyStarted() { this.monitor.enter(); try { if (this.snapshot.state != Service.State.STARTING) { String valueOf = String.valueOf(this.snapshot.state); StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 43); sb.append("Cannot notifyStarted() when the service is "); sb.append(valueOf); IllegalStateException illegalStateException = new IllegalStateException(sb.toString()); notifyFailed(illegalStateException); throw illegalStateException; } if (this.snapshot.shutdownWhenStartupFinishes) { this.snapshot = new StateSnapshot(Service.State.STOPPING); doStop(); } else { this.snapshot = new StateSnapshot(Service.State.RUNNING); enqueueRunningEvent(); } } finally { this.monitor.leave(); dispatchListenerEvents(); } } /* JADX INFO: Access modifiers changed from: protected */ public final void notifyStopped() { this.monitor.enter(); try { Service.State state = state(); switch (AnonymousClass6.$SwitchMap$com$google$common$util$concurrent$Service$State[state.ordinal()]) { case 1: case 5: case 6: String valueOf = String.valueOf(state); StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 43); sb.append("Cannot notifyStopped() when the service is "); sb.append(valueOf); throw new IllegalStateException(sb.toString()); case 2: case 3: case 4: this.snapshot = new StateSnapshot(Service.State.TERMINATED); enqueueTerminatedEvent(state); break; } } finally { this.monitor.leave(); dispatchListenerEvents(); } } /* JADX INFO: Access modifiers changed from: protected */ public final void notifyFailed(Throwable th) { Preconditions.checkNotNull(th); this.monitor.enter(); try { Service.State state = state(); int i = AnonymousClass6.$SwitchMap$com$google$common$util$concurrent$Service$State[state.ordinal()]; if (i != 1) { if (i == 2 || i == 3 || i == 4) { this.snapshot = new StateSnapshot(Service.State.FAILED, false, th); enqueueFailedEvent(state, th); } else if (i != 5) { } return; } String valueOf = String.valueOf(state); StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 22); sb.append("Failed while in state:"); sb.append(valueOf); throw new IllegalStateException(sb.toString(), th); } finally { this.monitor.leave(); dispatchListenerEvents(); } } @Override // com.google.common.util.concurrent.Service public final boolean isRunning() { return state() == Service.State.RUNNING; } @Override // com.google.common.util.concurrent.Service public final Service.State state() { return this.snapshot.externalState(); } @Override // com.google.common.util.concurrent.Service public final Throwable failureCause() { return this.snapshot.failureCause(); } @Override // com.google.common.util.concurrent.Service public final void addListener(Service.Listener listener, Executor executor) { this.listeners.addListener(listener, executor); } public String toString() { String simpleName = getClass().getSimpleName(); String valueOf = String.valueOf(state()); StringBuilder sb = new StringBuilder(String.valueOf(simpleName).length() + 3 + String.valueOf(valueOf).length()); sb.append(simpleName); sb.append(" ["); sb.append(valueOf); sb.append("]"); return sb.toString(); } private void dispatchListenerEvents() { if (this.monitor.isOccupiedByCurrentThread()) { return; } this.listeners.dispatch(); } private void enqueueStartingEvent() { this.listeners.enqueue(STARTING_EVENT); } private void enqueueRunningEvent() { this.listeners.enqueue(RUNNING_EVENT); } private void enqueueStoppingEvent(Service.State state) { if (state == Service.State.STARTING) { this.listeners.enqueue(STOPPING_FROM_STARTING_EVENT); } else { if (state == Service.State.RUNNING) { this.listeners.enqueue(STOPPING_FROM_RUNNING_EVENT); return; } throw new AssertionError(); } } private void enqueueTerminatedEvent(Service.State state) { switch (AnonymousClass6.$SwitchMap$com$google$common$util$concurrent$Service$State[state.ordinal()]) { case 1: this.listeners.enqueue(TERMINATED_FROM_NEW_EVENT); return; case 2: this.listeners.enqueue(TERMINATED_FROM_STARTING_EVENT); return; case 3: this.listeners.enqueue(TERMINATED_FROM_RUNNING_EVENT); return; case 4: this.listeners.enqueue(TERMINATED_FROM_STOPPING_EVENT); return; case 5: case 6: throw new AssertionError(); default: return; } } private void enqueueFailedEvent(Service.State state, Throwable th) { this.listeners.enqueue(new ListenerCallQueue.Event(this, state, th) { // from class: com.google.common.util.concurrent.AbstractService.5 final Throwable val$cause; final Service.State val$from; { this.val$from = state; this.val$cause = th; } @Override // com.google.common.util.concurrent.ListenerCallQueue.Event public void call(Service.Listener listener) { listener.failed(this.val$from, this.val$cause); } public String toString() { String valueOf = String.valueOf(this.val$from); String valueOf2 = String.valueOf(this.val$cause); StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 27 + String.valueOf(valueOf2).length()); sb.append("failed({from = "); sb.append(valueOf); sb.append(", cause = "); sb.append(valueOf2); sb.append("})"); return sb.toString(); } }); } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public static final class StateSnapshot { final Throwable failure; final boolean shutdownWhenStartupFinishes; final Service.State state; StateSnapshot(Service.State state) { this(state, false, null); } StateSnapshot(Service.State state, boolean z, Throwable th) { Preconditions.checkArgument(!z || state == Service.State.STARTING, "shutdownWhenStartupFinishes can only be set if state is STARTING. Got %s instead.", state); Preconditions.checkArgument(true ^ ((state == Service.State.FAILED) ^ (th != null)), "A failure cause should be set if and only if the state is failed. Got %s and %s instead.", state, th); this.state = state; this.shutdownWhenStartupFinishes = z; this.failure = th; } final Service.State externalState() { return (this.shutdownWhenStartupFinishes && this.state == Service.State.STARTING) ? Service.State.STOPPING : this.state; } final Throwable failureCause() { Preconditions.checkState(this.state == Service.State.FAILED, "failureCause() is only valid if the service has failed, service is %s", this.state); return this.failure; } } }