package visitor;

import java.util.HashMap;
import java.util.function.Function;

public interface visitor6 {
  public interface Vehicle { /* empty */ }
  public class Car implements Vehicle { /* empty */}
  public class Moto implements Vehicle { /* empty */ }

  public class Toto{ /* empty */ }
  
  public class Visitor<U, R> {
    private final HashMap<Class<? extends U>, Function<U, ? extends R>> map = new HashMap<>();
    
    public <T extends U> Visitor<U, R> when(Class<? extends T> type, Function<? super T, ? extends R> fun) {
      map.put(type, fun.compose(type::cast));
      return this;
    }
    public <T extends U> R call(T receiver) {
      return map.getOrDefault(receiver.getClass(),
              obj -> { throw new IllegalArgumentException("invalid " + obj); })
          .apply(receiver);
    }
  }

  public static void main(String[] args) {
    Visitor<Vehicle, String> visitor = new Visitor<>();
    visitor.when(Car.class, car -> "car")
           .when(Moto.class, moto -> "moto")
           .when(Toto.class, toto -> "toto"); // doesn't compile :)

    Vehicle vehicle = new Car();
    String text = visitor.call(vehicle);
    visitor.call(new Toto()); // doesn't compile :)
    System.out.println(text);
  }
}

@@ -269,20 +269,22 @@
 c class 
-Toto
+Fruit 
 %7B /* emp
@@ -619,30 +619,16 @@
 blic
- %3CT extends U%3E
  R call(
 T re
@@ -623,17 +623,17 @@
  R call(
-T
+U
  receive
@@ -1014,20 +1014,21 @@
 hen(
-Toto
+Fruit
 .class, 
 toto
@@ -1027,21 +1027,23 @@
 ss, 
-toto -%3E %22toto
+fruit -%3E %22fruit
 %22); 
@@ -1164,12 +1164,13 @@
 new 
-Toto
+Fruit
 ());

Remove unnecessary generics parameter