add DateTemplate and BigDecimalTemplate

This commit is contained in:
Watabiki Naoya 2011-04-03 18:39:41 +09:00
parent 324f215316
commit 24a8ee436f
2 changed files with 80 additions and 0 deletions

View File

@ -0,0 +1,40 @@
package org.msgpack.template;
import java.io.IOException;
import java.math.BigDecimal;
import org.msgpack.MessagePackObject;
import org.msgpack.MessageTypeException;
import org.msgpack.Packer;
import org.msgpack.Template;
import org.msgpack.Unpacker;
public class BigDecimalTemplate implements Template {
@Override
public void pack(Packer pk, Object target) throws IOException {
BigDecimal temp = (BigDecimal) target;
pk.packString(temp.toString());
}
@Override
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
String temp = pac.unpackString();
return new BigDecimal(temp);
}
@Override
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
String temp = from.asString();
return new BigDecimal(temp);
}
static public BigDecimalTemplate getInstance() {
return instance;
}
static final BigDecimalTemplate instance = new BigDecimalTemplate();
static {
TemplateRegistry.register(BigDecimal.class, instance);
}
}

View File

@ -0,0 +1,40 @@
package org.msgpack.template;
import java.io.IOException;
import java.util.Date;
import org.msgpack.MessagePackObject;
import org.msgpack.MessageTypeException;
import org.msgpack.Packer;
import org.msgpack.Template;
import org.msgpack.Unpacker;
public class DateTemplate implements Template {
@Override
public void pack(Packer pk, Object target) throws IOException {
Date temp = (Date) target;
pk.packLong(temp.getTime());
}
@Override
public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
Long temp = pac.unpackLong();
return new Date(temp);
}
@Override
public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
Long temp = from.asLong();
return new Date(temp);
}
static public DateTemplate getInstance() {
return instance;
}
static final DateTemplate instance = new DateTemplate();
static {
TemplateRegistry.register(Date.class, instance);
}
}