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
17
18
19
20
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
51
52
53
54
55 public static class MetaKey implements IMetaEntry {
56
57
58
59
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
71 private final String mKey;
72
73
74
75
76
77
78
79 public MetaKey(final String pData) {
80 mKey = pData;
81 }
82
83
84
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
103
104 @Override
105 public int hashCode() {
106 return Objects.hash(mKey);
107 }
108
109
110
111
112 @Override
113 public final boolean equals(final Object pObj) {
114 return this.hashCode() == pObj.hashCode();
115 }
116
117
118
119
120 @Override
121 public Funnel<IMetaEntry> getFunnel() {
122 return MetaKeyFunnel.INSTANCE;
123 }
124 }
125
126
127
128
129
130
131
132
133 public static class MetaValue implements IMetaEntry {
134
135
136
137
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
149 private final long mData;
150
151
152
153
154
155
156
157 public MetaValue(final long pData) {
158 mData = pData;
159 }
160
161
162
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
176
177 @Override
178 public int hashCode() {
179 return Objects.hash(getData());
180 }
181
182
183
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
196
197 @Override
198 public Funnel<IMetaEntry> getFunnel() {
199 return MetaValueFunnel.INSTANCE;
200 }
201
202 }
203
204 }