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

com.github.tonivade.purefun.effect.Task Maven / Gradle / Ivy

/*
 * Copyright (c) 2018-2019, Antonio Gabriel Muñoz Conejo 
 * Distributed under the terms of the MIT License
 */
package com.github.tonivade.purefun.effect;

import static com.github.tonivade.purefun.Function1.identity;
import static com.github.tonivade.purefun.Nothing.nothing;
import static java.util.Objects.requireNonNull;

import java.time.Duration;
import java.util.concurrent.Executor;

import com.github.tonivade.purefun.CheckedRunnable;
import com.github.tonivade.purefun.Consumer1;
import com.github.tonivade.purefun.Function1;
import com.github.tonivade.purefun.Function2;
import com.github.tonivade.purefun.Higher1;
import com.github.tonivade.purefun.HigherKind;
import com.github.tonivade.purefun.Kind;
import com.github.tonivade.purefun.Nothing;
import com.github.tonivade.purefun.Producer;
import com.github.tonivade.purefun.Recoverable;
import com.github.tonivade.purefun.Unit;
import com.github.tonivade.purefun.concurrent.Future;
import com.github.tonivade.purefun.type.Either;
import com.github.tonivade.purefun.type.Try;
import com.github.tonivade.purefun.typeclasses.MonadDefer;

@HigherKind
public final class Task implements Recoverable {

  private final ZIO value;

  Task(ZIO value) {
    this.value = requireNonNull(value);
  }

  @SuppressWarnings("unchecked")
  public  ZIO toZIO() {
    return (ZIO) value;
  }

  public EIO toEIO() {
    return new EIO<>(value);
  }

  public Try safeRunSync() {
    return absorb(value.provide(nothing()));
  }

  public Future> toFuture() {
    return value.toFuture(nothing()).map(this::absorb);
  }

  public void async(Executor executor, Consumer1> callback) {
    value.provideAsync(executor, nothing(), result -> callback.accept(flatAbsorb(result)));
  }

  public void async(Consumer1> callback) {
    async(Future.DEFAULT_EXECUTOR, callback);
  }

  public  Higher1 foldMap(MonadDefer monad) {
    return monad.flatMap(value.foldMap(nothing(), monad), monad::fromEither);
  }

  public  Task map(Function1 map) {
    return new Task<>(value.map(map));
  }

  public  Task flatMap(Function1> map) {
    return new Task<>(value.flatMap(value -> map.apply(value).value));
  }

  public  Task andThen(Task next) {
    return new Task<>(value.andThen(next.value));
  }

  public  Task foldM(Function1> mapError, Function1> map) {
    return new Task<>(value.foldM(error -> mapError.apply(error).value, value -> map.apply(value).value));
  }

  public  UIO fold(Function1 mapError, Function1 map) {
    return new UIO<>(value.fold(mapError, map));
  }

  public UIO recover(Function1 mapError) {
    return new UIO<>(value.recover(mapError));
  }

  public Task orElse(Producer> other) {
    return new Task<>(value.orElse(() -> other.get().value));
  }

  public Task repeat() {
    return repeat(1);
  }

  public Task repeat(int times) {
    return repeat(unit(), times);
  }

  public Task repeat(Duration delay) {
    return repeat(delay, 1);
  }

  public Task repeat(Duration delay, int times) {
    return repeat(sleep(delay), times);
  }

  public Task retry() {
    return retry(1);
  }

  public Task retry(int maxRetries) {
    return retry(unit(), maxRetries);
  }

  public Task retry(Duration delay) {
    return retry(delay, 1);
  }

  public Task retry(Duration delay, int maxRetries) {
    return retry(sleep(delay), maxRetries);
  }

  private Task repeat(Task pause, int times) {
    return foldM(Task::raiseError, value -> {
      if (times > 0)
        return pause.andThen(repeat(pause, times - 1));
      else
        return pure(value);
    });
  }

  private Task retry(Task pause, int maxRetries) {
    return foldM(error -> {
      if (maxRetries > 0)
        return pause.andThen(retry(pause.repeat(), maxRetries - 1));
      else
        return raiseError(error);
    }, Task::pure);
  }

  public static  Task map2(Task za, Task zb, Function2 mapper) {
    return new Task<>(ZIO.map2(za.value, zb.value, mapper));
  }

  public static  Task absorb(Task> value) {
    return new Task<>(ZIO.absorb(value.value));
  }

  public static  Function1> lift(Function1 function) {
    return ZIO.lift(function).andThen(Task::new);
  }

  public static  Task fromEither(Producer> task) {
    return new Task<>(ZIO.fromEither(task));
  }

  public static Task sleep(Duration delay) {
    return exec(() -> Thread.sleep(delay.toMillis()));
  }

  public static Task exec(CheckedRunnable task) {
    return new Task<>(ZIO.exec(task));
  }

  public static  Task pure(A value) {
    return new Task<>(ZIO.pure(value));
  }

  public static  Task defer(Producer> lazy) {
    return new Task<>(ZIO.defer(() -> lazy.get().value));
  }

  public static  Task task(Producer task) {
    return new Task<>(ZIO.task(task));
  }

  public static  Task raiseError(Throwable error) {
    return new Task<>(ZIO.raiseError(error));
  }

  public static  Task bracket(Task acquire, Function1> use) {
    return new Task<>(ZIO.bracket(acquire.value, resource -> use.apply(resource).value));
  }

  public static  Task bracket(Task acquire, Function1> use, Consumer1 release) {
    return new Task<>(ZIO.bracket(acquire.value, resource -> use.apply(resource).value, release));
  }

  public static Task unit() {
    return new Task<>(ZIO.unit());
  }

  private Try flatAbsorb(Try> result) {
    return result.map(this::absorb).flatMap(identity());
  }

  private Try absorb(Either either) {
    return either.fold(Try::failure, Try::success);
  }
}