com.github.tonivade.purefun.Producer Maven / Gradle / Ivy
/*
* Copyright (c) 2018-2020, Antonio Gabriel Muñoz Conejo
* Distributed under the terms of the MIT License
*/
package com.github.tonivade.purefun;
import com.github.tonivade.purefun.type.Either;
import com.github.tonivade.purefun.type.Option;
import com.github.tonivade.purefun.type.Try;
/**
* This interface represents a function without any parameter. Similar to {@link java.util.function.Supplier}
* but with additional functionality like the ability to memoize the result.
* @param the returned type
*/
@HigherKind
@FunctionalInterface
public interface Producer extends Recoverable {
default T get() {
try {
return run();
} catch (Throwable t) {
return sneakyThrow(t);
}
}
T run() throws Throwable;
default Function1 asFunction() {
return value -> run();
}
default Producer map(Function1 after) {
return () -> after.apply(get());
}
default Producer flatMap(Function1> after) {
return () -> after.apply(get()).get();
}
default Producer © 2015 - 2025 Weber Informatics LLC | Privacy Policy