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

com.github.tonivade.purefun.Producer 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;

import com.github.tonivade.purefun.type.Either;
import com.github.tonivade.purefun.type.Option;
import com.github.tonivade.purefun.type.Try;

@FunctionalInterface
public interface Producer {

  T get();

  default  Function1 asFunction() {
    return value -> get();
  }

  default  Producer andThen(Function1 after) {
    return () -> after.apply(get());
  }

  default Producer> liftOption() {
    return () -> Option.of(this::get);
  }

  default Producer> liftTry() {
    return () -> Try.of(this::get);
  }

  default Producer> liftEither() {
    return liftTry().andThen(Try::toEither);
  }

  default Producer memoized() {
    return new MemoizedProducer<>(this);
  }

  static  Producer cons(T value) {
    return () -> value;
  }

  static  Producer of(Producer reference) {
    return reference;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy