From 1a5e9022aee6f083c281d2b380eac198966488ea Mon Sep 17 00:00:00 2001 From: Redkale <8730487+redkale@users.noreply.github.com> Date: Thu, 3 Jan 2019 09:07:26 +0800 Subject: [PATCH] =?UTF-8?q?Convert=E6=94=AF=E6=8C=81java.time.Duration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/org/redkale/convert/ConvertFactory.java | 1 + .../convert/ext/DurationSimpledCoder.java | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 src/org/redkale/convert/ext/DurationSimpledCoder.java diff --git a/src/org/redkale/convert/ConvertFactory.java b/src/org/redkale/convert/ConvertFactory.java index 4df7e7968..889ccf9a7 100644 --- a/src/org/redkale/convert/ConvertFactory.java +++ b/src/org/redkale/convert/ConvertFactory.java @@ -93,6 +93,7 @@ public abstract class ConvertFactory { 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); diff --git a/src/org/redkale/convert/ext/DurationSimpledCoder.java b/src/org/redkale/convert/ext/DurationSimpledCoder.java new file mode 100644 index 000000000..631d41965 --- /dev/null +++ b/src/org/redkale/convert/ext/DurationSimpledCoder.java @@ -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实现 + * + *

+ * 详情见: https://redkale.org + * + * @author zhangjx + * @param Reader输入的子类型 + * @param Writer输出的子类型 + */ +public class DurationSimpledCoder extends SimpledCoder { + + 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)); + } + +}