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

com.github.dakusui.printables.PrintableFunction Maven / Gradle / Ivy

There is a newer version: 6.0.1
Show newest version
package com.github.dakusui.printables;

import java.util.function.Function;
import java.util.function.Supplier;

import static java.util.Objects.requireNonNull;

public class PrintableFunction implements Function {
  private final Function function;
  private final Supplier                 formatter;

  public PrintableFunction(Supplier formatter, Function function) {
    this.formatter = requireNonNull(formatter);
    this.function = requireNonNull(function);
  }

  @Override
  public R apply(T t) {
    return this.function.apply(t);
  }

  public  Function compose(Function before) {
    requireNonNull(before);
    return new PrintableFunction<>(() -> String.format("%s(%s)", formatter.get(), before), this.function.compose(before));
  }

  public  Function andThen(Function after) {
    requireNonNull(after);
    return new PrintableFunction<>(() -> String.format("%s(%s)", after, formatter.get()), this.function.andThen(after));
  }

  protected Supplier getFormatter() {
    return formatter;
  }

  protected Function getFunction() {
    return this.function;
  }

  @Override
  public String toString() {
    return formatter.get();
  }

  public static class Builder {

    private final Function function;

    public Builder(Function function) {
      this.function = requireNonNull(function);
    }

    public Function describe(Supplier formatter) {
      return new PrintableFunction<>(requireNonNull(formatter), function);
    }

    public Function describe(String description) {
      return describe(() -> description);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy