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.

Target convertedObject = TypeConverter.convert(objectThatWillBeConverted, Target.class)

so for example to convert String to Double

Double result = TypeConverter.convert("12", Double.class);

or Date to Calendar:

Date now = new Date();
Calendar calendar = TypeConverter.convert(now, Calendar.class);

or even List<Integer> to Integer[]:

List<Integer> integersList = Arrays.asList(1, 2, 3);
Integer[] integersArray = TypeConverter.convert(integersList, Integer[].class);

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.:

class ObjectToStringConverter implements Converter<Object, String> {

   @Override
   public String convert(Object source) {
      return source.toString();
   }
}

And pass it to the TypeConverter:

TypeConverter.registerConverter(Object.class, String.class, new ObjectToStringConverter());

To make it more readable you can use anonymous class:

TypeConverter.registerConverter(Object.class, String.class, new Converter<Object, String>() {
	@Override
	public String convert(Object source) {
		return source.toString();
	}
});

TypeConverter is also lambda friendly so if you are using java 8 in your project it gets as simple as:

// lambda
TypeConverter.registerConverter(Object.class, String.class, source -> source.toString());

// method reference
TypeConverter.registerConverter(Object.class, String.class, Object::toString);

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:

TypeConverter.unregisterConverter(Source.class, Target.class);

How do I get this?

We’re on Maven Central so just add this to your pom.xml:

<dependency>
    <groupId>pl.jsolve</groupId>
    <artifactId>typeconverter</artifactId>
</dependency>

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!


Łukasz Stypka

Java experienced enthusiast, opened for new technologies. Clean code and simple solution fan.

Tomasz Kuryłek

Self-styled and passionate java hipster. Focused on solution that makes user and himself ecstatic..