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

xdean.fluent.Fluent Maven / Gradle / Ivy

package xdean.fluent;

import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;

public interface Fluent> extends Self {

  default S let(Consumer action) {
    action.accept(self());
    return self();
  }

  default S letIf(Predicate test, Consumer action) {
    if (test.test(self())) {
      return let(action);
    } else {
      return self();
    }
  }

  default  T map(Function transformer) {
    return transformer.apply(self());
  }

  default S mapIf(Predicate test, UnaryOperator transformer) {
    if (test.test(self())) {
      return map(transformer);
    } else {
      return self();
    }
  }

  default  T mapIfOrElse(Predicate test, Function transformer, Function elseTransformer) {
    if (test.test(self())) {
      return map(transformer);
    } else {
      return map(elseTransformer);
    }
  }

  default Optional filter(Predicate p) {
    if (p.test(self())) {
      return Optional.of(self());
    } else {
      return Optional.empty();
    }
  }

  default Optional toOptional() {
    return Optional.of(self());
  }

  default Stream toStream() {
    return Stream.of(self());
  }
}