View Javadoc

1   package org.treetank.filelistener.file.data;
2   
3   import java.io.DataInput;
4   import java.io.DataOutput;
5   import java.io.IOException;
6   import java.util.Objects;
7   
8   import org.treetank.api.IMetaEntry;
9   import org.treetank.api.IMetaEntryFactory;
10  import org.treetank.exception.TTIOException;
11  
12  import com.google.common.hash.Funnel;
13  import com.google.common.hash.PrimitiveSink;
14  
15  /**
16   * This factory is used to give the treetank
17   * system knowledge of the MetaEntries this interface module
18   * uses to store the data.
19   * 
20   * @author Andreas Rain
21   * 
22   */
23  public class FilelistenerMetaDataFactory implements IMetaEntryFactory {
24  
25      private final static int KEY = 1;
26      private final static int VALUE = 2;
27  
28      @Override
29      public IMetaEntry deserializeEntry(DataInput input) throws TTIOException {
30          try {
31              final int kind = input.readInt();
32  
33              switch (kind) {
34              case KEY:
35                  final int valSize = input.readInt();
36                  final byte[] bytes = new byte[valSize];
37                  input.readFully(bytes);
38                  return new MetaKey(new String(bytes));
39              case VALUE:
40                  return new MetaValue(input.readLong());
41              default:
42                  throw new IllegalStateException("Kind not defined.");
43              }
44          } catch (final IOException exc) {
45              throw new TTIOException(exc);
46          }
47      }
48  
49      /**
50       * Key representing a relative path to the mount point.
51       * 
52       * @author Andreas Rain
53       * 
54       */
55      public static class MetaKey implements IMetaEntry {
56          /**
57           * Enum for MetaKeyFunnel.
58           * 
59           * @author Sebastian Graf, University of Konstanz
60           * 
61           */
62          enum MetaKeyFunnel implements Funnel<IMetaEntry> {
63              INSTANCE;
64              public void funnel(IMetaEntry from, PrimitiveSink into) {
65                  MetaKey node = (MetaKey)from;
66                  into.putString(node.mKey);
67              }
68          }
69          
70          /** Key Variable. */
71          private final String mKey;
72  
73          /**
74           * Constructor
75           * 
76           * @param pData
77           *            setting the key
78           */
79          public MetaKey(final String pData) {
80              mKey = pData;
81          }
82  
83          /**
84           * {@inheritDoc}
85           */
86          @Override
87          public void serialize(final DataOutput output) throws TTIOException {
88              try {
89                  output.writeInt(KEY);
90                  output.writeInt(mKey.getBytes().length);
91                  output.write(mKey.getBytes());
92              } catch (final IOException exc) {
93                  throw new TTIOException(exc);
94              }
95          }
96  
97          public String getKey() {
98              return this.mKey;
99          }
100 
101         /**
102          * {@inheritDoc}
103          */
104         @Override
105         public int hashCode() {
106             return Objects.hash(mKey);
107         }
108 
109         /**
110          * {@inheritDoc}
111          */
112         @Override
113         public final boolean equals(final Object pObj) {
114             return this.hashCode() == pObj.hashCode();
115         }
116         
117         /**
118          * {@inheritDoc}
119          */
120         @Override
121         public Funnel<IMetaEntry> getFunnel() {
122             return MetaKeyFunnel.INSTANCE;
123         }
124     }
125 
126     /**
127      * Value which is basically the byte representation
128      * of the header FileData
129      * 
130      * @author Andreas Rain
131      * 
132      */
133     public static class MetaValue implements IMetaEntry {
134         /**
135          * Enum for MetaValueFunnel.
136          * 
137          * @author Sebastian Graf, University of Konstanz
138          * 
139          */
140         enum MetaValueFunnel implements Funnel<IMetaEntry> {
141             INSTANCE;
142             public void funnel(IMetaEntry from, PrimitiveSink into) {
143                 MetaValue node = (MetaValue)from;
144                 into.putLong(node.mData);
145             }
146         }
147         
148         /** Value Variable. */
149         private final long mData;
150 
151         /**
152          * Constructor.
153          * 
154          * @param pData
155          *            setting the value
156          */
157         public MetaValue(final long pData) {
158             mData = pData;
159         }
160 
161         /**
162          * {@inheritDoc}
163          */
164         @Override
165         public void serialize(final DataOutput output) throws TTIOException {
166             try {
167                 output.writeInt(VALUE);
168                 output.writeLong(mData);
169             } catch (final IOException exc) {
170                 throw new TTIOException(exc);
171             }
172         }
173 
174         /**
175          * {@inheritDoc}
176          */
177         @Override
178         public int hashCode() {
179             return Objects.hash(getData());
180         }
181 
182         /**
183          * {@inheritDoc}
184          */
185         @Override
186         public final boolean equals(final Object pObj) {
187             return this.hashCode() == pObj.hashCode();
188         }
189 
190         public long getData() {
191             return mData;
192         }
193         
194         /**
195          * {@inheritDoc}
196          */
197         @Override
198         public Funnel<IMetaEntry> getFunnel() {
199             return MetaValueFunnel.INSTANCE;
200         }
201 
202     }
203 
204 }