com.github.tonivade.purefun.effect.EIO 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.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.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 EIO {
private final ZIO value;
EIO(ZIO value) {
this.value = requireNonNull(value);
}
@SuppressWarnings("unchecked")
public ZIO toZIO() {
return (ZIO) value;
}
public Either safeRunSync() {
return value.provide(nothing());
}
public Future> toFuture() {
return value.toFuture(nothing());
}
public void async(Executor executor, Consumer1>> callback) {
value.provideAsync(executor, nothing(), callback);
}
public void async(Consumer1>> callback) {
async(Future.DEFAULT_EXECUTOR, callback);
}
public Higher1> foldMap(MonadDefer monad) {
return value.foldMap(nothing(), monad);
}
public EIO map(Function1 map) {
return new EIO<>(value.map(map));
}
public EIO flatMap(Function1> map) {
return new EIO<>(value.flatMap(value -> map.apply(value).value));
}
public EIO swap() {
return new EIO<>(value.swap());
}
public EIO mapError(Function1 map) {
return new EIO<>(value.mapError(map));
}
public EIO flatMapError(Function1> map) {
return new EIO<>(value.flatMapError(error -> map.apply(error).value));
}
public EIO bimap(Function1 mapError, Function1 map) {
return new EIO<>(value.bimap(mapError, map));
}
public EIO andThen(EIO next) {
return new EIO<>(value.andThen(next.value));
}
public EIO foldM(Function1> mapError, Function1> map) {
return new EIO<>(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 EIO orElse(Producer> other) {
return new EIO<>(value.orElse(() -> other.get().value));
}
public EIO repeat() {
return repeat(1);
}
public EIO repeat(int times) {
return repeat(UIO.unit(), times);
}
public EIO repeat(Duration delay) {
return repeat(delay, 1);
}
public EIO repeat(Duration delay, int times) {
return repeat(UIO.sleep(delay), times);
}
public EIO retry() {
return retry(1);
}
public EIO retry(int maxRetries) {
return retry(UIO.unit(), maxRetries);
}
public EIO retry(Duration delay) {
return retry(delay, 1);
}
public EIO retry(Duration delay, int maxRetries) {
return retry(UIO.sleep(delay), maxRetries);
}
private EIO repeat(UIO pause, int times) {
return foldM(EIO::raiseError, value -> {
if (times > 0)
return pause.toEIO().andThen(repeat(pause, times - 1));
else
return pure(value);
});
}
private EIO retry(UIO pause, int maxRetries) {
return foldM(error -> {
if (maxRetries > 0)
return pause.toEIO().andThen(retry(pause.repeat(), maxRetries - 1));
else
return raiseError(error);
}, EIO::pure);
}
public static EIO map2(EIO za, EIO zb, Function2 mapper) {
return new EIO<>(ZIO.map2(za.value, zb.value, mapper));
}
public static EIO absorb(EIO> value) {
return new EIO<>(ZIO.absorb(value.value));
}
public static Function1> lift(Function1 function) {
return ZIO.lift(function).andThen(EIO::new);
}
public static EIO fromEither(Producer> task) {
return new EIO<>(ZIO.fromEither(task));
}
public static EIO exec(CheckedRunnable task) {
return new EIO<>(ZIO.exec(task));
}
public static EIO pure(A value) {
return new EIO<>(ZIO.pure(value));
}
public static EIO defer(Producer> lazy) {
return new EIO<>(ZIO.defer(() -> lazy.get().value));
}
public static EIO task(Producer task) {
return new EIO<>(ZIO.task(task));
}
public static EIO raiseError(E error) {
return new EIO<>(ZIO.raiseError(error));
}
public static EIO bracket(EIO acquire, Function1> use) {
return new EIO<>(ZIO.bracket(acquire.value, resource -> use.apply(resource).value));
}
public static EIO bracket(EIO acquire, Function1> use, Consumer1 release) {
return new EIO<>(ZIO.bracket(acquire.value, resource -> use.apply(resource).value, release));
}
public static EIO unit() {
return new EIO<>(ZIO.unit());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy