For the method below, is there a better way of building the returned object?
It’s of type <T>
.
The first input
parameter has the same type as the returned object.
The last builder
parameter is what makes me anxious.
It currently points to a static factory method in each T class.
If you wanted to avoid passing the builder
param, how would you handle that?
(I would prefer not to resort to a cast.)
public <T extends FourVector> T transformVector(T input, TransformInto direction, Function<Map<Axis, Double>, T> builder) {
//the core calculation uses matrices, so we convert back and forth like so
Matrix input_matrix = Matrix.asMatrix(input);
Matrix result = Λ(direction.sign()).times(input_matrix);
//I need to build a new T object here; hence the builder param
return builder.apply(asComponents(result));
}