Java generics with Function.apply

Minato Namikaze :

I am playing with Java utility functions. I have the following code:

public class Checker<T>{

  private T value;
  private Function<T, T> callback;

  private Checker(T value) {
    this.value = value;
  }

  public static Checker when(String o) {
    return new Checker<String>(o);
  }

  public static Checker when(int o) {
    return new Checker<Integer>(o);
  }

  public Checker then(Function<T, T> callback) {
    this.callback = callback;
    return this;
  }

  public void execute() {
    if (this.value instanceof String) {
      this.callback.apply("123");
    }
    if (this.value instanceof Integer) {
      this.callback.apply(123);
    }
  }

  Checker.when("123").then(str -> {
    return "";
  }).execute();

  Checker.when(123).then(str -> {
    return "";
  }).execute();

Now here I am getting an error for this.callback.apply("123") as it requires T and cannot cast it to String.

Is it possible to have generic return types for Function<T,T>? I can send T, but then its received as an Object in my lambda, but I want as String or Integer.

Eran :

I made some changes in your Checker class which make sense to me. I eliminated all the raw types, and I used the value member in execute. I added a return type of execute, in order to be able to print its result.

class Checker<T>{

  private T value;
  private Function<T, T> callback;

  private Checker(T value) {
    this.value = value;
  }

  public static Checker<String> when(String o) {
    return new Checker<>(o);
  }

  public static Checker<Integer> when(int o) {
    return new Checker<>(o);
  }

  public Checker<T> then(Function<T, T> callback) {
    this.callback = callback;
    return this;
  }

  public T execute() {
    return this.callback.apply(value);
  }

  public static void main (String[] args) {
    Checker.when("123").then(str -> {
      return "." + str + ".";
    }).execute();

    Checker.when(123).then(i -> {
      return i + 100;
    }).execute();
  }
}

Now when you check your class with:

System.out.println (Checker.when("123").then(str -> "." + str + ".").execute());
System.out.println (Checker.when(123).then(i -> i + 100).execute());

You get:

.123.
223

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=475554&siteId=1