java: adds OptionalTemplate

This commit is contained in:
frsyuki 2010-10-22 08:31:03 +09:00
parent d8aaef4f04
commit 86043fd87e
2 changed files with 53 additions and 1 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.msgpack</groupId>
<artifactId>msgpack</artifactId>
<version>0.4</version>
<version>0.4.0-devel</version>
<description>MessagePack for Java</description>
<name>MessagePack for Java</name>

View File

@ -0,0 +1,52 @@
//
// MessagePack for Java
//
// Copyright (C) 2009-2010 FURUHASHI Sadayuki
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package org.msgpack.template;
import java.util.List;
import java.util.ArrayList;
import java.io.IOException;
import org.msgpack.*;
public class OptionalTemplate implements Template {
private Template elementTemplate;
private Object defaultObject;
public OptionalTemplate(Template elementTemplate) {
this(elementTemplate, null);
}
public OptionalTemplate(Template elementTemplate, Object defaultObject) {
this.elementTemplate = elementTemplate;
this.defaultObject = defaultObject;
}
public Object unpack(Unpacker pac) throws IOException, MessageTypeException {
if(pac.tryUnpackNull()) {
return defaultObject;
}
return elementTemplate.unpack(pac);
}
public Object convert(MessagePackObject from) throws MessageTypeException {
if(from.isNil()) {
return defaultObject;
}
return elementTemplate.convert(from);
}
}