I have the following Code which is a boiled-down version of something I've stumbled upon:
public class Transforming
{
static interface MyInterface<T>
{
void consume(T... toConsume);
}
static abstract class Mapper<T> implements MyInterface<String> {
MyInterface<T> delegate;
public Mapper(MyInterface<T> delegateTo)
{
delegate = delegateTo;
}
public void consume(String... transformFrom)
{
T[] array = (T[]) Arrays.stream(transformFrom)
.map(this::transform)
.toArray(); // can't toArray(T[]::new) here!
delegate.consume(array);
}
protected abstract T transform(String toTransform);
}
}
The searches on how to transform streams to arrays fall short obviously since I don't have the resulting type of array at this point, and Java doesn't allow me to create arrays of a generic type...
I do understand the issue, but any input on how to clean code this? AFAICT, my options here are
- change the interface from varargs to List
- the cast I'm using in the code sample
- adding an IntFunction to the Mapper creation
Anything I'm missing? What would be your preference?
Mapper.consumemethod doesn't implement interface because it haveStringinstead ofTin parameter type.MyInterface<String>.