I have a methods:
public List convertBy(Function> flines, Function, String> join, Fu
If you want to create a method which applies multiple functions and is not interested in the intermediate values, you can make it a generic method. The code in your question is strange as it assumes that value
can be a String
and a List<String>
at the same time.
But comparing with your other question, there’s a different picture. While the varargs method there can’t work that way, you can easily provide overloaded methods for the actual use cases:
public class InputConverter<T> {
private T value;
public InputConverter(T value) {
this.value = value;
}
public <R> R convertBy(Function<? super T, ? extends R> f) {
return f.apply(value);
}
public <T1,R> R convertBy(
Function<? super T, ? extends T1> f1, Function<? super T1, ? extends R> f2) {
return f2.apply(f1.apply(value));
}
public <T1,T2,R> R convertBy(
Function<? super T, ? extends T1> f1, Function<? super T1, ? extends T2> f2,
Function<? super T2, ? extends R> f3) {
return f3.apply(f2.apply(f1.apply(value)));
}
public <T1,T2,T3,R> R convertBy(
Function<? super T, ? extends T1> f1, Function<? super T1, ? extends T2> f2,
Function<? super T2, ? extends T3> f3, Function<? super T3, ? extends R> f4) {
return f4.apply(f3.apply(f2.apply(f1.apply(value))));
}
}
Assuming that you fixed your interface types and created functions as described in this answer, you can use it like
InputConverter<String> fileConv=new InputConverter<>("LamComFile.txt");
List<String> lines = fileConv.convertBy(flines);
String text = fileConv.convertBy(flines, join);
List<Integer> ints = fileConv.convertBy(flines, join, collectInts);
Integer sumints = fileConv.convertBy(flines, join, collectInts, sum);