Convert支持java.time.Duration

This commit is contained in:
Redkale
2019-01-03 09:07:26 +08:00
parent 2a3b8f87d3
commit 1a5e9022ae
2 changed files with 42 additions and 0 deletions

View File

@@ -93,6 +93,7 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
this.register(String.class, StringSimpledCoder.instance);
this.register(CharSequence.class, CharSequenceSimpledCoder.instance);
this.register(java.util.Date.class, DateSimpledCoder.instance);
this.register(java.time.Duration.class, DurationSimpledCoder.instance);
this.register(AtomicInteger.class, AtomicIntegerSimpledCoder.instance);
this.register(AtomicLong.class, AtomicLongSimpledCoder.instance);
this.register(BigInteger.class, BigIntegerSimpledCoder.instance);

View File

@@ -0,0 +1,41 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.redkale.convert.ext;
import java.time.Duration;
import org.redkale.convert.*;
/**
* Duration 的SimpledCoder实现
*
* <p>
* 详情见: https://redkale.org
*
* @author zhangjx
* @param <R> Reader输入的子类型
* @param <W> Writer输出的子类型
*/
public class DurationSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, Duration> {
public static final DurationSimpledCoder instance = new DurationSimpledCoder();
@Override
public void convertTo(W out, Duration value) {
if (value == null) {
out.writeNull();
} else {
out.writeLong(value.toNanos());
}
}
@Override
public Duration convertFrom(R in) {
String value = in.readSmallString();
if (value == null) return null;
return Duration.ofNanos(Long.parseLong(value));
}
}