The Converters module adds some string-to-type converters not already present in Google Guice, plus a simple to use module that makes easier the converters registration.
Starting from the version 3.1, each converter contained in the com.googlecode.rocoto.converters package is implemented as a self-binding module easy to install in the Injector as shown below:
import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Injector; ... Injector injector = Guice.createInjector(new XXXConverter(), new YYYConverter(), new AbstractModule() { @Override protected void configure() { this.bindConstant() .annotatedWith(Names.named("charset")) .to("UTF-8"); } });
The com.googlecode.rocoto.converters package comes with default implementations of converers not already included in Google Guice.
Every converter throws runtime exceptions if invalid input are submitted to the conversion process.
Let's show and explain how they work:
Is the converter that converts a java.lang.String representation to a java.math.BigDecimal.
Is the converter that converts a java.lang.String representation to a java.math.BigInteger.
Is the converter that converts a java.lang.String representation to a java.util.BitSet.
String representation is typically a CSV String mixed o chars and numbers, i.e:
a, 123, ~
in the example, a is taken in consideration as a char, 123 as an int, ~ as a char.
Note non numerical fragments with length great than 1 are not allowed!!!
Is the converter that converts a java.lang.String representation to a java.nio.charset.Charset.
Is the converter that converts a java.lang.String representation to a java.util.Currency.
Is the converter that converts a java.lang.String representation to a java.util.Calendar and to java.util.Date.
By default, this converter manages the following ISO Date format representation:
If users need to add new supported date formats, first they have to retrieve the DateConverter, then add a new pattern:
DateConverter dateConverter = module.lookup(Date.class, DateConverter.class); dateConverter.addPattern("EEE, MMM d, ''yy");
If users need to set the java.util.Locale and/or the java.util.TimeZone, first they have to retrieve the DateConverter, then set their preferences:
import java.util.Locale; import java.util.TimeZone; ... DateConverter dateConverter = module.lookup(Date.class, DateConverter.class); dateConverter.setLocale(Locale.getDefault()); dateConverter.setTimeZone(TimeZone.getDefault());
Is the converter that converts a java.lang.String representation to a java.text.DecimalFormat
Is the converter that converts a java.lang.String representation to a java.net.InetAddress
Is the converter that converts a java.lang.String representation to a java.util.Locale
The converter checks first if the input String matches with the pattern languageCode_counrtyCode to create the java.util.Locale otherwise will use the input value as Locale language.
Is the converter that converts a java.lang.String representation to a java.util.regex.Pattern
Is the converter that converts a java.lang.String representation to a java.util.Properties
Note Input string has to match with the pattern key1=value1\nkey2=value2... encoded using the ISO-8859-1 charset, according to java.util.Properties recommendation.
Is the converter that converts a java.lang.String representation to a java.sql.Date, input has to match with yyyy-MM-dd pattern.
Is the converter that converts a java.lang.String representation to a java.sql.Time, input has to match with HH:mm:ss pattern.
Is the converter that converts a java.lang.String representation to a java.sql.Timestamp, input has to match with yyyy-MM-dd HH:mm:ss.fffffffff pattern.
Is the converter that converts a java.lang.String representation to a java.util.TimeZone.
Is the converter that converts a java.lang.String representation to a java.net.URL, supporting the classpath:// pseudo protocol, to load resources from the the class path.
Users that need to load classpath resources, have to specify the full qualified name of the resource. For example, given the class path resource:
com.acme.myapplication.JDBC.properties
following URLs point to the same resource:
classpath://com/acme/myapplication/JDBC.properties classpath:///com/acme/myapplication/JDBC.properties