View Javadoc

1   /**
2    * 
3    */
4   package org.treetank.io.bytepipe;
5   
6   import static com.google.common.base.Objects.toStringHelper;
7   
8   import java.io.InputStream;
9   import java.io.OutputStream;
10  import java.util.ArrayList;
11  import java.util.Iterator;
12  import java.util.List;
13  
14  import org.treetank.exception.TTByteHandleException;
15  import org.treetank.io.bytepipe.IByteHandler.IByteHandlerPipeline;
16  
17  import com.google.inject.Inject;
18  
19  /**
20   * 
21   * Pipeline to handle Bytes before stored in the backends.
22   * 
23   * @author Sebastian Graf, University of Konstanz
24   * 
25   */
26  public final class ByteHandlerPipeline implements IByteHandlerPipeline {
27  
28      /** Pipeline hold over here. */
29      private final List<IByteHandler> mParts;
30  
31      /**
32       * 
33       * Constructor.
34       * 
35       * @param pParts
36       *            to be stored, Order is important!
37       */
38      @Inject
39      public ByteHandlerPipeline(final IByteHandler... pParts) {
40          mParts = new ArrayList<IByteHandler>();
41          for (IByteHandler part : pParts) {
42              mParts.add(part);
43          }
44      }
45  
46      /**
47       * {@inheritDoc}
48       */
49      public OutputStream serialize(final OutputStream pToSerialize) throws TTByteHandleException {
50          OutputStream lastOutput = pToSerialize;
51          for (IByteHandler part : mParts) {
52              lastOutput = part.serialize(lastOutput);
53          }
54          return lastOutput;
55      }
56  
57      /**
58       * {@inheritDoc}
59       */
60      public InputStream deserialize(final InputStream pToDeserialize) throws TTByteHandleException {
61          InputStream lastInput = pToDeserialize;
62          for (IByteHandler part : mParts) {
63              lastInput = part.deserialize(lastInput);
64          }
65          return lastInput;
66      }
67  
68      /**
69       * {@inheritDoc}
70       */
71      @Override
72      public ByteHandlerPipeline clone() {
73          final List<IByteHandler> handlers = new ArrayList<IByteHandler>();
74          for (IByteHandler origHandler : mParts) {
75              handlers.add(origHandler.clone());
76          }
77          return new ByteHandlerPipeline(handlers.toArray(new IByteHandler[handlers.size()]));
78      }
79  
80      /**
81       * {@inheritDoc}
82       */
83      @Override
84      public Iterator<IByteHandler> iterator() {
85          return mParts.iterator();
86      }
87  
88      /**
89       * {@inheritDoc}
90       */
91      @Override
92      public String toString() {
93          return toStringHelper(this).add("mParts", mParts).toString();
94      }
95  
96  }