Given the following builder setup:
class Factory {
public static <T> FooBuilder<T> fooBuilder() {
return new FooBuilder<>();
}
}
class FooBuilder<T> {
int x;
public FooBuilder<T> addFooStep(int x) {
this.x = x;
return this;
}
public Metric<T> build() {
return new Metric<>(x);
}
}
class Metric<T> {
final int cm;
public Metric(int cm)
{
this.cm = cm;
}
}
Somewhere the type T information is lost. So in order to use this builder, I have to specify the type explicitly like:
Metric<Integer> integerMetric = Factory.<Integer>fooBuilder()
.addFooStep(1)
.build();
However, I would like to be able to use it without the explicity type notation, like:
Metric<Integer> integerMetric = Factory.fooBuilder()
.addFooStep(1)
.build();
Is this possible, and if so, how can it be achieved?