summaryrefslogtreecommitdiff
path: root/java/src
diff options
context:
space:
mode:
authorfrsyuki <frsyuki@users.sourceforge.jp>2010-10-22 08:31:03 +0900
committerfrsyuki <frsyuki@users.sourceforge.jp>2010-10-22 08:31:03 +0900
commit86043fd87e14690e48fca25d53cfb8be652d009c (patch)
tree7beb3228e8356db29ddc5489ba22fe1014fcf453 /java/src
parentd8aaef4f04e3e22c028a7d6a10755f2a084f3cd2 (diff)
downloadmsgpack-python-86043fd87e14690e48fca25d53cfb8be652d009c.tar.gz
java: adds OptionalTemplate
Diffstat (limited to 'java/src')
-rw-r--r--java/src/main/java/org/msgpack/template/OptionalTemplate.java52
1 files changed, 52 insertions, 0 deletions
diff --git a/java/src/main/java/org/msgpack/template/OptionalTemplate.java b/java/src/main/java/org/msgpack/template/OptionalTemplate.java
new file mode 100644
index 0000000..6be16c1
--- /dev/null
+++ b/java/src/main/java/org/msgpack/template/OptionalTemplate.java
@@ -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);
+ }
+}
+