1 package org.treetank.bucket;
2
3 import static com.google.common.base.Objects.toStringHelper;
4
5 import java.io.DataInput;
6 import java.io.DataOutput;
7 import java.io.IOException;
8 import java.util.Objects;
9
10 import org.treetank.api.IMetaEntry;
11 import org.treetank.api.IMetaEntryFactory;
12 import org.treetank.exception.TTIOException;
13
14 import com.google.common.hash.Funnel;
15 import com.google.common.hash.PrimitiveSink;
16
17
18
19
20
21
22
23
24 public class DumbMetaEntryFactory implements IMetaEntryFactory {
25
26
27 private final static int KEY = 1;
28
29 private final static int VALUE = 2;
30
31
32
33
34 @Override
35 public IMetaEntry deserializeEntry(DataInput pData) throws TTIOException {
36 try {
37 final int kind = pData.readInt();
38 switch (kind) {
39 case KEY:
40 return new DumbKey(pData.readLong());
41 case VALUE:
42 return new DumbValue(pData.readLong());
43 default:
44 throw new IllegalStateException("Kind not defined.");
45 }
46 } catch (final IOException exc) {
47 throw new TTIOException(exc);
48 }
49 }
50
51
52
53
54
55
56
57 public static class DumbKey implements IMetaEntry {
58
59
60
61
62
63
64
65 enum DumbKeyFunnel implements Funnel<IMetaEntry> {
66 INSTANCE;
67 public void funnel(IMetaEntry from, PrimitiveSink into) {
68 DumbKey data = (DumbKey)from;
69 into.putLong(data.mData);
70 }
71 }
72
73
74 private final long mData;
75
76
77
78
79
80
81
82 public DumbKey(final long pData) {
83 mData = pData;
84 }
85
86
87
88
89 @Override
90 public void serialize(final DataOutput pOutput) throws TTIOException {
91 try {
92 pOutput.writeInt(KEY);
93 pOutput.writeLong(mData);
94 } catch (final IOException exc) {
95 throw new TTIOException(exc);
96 }
97 }
98
99
100
101
102 @Override
103 public int hashCode() {
104 return Objects.hash(mData);
105 }
106
107
108
109
110 @Override
111 public final boolean equals(final Object pObj) {
112 return this.hashCode() == pObj.hashCode();
113 }
114
115
116
117
118 @Override
119 public String toString() {
120 return toStringHelper(this).add("mData", mData).toString();
121 }
122
123
124
125
126 @Override
127 public Funnel<IMetaEntry> getFunnel() {
128 return DumbKeyFunnel.INSTANCE;
129 }
130 }
131
132
133
134
135
136
137
138 public static class DumbValue implements IMetaEntry {
139
140
141
142
143
144
145
146 enum DumbValueFunnel implements Funnel<IMetaEntry> {
147 INSTANCE;
148 public void funnel(IMetaEntry from, PrimitiveSink into) {
149 DumbValue data = (DumbValue)from;
150 into.putLong(data.mData);
151 }
152 }
153
154
155 private final long mData;
156
157
158
159
160
161
162
163 public DumbValue(final long pData) {
164 mData = pData;
165 }
166
167
168
169
170 @Override
171 public void serialize(final DataOutput pOutput) throws TTIOException {
172 try {
173 pOutput.writeInt(VALUE);
174 pOutput.writeLong(mData);
175 } catch (final IOException exc) {
176 throw new TTIOException(exc);
177 }
178 }
179
180
181
182
183 @Override
184 public int hashCode() {
185 return Objects.hash(mData);
186 }
187
188
189
190
191 @Override
192 public final boolean equals(final Object pObj) {
193 return this.hashCode() == pObj.hashCode();
194 }
195
196
197
198
199 @Override
200 public String toString() {
201 return toStringHelper(this).add("mData", mData).toString();
202 }
203
204
205
206 @Override
207 public Funnel<IMetaEntry> getFunnel() {
208 return DumbValueFunnel.INSTANCE;
209 }
210 }
211
212 }