Type conversion made easy
In strongly typed languages, like Java, conversion between multiple types can be really painful. How many times did you forget how to convert Date
to Calendar
? How about conversion between collections? The operation is often performed in multiple lines of code and in no time you find yourself focusing on the type juggling instead of problem solving.
That’s why we came up with TypeConverter. It’s a utility class that makes type conversion very easy. It’s very fast and extendable.
Overview
To use TypeConverter
execute convert()
method with object and expected type as parameters.
so for example to convert String
to Double
or Date
to Calendar
:
or even List<Integer>
to Integer[]
:
It’s always short and easy.
What conversions are supported
Multiple conversions are supported out of the box. Take a look at the following list:
Boolean
<->Integer
String
<->Number
Number
<->Number
Long
<->java.util.Date
,Long
<->java.util.Calendar
,java.util.Date
<->java.util.Calendar
.Array
<->Collection
Collection
<->Collection
Object
->String
All primitives are also supported so you don’t have to worry about boxing.
Your type is not on the list? You can easily “teach” TypeConverter
new conversion.
Custom converters
To create new converter just create a class that implements Converter
interface, i.e.:
And pass it to the TypeConverter
:
To make it more readable you can use anonymous class:
TypeConverter is also lambda friendly so if you are using java 8 in your project it gets as simple as:
It’s worth to note that our newly registered converter from Object.class to String.class will override any already registered converter that supports the same conversion.
As registering converters is very easy, unregistering them is even easier:
How do I get this?
We’re on Maven Central so just add this to your pom.xml
:
or use any other maven like tool. Or even if you are into old school and coding without any project manager just download the jar
Happy type-converting!