PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/java/src/main/java/org/msgpack/template/CollectionTemplate.java

http://github.com/msgpack/msgpack
Java | 93 lines | 64 code | 10 blank | 19 comment | 12 complexity | 7d7dec083f5a2118be1ac47f436da6fa MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. //
  2. // MessagePack for Java
  3. //
  4. // Copyright (C) 2009-2010 FURUHASHI Sadayuki
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License");
  7. // you may not use this file except in compliance with the License.
  8. // You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. //
  18. package org.msgpack.template;
  19. import java.util.Collection;
  20. import java.util.LinkedList;
  21. import java.io.IOException;
  22. import org.msgpack.MessagePackObject;
  23. import org.msgpack.MessageTypeException;
  24. import org.msgpack.Packer;
  25. import org.msgpack.Template;
  26. import org.msgpack.Unpacker;
  27. public class CollectionTemplate implements Template {
  28. public static void load() { }
  29. private Template elementTemplate;
  30. public CollectionTemplate(Template elementTemplate) {
  31. this.elementTemplate = elementTemplate;
  32. }
  33. @SuppressWarnings("unchecked")
  34. public void pack(Packer pk, Object target) throws IOException {
  35. if (! (target instanceof Collection)) {
  36. if (target == null) {
  37. throw new MessageTypeException(new NullPointerException("target is null."));
  38. }
  39. throw new MessageTypeException("target is not Collection type: " + target.getClass());
  40. }
  41. Collection<Object> collection = (Collection<Object>) target;
  42. pk.packArray(collection.size());
  43. for(Object element : collection) {
  44. elementTemplate.pack(pk, element);
  45. }
  46. }
  47. @SuppressWarnings("unchecked")
  48. public Object unpack(Unpacker pac, Object to) throws IOException, MessageTypeException {
  49. int length = pac.unpackArray();
  50. Collection<Object> c;
  51. if(to == null) {
  52. c = new LinkedList<Object>();
  53. } else {
  54. // TODO: optimize if list is instanceof ArrayList
  55. c = (Collection<Object>) to;
  56. c.clear();
  57. }
  58. for(; length > 0; length--) {
  59. c.add(elementTemplate.unpack(pac, null));
  60. }
  61. return c;
  62. }
  63. @SuppressWarnings("unchecked")
  64. public Object convert(MessagePackObject from, Object to) throws MessageTypeException {
  65. MessagePackObject[] array = from.asArray();
  66. Collection<Object> c;
  67. if(to == null) {
  68. c = new LinkedList<Object>();
  69. } else {
  70. // TODO: optimize if list is instanceof ArrayList
  71. c = (Collection<Object>) to;
  72. c.clear();
  73. }
  74. for(MessagePackObject element : array) {
  75. c.add(elementTemplate.convert(element, null));
  76. }
  77. return c;
  78. }
  79. static {
  80. TemplateRegistry.registerGeneric(Collection.class, new GenericTemplate1(CollectionTemplate.class));
  81. TemplateRegistry.register(Collection.class, new CollectionTemplate(AnyTemplate.getInstance()));
  82. }
  83. }