OSGL 工具库 - 类型转换的艺术

在前面的 OSGL 工具库之编程艺术系列中我们讲述了

本篇讲述如何使用 OSGL 工具库进行类型转换.

类型转换的 API 使用非常简单:

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

1. 系统内置类型转换

1.1 基本类型转换示例

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"

1.2 hint (转换提示) 的使用

// 这里 hint 的意思是 radix
n = $.convert("FF").hint(16).toInt(); // 255
// 这里 hint  的意思是日期格式
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

1.3 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

1.4 空值的处理

int n = $.convert(null).to(int.class); // 0
n = $.convert(null).to
Integer N = $.convert(null).to(Integer.class); // null
1.4.1 使用默认值
n = $.convert(null).defaultTo(5).toInt(); // 5
n = $.convert("2").defaultTo(5).toInt(); // 2

1.5 通过管道来级联类型转换

// 例1: 不用日期函数来转换日期格式:
// 06/Apr/2018 -> 2018-04-06
s = $.convert("06/Apr/2018")
        .hint("dd/MMM/yyyy")
        .pipeline(Date.class)
        .hint("yyyy-MM-dd")
        .toString(); // 2018-04-06

// 例2: 日期转到byte[]
byte[] ba = $.convert(new Date()).pipeline(String.class).to(byte[].class);
1.5.1 隐式管道

注意, 并没有注册直接的从 Date 到 byte[] 的转换器, OSGL 选择最近转换路径自动转换, 下面的代码的实际转换路径为: Date -> String -> Reader -> InputStream -> byte[]

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

2. 向 OSGL 注册并使用自己的类型转换器

2.1 源类型

public class MyFrom {
    public String id;

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

    @Override
    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;
    }

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

2.2 目标类型


public class MyTo {
    public String id;

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

    @Override
    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;
    }

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

2.3 转换器


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

2.4 注册转换器到 OSGL

TypeConverterRegistry.INSTANCE
    .register(new MyConverter())
    .register(new StringToMyFrom());

2.5 使用自定义转换器

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

// 通过隐式管道转换 String 到 myTo
to = $.convert(s).to(MyTo.class);

转载于:https://my.oschina.net/greenlaw110/blog/1790380

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值