Generics w Javie

0

Cześć,
Mam problem z funkcyjnymi interfejsami w Javie.
Mam w zadaniu zrobić kilka takich funkcji, założenie jest takie:

Function <Integer, List<Integer>> funkcjaA;
Function <<List<Integer>, Integer> funckjaB;
Function <Integer, List<String>> funkcjaC;
Function <List<String>, Integer> funckjaD;

Chodzi o to, że można te funkcje wywołać tak:

List<Integer> = obiekt.metoda(funkcjaA);
Integer = obiekt.metoda(funkcjaA, funkcjaB);
List<String> obiekt.metoda(funkcjaA, funkcjaB, funkcjaC);
Integer = obiekt.metoda(funkcjaA, funkcjaB, funkcjaC, funkcjaD);

W zależności od tego jaki będzie argument to ma zwrócić... Niestety nie potrafię sobie z tym poradzić ponieważ nie wiem jak wywołać po sobie te funkcje w metodzie. Czytałem o andThen ale za nic nie przyjmuje mi argumentów...
Ważne jest to że funkcjaA przyjmuje za argument path do pliku czyli, funkcjaA wykonuje działanie na pliku, funkcjaB na wyniku funkcji A, funkcja C na wyniku funkcji B etc... Ważne jest to, że nie muszą być wykonane wszystkie po sobie.

Czy mógłbym prosić o jakąś podpowiedź? Troszeczkę goni mnie czas a ja siedzę i nic z tym nie moge zrobić. Moja metoda wyglada tak:

public <R> R metoda(Function<?, ?> ... tab) {
  R result = null;
  null = ... cos co wykona funkcje z tab;
  return cos; // zwroci to co zwraca ostatnia dodana funkcja w tab
}
0

Możesz tu wkleić treść zdania, a nie interpretację treści?
Być może to, co potrzebujesz, to wywołanie metody map na strumieniu. Ale bez treści to zgadywanie.

1
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Functionals {

    public static Integer cascade(Function<Integer, List<Integer>> funkcjaA, Function<List<Integer>, Integer> funkcjaB, Function<Integer, List<String>> funkcjaC, Function<List<String>, Integer> funckjaD, Integer arg){
        return funkcjaA
                .andThen(funkcjaB)
                .andThen(funkcjaC)
                .andThen(funckjaD)
                .apply(arg);
    }

    public static void main(String[] args) {
        Function<Integer, List<Integer>> funkcjaA = x -> IntStream.of(x).mapToObj(Integer::new).collect(Collectors.toList());
        Function<List<Integer>, Integer> funkcjaB = x -> (Integer) x.stream().mapToInt(Integer::new).sum();
        Function<Integer, List<String>> funkcjaC = x -> IntStream.of(x).mapToObj(Integer::new).map(Object::toString).collect(Collectors.toList());
        Function<List<String>, Integer> funckjaD = x -> x.stream().collect(Collectors.joining()).hashCode();

        Integer arg = 10;
        System.out.println(cascade(funkcjaA, funkcjaB, funkcjaC, funckjaD, arg));
    }
}

Ale to oczywiście nie zadziała dla gołych wildcardów z serii ? bo przecież nie byłoby wiadomo czy argumenty pasują. Mógłbyś ewentualnie cascade dać:

    public static <T, S, R, E, K> K cascade2(Function<T, S> funkcjaA, Function<S, R> funkcjaB, Function<R, E> funkcjaC, Function<E, K> funckjaD, T arg) {
        return funkcjaA
                .andThen(funkcjaB)
                .andThen(funkcjaC)
                .andThen(funckjaD)
                .apply(arg);
    }

1 użytkowników online, w tym zalogowanych: 0, gości: 1