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


声明:本文转载自https://my.oschina.net/greenlaw110/blog/1790380,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

在前面的 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); 

本文发表于2018年04月06日 22:38
(c)注:本文转载自https://my.oschina.net/greenlaw110/blog/1790380,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 1790 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1