Sunday, May 25, 2008

Avoiding Java generics verbosity

public class GenericsTest { 
static class Model<T> {
public Model(T value) {
}
}

static class TextField<T> {
public TextField(Model<T> model) {
}
}

static <T> TextField<T> createTextField(Model<T> model) {
return new TextField<T>(model);
}

static <T> Model<T> createModel(T value) {
return new Model<T>(value);
}

public static void main(String[] args) {
TextField<String> tf1 = createTextField(createModel("tf1"));
TextField<String> tf2 = createTextField(new Model<String>("tf1"));
TextField<String> tf3 = new TextField<String>(new Model<String>("tf2"));
}
}

No comments: