Monday, September 30, 2013

Spring: Inject comma separated values into a List

1. Define your properties in a properties file

gestures=rock, paper, scissors, lizard, spock

2. Ensure that you have your context property placeholder defined in your context xml

<context:property-placeholder location="classpath*:*.properties"/>

3. Finally in your java class, you can inject the value when you define the class member:

 private List<String> gameGestures;

Tuesday, September 17, 2013

Java String.trim() is dead

Ever had problems with spaces that were not removed by String.trim() ? 

This is because trim() only removes breaking white spaces. 

But sometimes, if you are copying from Microsoft products (like Note, for example) some white spaces get copied by default. These whitespaces are non-breaking white spaces, which will not be handled by trim().

To get around this, instead of using trim() use

return CharMatcher.WHITESPACE.trimFrom(" Hello   ");

returns "Hello" regardless of what kind of spaces are involved.

Also note that other java and Apache Commons methods like StringUtils.isBlank() might use only breaking white spaces. That is why I prefer to eschew these and use where ever possible.

For a full list of white space chars, take a peek at