Tuesday, April 24, 2012

PECS - wildcards - generics

PECS - Pruducer extends, Consumer super

Only applies to input parameters.
Do not use wildcard type for return value - doesn't make it more flexible!

List<String> is a subtype of List<? extends Object>
List<Object> is a subtype of List<? super String>

eg.
pushAll(Collection<? extends E> src);
 - src is an E producer; you can push Long to Collection<Number>
popAll(Collection<? super E> dst);
 - dst is an E consumer; you can pop Numbers to Collection<Object>

Both produces and consumes use T.
Neither produces or consumes use ?.

Monday, April 23, 2012

ResourceBundle

I need to read messages_hr.properties file that is in the classpath of web project.

1) Load properties file: 
ResourceBundle bridgeMsgBundle = ResourceBundle.getBundle("messages", new Locale("hr"));

2) Fomat text with parameters:
String text = MessageFormat.format(bridgeMsgBundle.getString("javax.faces.validator.LengthValidator.MAXIMUM"), 15);

In messages_hr.properties: 
javax.faces.validator.LengthValidator.MAXIMUM=* Maksimalan broj znakova je {0}.