All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.firebase.tasks.TaskImpl Maven / Gradle / Ivy

Go to download

This is the official Firebase Admin Java SDK. Build extraordinary native JVM apps in minutes with Firebase. The Firebase platform can power your app’s backend, user authentication, static hosting, and more.

There is a newer version: 9.3.0
Show newest version
/*
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.firebase.tasks;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.Preconditions;
import com.google.firebase.internal.GuardedBy;
import com.google.firebase.internal.NonNull;
import com.google.firebase.internal.Nullable;

import java.util.concurrent.Executor;

/** 
 * Default implementation of {@link Task}.
 */
final class TaskImpl extends Task {

  private final Object lock = new Object();
  private final TaskCompletionListenerQueue listenerQueue = new TaskCompletionListenerQueue<>();

  @GuardedBy("lock")
  private boolean complete;

  @GuardedBy("lock")
  private T result;

  @GuardedBy("lock")
  private Exception exception;

  @Override
  public boolean isComplete() {
    synchronized (lock) {
      return complete;
    }
  }

  @Override
  public boolean isSuccessful() {
    synchronized (lock) {
      return complete && exception == null;
    }
  }

  @Override
  public T getResult() {
    synchronized (lock) {
      checkCompleteLocked();

      if (exception != null) {
        throw new RuntimeExecutionException(exception);
      }

      return result;
    }
  }

  @Override
  public  T getResult(@NonNull Class exceptionType) throws X {
    synchronized (lock) {
      checkCompleteLocked();

      if (exceptionType.isInstance(exception)) {
        throw exceptionType.cast(exception);
      }
      if (exception != null) {
        throw new RuntimeExecutionException(exception);
      }

      return result;
    }
  }

  public void setResult(T result) {
    synchronized (lock) {
      checkNotCompleteLocked();
      complete = true;
      this.result = result;
    }
    // Intentionally outside the lock.
    listenerQueue.flush(this);
  }

  @Nullable
  @Override
  public Exception getException() {
    synchronized (lock) {
      return exception;
    }
  }

  @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
  public void setException(@NonNull Exception e) {
    checkNotNull(e, "Exception must not be null");
    synchronized (lock) {
      checkNotCompleteLocked();
      complete = true;
      exception = e;
    }
    // Intentionally outside the lock.
    listenerQueue.flush(this);
  }

  @NonNull
  @Override
  public Task addOnSuccessListener(@NonNull OnSuccessListener listener) {
    return addOnSuccessListener(TaskExecutors.DEFAULT_THREAD_POOL, listener);
  }

  @NonNull
  @Override
  public Task addOnSuccessListener(
      @NonNull Executor executor, @NonNull OnSuccessListener listener) {
    listenerQueue.add(new OnSuccessCompletionListener<>(executor, listener));
    flushIfComplete();
    return this;
  }

  @NonNull
  @Override
  public Task addOnFailureListener(@NonNull OnFailureListener listener) {
    return addOnFailureListener(TaskExecutors.DEFAULT_THREAD_POOL, listener);
  }

  @NonNull
  @Override
  public Task addOnFailureListener(
      @NonNull Executor executor, @NonNull OnFailureListener listener) {
    listenerQueue.add(new OnFailureCompletionListener(executor, listener));
    flushIfComplete();
    return this;
  }

  @NonNull
  @Override
  public Task addOnCompleteListener(@NonNull OnCompleteListener listener) {
    return addOnCompleteListener(TaskExecutors.DEFAULT_THREAD_POOL, listener);
  }

  @NonNull
  @Override
  public Task addOnCompleteListener(
      @NonNull Executor executor, @NonNull OnCompleteListener listener) {
    listenerQueue.add(new OnCompleteCompletionListener<>(executor, listener));
    flushIfComplete();
    return this;
  }

  @NonNull
  @Override
  public  Task continueWith(@NonNull Continuation continuation) {
    return continueWith(TaskExecutors.DEFAULT_THREAD_POOL, continuation);
  }

  @NonNull
  @Override
  public  Task continueWith(
      @NonNull Executor executor, @NonNull Continuation continuation) {
    TaskImpl continuationTask = new TaskImpl<>();
    listenerQueue.add(
        new ContinueWithCompletionListener<>(executor, continuation, continuationTask));
    flushIfComplete();
    return continuationTask;
  }

  @NonNull
  @Override
  public  Task continueWithTask(@NonNull Continuation> continuation) {
    return continueWithTask(TaskExecutors.DEFAULT_THREAD_POOL, continuation);
  }

  @NonNull
  @Override
  public  Task continueWithTask(
      @NonNull Executor executor, @NonNull Continuation> continuation) {
    TaskImpl continuationTask = new TaskImpl<>();
    listenerQueue.add(
        new ContinueWithTaskCompletionListener<>(executor, continuation, continuationTask));
    flushIfComplete();
    return continuationTask;
  }

  public boolean trySetResult(T result) {
    synchronized (lock) {
      if (complete) {
        return false;
      }
      complete = true;
      this.result = result;
    }
    // Intentionally outside the lock.
    listenerQueue.flush(this);
    return true;
  }

  @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
  public boolean trySetException(@NonNull Exception e) {
    checkNotNull(e, "Exception must not be null");
    synchronized (lock) {
      if (complete) {
        return false;
      }
      complete = true;
      exception = e;
    }
    // Intentionally outside the lock.
    listenerQueue.flush(this);
    return true;
  }

  @GuardedBy("lock")
  private void checkCompleteLocked() {
    Preconditions.checkState(complete, "Task is not yet complete");
  }

  @GuardedBy("lock")
  private void checkNotCompleteLocked() {
    Preconditions.checkState(!complete, "Task is already complete");
  }

  private void flushIfComplete() {
    synchronized (lock) {
      if (!complete) {
        return;
      }
    }
    // Intentionally outside the lock.
    listenerQueue.flush(this);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy