Java Generics

Reference

http://docs.oracle.com/javase/tutorial/java/generics/index.html

Example:

private <T> List<LabelValueBean> convertToLabelValue(List<T> sysConfigs) {
  List<LabelValueBean> results = new ArrayList<LabelValueBean>();
 
  for (T sysConfig : sysConfigs) {
    LabelValueBean bean = new LabelValueBean();
    if (sysConfig instanceof XXX) {
      XXX config = (XXX) sysConfig;
      bean.setLabel(config.getName());
      bean.setValue(config.getValue());
    } else {
      ...
    }
    results.add(bean);
  }
  return results;
}

private static <T extends Enum<T>> T getEnumByString(Class<T> c, String name) {
  try {
    return Enum.valueOf(c, name);
  } catch (IllegalArgumentException e) {
    ...
  }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s