OSGL (Part 5): The Art of Type Conversion

DZone 's Guide to

OSGL (Part 5): The Art of Type Conversion

Continuing our journey through the OSGL library, let's focus on how to implement type conversion, or bring your own, using this handy tool.

· Java Zone ·
Free Resource

In the previous articles in this OSGL Art of Coding series, we talked about:

In this article, we will about type conversion with the OSGL library.

There is a single API for type conversion in the OSGL library:

Y y = $.convert(x).to(Y.class)

Let's have a look at some sample code.

OSGL Built-In Type Conversion

Primitive Types

String s = "60";
int n = $.convert(s).to(Integer.class); // 60
n = $.convert(s).toInteger(); // 60
n = $.convert(s).toInt(); // 60
s = $.convert(n).toString(); // "60"
s = $.convert(n).to(String.class); // "60"

The Use of hint

// Here `hint` means radix
n = $.convert("FF").hint(16).toInt(); // 255
// Here `hint` means date time format
Date date = $.convert("06 Apr 2018").hint("dd MMM yyyy").toDate(); // 2018-04-06
String dateStr = $.convert(date).hint("yyyy-MM-dd").toString(); // 2018-04-06

Convert to Enum

enum Code {
    AB, bc, Red;
Code code; 
code = $.convert("AB").to(Code.class); // Code.AB
code = $.convert("ab").caseInsensitivie().to(Code.class); // Code.AB

The Processing of null Values

int n = $.convert(null).to(int.class); // 0
n = $.convert(null).to
Integer N = $.convert(null).to(Integer.class); // null

Use of Default Values

n = $.convert(null).defaultTo(5).toInt(); // 5
n = $.convert("2").defaultTo(5).toInt(); // 2

Use Pipelines to Cascade Type Conversions

// 1. use type conversion to transform between date string with different formats
// 06/Apr/2018 -> 2018-04-06
s = $.convert("06/Apr/2018")
        .toString(); // 2018-04-06

// 2. convert date into byte[]
byte[] ba = $.convert(new Date()).pipeline(String.class).to(byte[].class);

Implicit Pipeline

ba = $.convert(new Date()).to(byte[].class);

Note: There is no direct type converter registered to the OSGL library, and the type conversion logic automatically picks up the shortest path among registered type converters to finish the type conversion from Date to byte[]. The actual conversions happening for the above code is:

Date -> String -> Reader -> InputStream -> byte[]

Register Your Own Type Converter to OSGL

Suppose we have a from type defined as below:

public class MyFrom {
    public String id;

    public MyFrom(String id) {
        this.id = id;

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MyFrom myFrom = (MyFrom) o;
        return id != null ? id.equals(myFrom.id) : myFrom.id == null;

    public int hashCode() {
        return id != null ? id.hashCode() : 0;

And now the target type:

public class MyTo {
    public String id;

    public MyTo(String id) {
        this.id = id;

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MyTo MyTo = (MyTo) o;
        return id != null ? id.equals(MyTo.id) : MyTo.id == null;

    public int hashCode() {
        return id != null ? id.hashCode() : 0;

We can define a type converter for MyFrom to MyTo conversion:

// MyFrom -> MyTo
public class MyConverter extends $.TypeConverter<MyFrom, MyTo> {
    public MyTo convert(MyFrom myFrom) {
        return new MyTo(myFrom.id);
// String -> MyFrom
public class MyConverter extends $.TypeConverter<String, MyFrom> {
    public MyFrom convert(String s) {
        return new MyFrom(s);

Now let's register the type converter to the OSGL library:


Now we can use the OSGL type conversion API to do the conversion from MyFrom to MyTo instance:

String s = "abc";
MyFrom from = newMyFrom(s);
MyTo to = $.convert(from).to(MyTo.class);

// implicity pipeline also applied to customized converter
// here `String` -> `MyFrom` -> `MyTo`
to = $.convert(s).to(MyTo.class);


We have demonstrated the handy type conversion tool of OSGL library, touching on:

  1. The simple, elegant API for type conversion.
  2. Use a conversion pipeline to convert from A to Z when there is no direct converter for A to Z.
  3. The implicit pipeline mechanism of the OSGL conversion utility.
  4. Creating and registering your own type converter to OSGL.

To use the OSGL library, add the following dependencies to your pom.xml file:


At the moment, the osgl-tool.version is 1.10.0.

java ,fluent api ,type casting ,type conversion ,osgl ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}