PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/xxlcore/src/xxl/core/io/converters/MultiConverter.java

http://xxl.googlecode.com/
Java | 122 lines | 30 code | 10 blank | 82 comment | 2 complexity | 5ac88743bebe2164541b105bcf6ddf5c MD5 | raw file
  1. /* XXL: The eXtensible and fleXible Library for data processing
  2. Copyright (C) 2000-2011 Prof. Dr. Bernhard Seeger
  3. Head of the Database Research Group
  4. Department of Mathematics and Computer Science
  5. University of Marburg
  6. Germany
  7. This library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 3 of the License, or (at your option) any later version.
  11. This library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with this library; If not, see <http://www.gnu.org/licenses/>.
  17. http://code.google.com/p/xxl/
  18. */
  19. package xxl.core.io.converters;
  20. import java.io.DataInput;
  21. import java.io.DataOutput;
  22. import java.io.IOException;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import xxl.core.functions.Function;
  26. /**
  27. * Converts an object with multiple convertable fields into a byte
  28. * representation and vice versa.
  29. *
  30. * @param <T> the type to be converted.
  31. */
  32. public class MultiConverter<T> extends Converter<T> {
  33. /**
  34. * The converters that is wrapped. The converter must be able to read and
  35. * write uniform objects.
  36. */
  37. protected Converter<Object>[] converters;
  38. /**
  39. * A factory method that is used for creating the object to be read. This
  40. * function will be invoked when the read method is called (even if there
  41. * is an object specified to restore).
  42. */
  43. protected Function<Object, ? extends T> createObject;
  44. /**
  45. * A function which converts the components of the object into an array of
  46. * objects.
  47. */
  48. protected Function<? super T, Object[]> objectToObjectArray;
  49. /**
  50. * Constructs a new converter that wraps the specified converter and uses
  51. * the specified function as factory method.
  52. *
  53. * @param createObject a factory method that is used for initializing the
  54. * object to read when it is not specified.
  55. * @param objectToObjectArray a function which converts the components of
  56. * the object into an array of objects.
  57. * @param converters the converter to be wrapped.
  58. */
  59. public MultiConverter(Function<Object, ? extends T> createObject, Function<? super T, Object[]> objectToObjectArray, Converter... converters) {
  60. this.converters = converters;
  61. this.createObject = createObject;
  62. this.objectToObjectArray = objectToObjectArray;
  63. }
  64. /**
  65. * Reads the state (the attributes) for the specified object from the
  66. * specified data input and returns the restored object.
  67. *
  68. * <p>This implementation calls the read method of the wrapped converter.
  69. * When the specified object is <code>null</code> it is initialized by
  70. * invoking the function (factory method).</p>
  71. *
  72. * @param dataInput the stream to read data from in order to restore the
  73. * object.
  74. * @param object the object to be restored. If the object is
  75. * <code>null</code> it is initialized by invoking the function
  76. * (factory method).
  77. * @return the restored object.
  78. * @throws IOException if I/O errors occur.
  79. */
  80. @Override
  81. public T read(DataInput dataInput, T object) throws IOException {
  82. List<Object> o = new ArrayList<Object>(converters.length);
  83. for (int i = 0; i < converters.length; i++)
  84. o.add(converters[i].read(dataInput));
  85. return createObject.invoke(o);
  86. }
  87. /**
  88. * Writes the state (the attributes) of the specified object to the
  89. * specified data output.
  90. *
  91. * <p>This implementation calls the write method of the wrapped
  92. * converter.</p>
  93. *
  94. * @param dataOutput the stream to write the state (the attributes) of the
  95. * object to.
  96. * @param object the object whose state (attributes) should be written to
  97. * the data output.
  98. * @throws IOException includes any I/O exceptions that may occur.
  99. */
  100. @Override
  101. public void write(DataOutput dataOutput, T object) throws IOException {
  102. Object[] o = objectToObjectArray.invoke(object);
  103. for (int i = 0; i < converters.length; i++)
  104. converters[i].write(dataOutput, o[i]);
  105. }
  106. }