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

org.jetbrains.concurrency.Promise Maven / Gradle / Ivy

Go to download

A packaging of the IntelliJ Community Edition core-api library. This is release number 1 of trunk branch 142.

The newest version!
/*
 * Copyright 2000-2015 JetBrains s.r.o.
 *
 * 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 org.jetbrains.concurrency;

import com.intellij.openapi.util.ActionCallback;
import com.intellij.openapi.util.AsyncResult;
import com.intellij.util.Consumer;
import com.intellij.util.Function;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;

public abstract class Promise {
  public static final Promise DONE = new DonePromise(null);
  public static final Promise REJECTED = new RejectedPromise(createError("rejected"));

  @NotNull
  public static RuntimeException createError(@NotNull String error) {
    return new MessageError(error);
  }

  public enum State {
    PENDING, FULFILLED, REJECTED
  }

  @NotNull
  public static  Promise resolve(T result) {
    if (result == null) {
      //noinspection unchecked
      return (Promise)DONE;
    }
    else {
      return new DonePromise(result);
    }
  }

  @NotNull
  public static  Promise reject(@NotNull String error) {
    return reject(createError(error));
  }

  @NotNull
  public static  Promise reject(@Nullable Throwable error) {
    if (error == null) {
      //noinspection unchecked
      return (Promise)REJECTED;
    }
    else {
      return new RejectedPromise(error);
    }
  }

  @NotNull
  public static Promise all(@NotNull Collection> promises) {
    return all(promises, null);
  }

  @NotNull
  public static  Promise all(@NotNull Collection> promises, @Nullable T totalResult) {
    if (promises.isEmpty()) {
      //noinspection unchecked
      return (Promise)DONE;
    }

    final AsyncPromise totalPromise = new AsyncPromise();
    Consumer done = new CountDownConsumer(promises.size(), totalPromise, totalResult);
    Consumer rejected = new Consumer() {
      @Override
      public void consume(Throwable error) {
        if (totalPromise.state == AsyncPromise.State.PENDING) {
          totalPromise.setError(error);
        }
      }
    };

    for (Promise promise : promises) {
      //noinspection unchecked
      promise.done(done);
      promise.rejected(rejected);
    }
    return totalPromise;
  }

  @NotNull
  public static Promise wrapAsVoid(@NotNull ActionCallback asyncResult) {
    final AsyncPromise promise = new AsyncPromise();
    asyncResult.doWhenDone(new Runnable() {
      @Override
      public void run() {
        promise.setResult(null);
      }
    }).doWhenRejected(new Consumer() {
      @Override
      public void consume(String error) {
        promise.setError(createError(error));
      }
    });
    return promise;
  }

  @NotNull
  public static  Promise wrap(@NotNull AsyncResult asyncResult) {
    final AsyncPromise promise = new AsyncPromise();
    asyncResult.doWhenDone(new Consumer() {
      @Override
      public void consume(T result) {
        promise.setResult(result);
      }
    }).doWhenRejected(new Consumer() {
      @Override
      public void consume(String error) {
        promise.setError(createError(error));
      }
    });
    return promise;
  }

  @NotNull
  public abstract Promise done(@NotNull Consumer done);

  @NotNull
  public abstract Promise processed(@NotNull AsyncPromise fulfilled);

  @NotNull
  public abstract Promise rejected(@NotNull Consumer rejected);

  public abstract Promise processed(@NotNull Consumer processed);

  @NotNull
  public abstract  Promise then(@NotNull Function done);

  @NotNull
  public abstract  Promise then(@NotNull AsyncFunction done);

  @NotNull
  public abstract State getState();

  @SuppressWarnings("ExceptionClassNameDoesntEndWithException")
  public static class MessageError extends RuntimeException {
    public MessageError(@NotNull String error) {
      super(error);
    }

    @NotNull
    @Override
    public final synchronized Throwable fillInStackTrace() {
      return this;
    }
  }

  abstract void notify(@NotNull AsyncPromise child);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy