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

swim.concurrent.Conts Maven / Gradle / Ivy

There is a newer version: 3.10.0
Show newest version
// Copyright 2015-2019 SWIM.AI 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 swim.concurrent;

/**
 * Factory functions for {@link Cont}inuation combinators.
 */
public final class Conts {
  private Conts() {
  }

  private static Cont ignore;

  /**
   * Returns a {@code Runnable} that, when executed, invokes the given {@code
   * cont}inuation with the provided constant {@code value}.
   */
  public static  Runnable async(Cont cont, T value) {
    return new ConstantCont(cont, value);
  }

  /**
   * Returns a {@code Cont}inuation that, when completed successfully,
   * completes the given {@code cont}inuation with the provided constant {@code
   * value}; and when failed with an error, fails the given {@code cont}inuation
   * with the error.
   */
  public static  Cont constant(Cont cont, T value) {
    return new ConstantCont(cont, value);
  }

  /**
   * Returns a {@code Cont} continuation that, when completed successfully,
   * does nothing; and when failed with an exception, throws the exception.
   */
  @SuppressWarnings("unchecked")
  public static  Cont ignore() {
    if (ignore == null) {
      ignore = new IgnoreCont();
    }
    return (Cont) ignore;
  }

  /**
   * Returns {@code true} if {@code throwable} is a recoverable exception;
   * returns {@code false} if {@code throwable} cannot be recovered from.
   */
  public static boolean isNonFatal(Throwable throwable) {
    return !(throwable instanceof InterruptedException
          || throwable instanceof LinkageError
          || throwable instanceof ThreadDeath
          || throwable instanceof VirtualMachineError);
  }
}

final class ConstantCont implements Cont, Runnable {
  private final Cont cont;
  private final T value;

  ConstantCont(Cont cont, T value) {
    this.cont = cont;
    this.value = value;
  }

  @Override
  public void run() {
    try {
      this.cont.bind(this.value);
    } catch (Throwable error) {
      this.cont.trap(error);
    }
  }

  @Override
  public void bind(X object) {
    try {
      this.cont.bind(this.value);
    } catch (Throwable error) {
      this.cont.trap(error);
    }
  }

  @Override
  public void trap(Throwable error) {
    this.cont.trap(error);
  }
}

final class IgnoreCont implements Cont {
  @Override
  public void bind(T value) {
    // nop
  }

  @Override
  public void trap(Throwable error) {
    if (error instanceof Error) {
      throw (Error) error;
    } else if (error instanceof RuntimeException) {
      throw (RuntimeException) error;
    } else {
      throw new ContException(error);
    }
  }
}