Skip to content

z_ext_Common.StaticProducer

yanlu edited this page Jun 1, 2016 · 1 revision

StaticProducer that a Simple Supplier,Function,Merger, return TTo.

The Reference:*

  //Functions
  public static <F, T> Function<F, T> staticFunction(@NonNull final T object) {
    return new StaticProducer<>(object);
  }

  //Mergers
  public static <TFirst, TSecond, TTo> Merger<TFirst, TSecond, TTo> staticMerger(
      @NonNull final TTo value) {
    return new StaticProducer<>(value);
  }

  //Suppliers
  @NonNull
  public static <T> Supplier<T> staticSupplier(@NonNull final T object) {
    return new StaticProducer<>(object);
  }

StaticProducer

  static final class StaticProducer<TFirst, TSecond, TTo>
      implements Supplier<TTo>, Function<TFirst, TTo>, Merger<TFirst, TSecond, TTo> {
    @NonNull
    private final TTo staticValue;

    StaticProducer(@NonNull final TTo staticValue) {
      this.staticValue = checkNotNull(staticValue);
    }

    @NonNull
    @Override
    public TTo apply(@NonNull final TFirst input) {
      return staticValue;
    }

    @NonNull
    @Override
    public TTo merge(@NonNull final TFirst o, @NonNull final TSecond o2) {
      return staticValue;
    }

    @NonNull
    @Override
    public TTo get() {
      return staticValue;
    }
  }