This is a simplified version of the code in question, one generic class uses another class with generic type parameters and needs to pass one of the generic types to a metho
If you're after a fluent-type interface, you could try the builder pattern. Not as concise as varargs but it is type safe.
A static generically-typed method can eliminate some of the boilerplate when using the builder, while retaining the type safety.
The builder
public class ArgBuilder implements Iterable {
private final List args = new ArrayList();
public ArgBuilder and(T arg) {
args.add(arg);
return this;
}
@Override
public Iterator iterator() {
return args.iterator();
}
public static ArgBuilder with(T firstArgument) {
return new ArgBuilder().and(firstArgument);
}
}
Using it
import static com.example.ArgBuilder.*;
public class VarargsTest {
public static void main(String[] args) {
doSomething(new ArgBuilder().and("foo").and("bar").and("baz"));
// or
doSomething(with("foo").and("bar").and("baz"));
}
static void doSomething(Iterable args) {
for (String arg : args) {
System.out.println(arg);
}
}
}