1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.ning.metrics.goodwill.access;
18
19 import com.google.common.collect.ImmutableMap;
20 import org.codehaus.jackson.JsonGenerationException;
21 import org.codehaus.jackson.annotate.JsonCreator;
22 import org.codehaus.jackson.annotate.JsonProperty;
23 import org.codehaus.jackson.annotate.JsonValue;
24 import org.codehaus.jackson.map.ObjectMapper;
25
26 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.Comparator;
31 import java.util.HashMap;
32 import java.util.List;
33
34
35
36
37
38
39
40 public class GoodwillSchema
41 {
42 private static final ObjectMapper mapper = new ObjectMapper();
43
44 private final String name;
45 private String sinkAddInfo;
46 private final HashMap<Short, GoodwillSchemaField> thriftItems = new HashMap<Short, GoodwillSchemaField>();
47
48 public static final String JSON_THRIFT_TYPE_NAME = "name";
49 public static final String JSON_THRIFT_TYPE_SCHEMA = "schema";
50 public static final String JSON_THRIFT_TYPE_SINK_ADD_INFO = "sinkAddInfo";
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102 @JsonCreator
103 public GoodwillSchema(
104 @JsonProperty(JSON_THRIFT_TYPE_NAME) final String name,
105 @JsonProperty(JSON_THRIFT_TYPE_SCHEMA) final List<GoodwillSchemaField> items,
106 @JsonProperty(JSON_THRIFT_TYPE_SINK_ADD_INFO) final String sinkAddInfo
107 )
108 {
109 this(name, items);
110 setSinkAddInfo(sinkAddInfo);
111 }
112
113
114
115
116
117
118
119 public GoodwillSchema(final String name, final List<GoodwillSchemaField> items)
120 {
121 this.name = name;
122 for (final GoodwillSchemaField field : items) {
123 addThriftField(field);
124 }
125 }
126
127 public static GoodwillSchema decode(final String thriftJson) throws IOException
128 {
129 return mapper.readValue(thriftJson, GoodwillSchema.class);
130 }
131
132 @JsonValue
133 @SuppressWarnings({"unchecked"})
134 public ImmutableMap toMap()
135 {
136 return new ImmutableMap.Builder()
137 .put(JSON_THRIFT_TYPE_NAME, getName())
138 .put(JSON_THRIFT_TYPE_SCHEMA, getSchema())
139 .put(JSON_THRIFT_TYPE_SINK_ADD_INFO, sinkAddInfo == null ? "" : sinkAddInfo)
140 .build();
141 }
142
143
144
145
146
147
148 public void addThriftField(final GoodwillSchemaField goodwillSchemaField)
149 {
150 thriftItems.put(goodwillSchemaField.getId(), goodwillSchemaField);
151 }
152
153 public String getName()
154 {
155 return name;
156 }
157
158
159
160
161
162
163
164 public ArrayList<GoodwillSchemaField> getSchema()
165 {
166 final ArrayList<GoodwillSchemaField> items = new ArrayList<GoodwillSchemaField>(thriftItems.values());
167
168 Collections.sort(items, new Comparator<GoodwillSchemaField>()
169 {
170 @Override
171 public int compare(final GoodwillSchemaField left, final GoodwillSchemaField right)
172 {
173 return Short.valueOf(left.getId()).compareTo(right.getId());
174 }
175 });
176
177 return items;
178 }
179
180 public void setSinkAddInfo(final String sinkAddInfo)
181 {
182 this.sinkAddInfo = sinkAddInfo;
183 }
184
185
186
187
188
189
190
191 public GoodwillSchemaField getFieldByPosition(final short i)
192 {
193 return thriftItems.get(i);
194 }
195
196
197
198
199
200
201
202 public GoodwillSchemaField getFieldByName(final String name)
203 {
204 for (final GoodwillSchemaField field : thriftItems.values()) {
205 if (field.getName().equals(name)) {
206 return field;
207 }
208 }
209
210 return null;
211 }
212
213 @Override
214 public String toString()
215 {
216 try {
217 return mapper.writeValueAsString(this);
218 }
219 catch (JsonGenerationException e) {
220 return "GoodwillSchema{" +
221 JSON_THRIFT_TYPE_NAME + "='" + getName() + '\'' +
222 ", thriftItems=" + getSchema() +
223 '}';
224 }
225 catch (IOException e) {
226 return "GoodwillSchema{" +
227 JSON_THRIFT_TYPE_NAME + "='" + getName() + '\'' +
228 ", thriftItems=" + getSchema() +
229 '}';
230 }
231 }
232
233 public byte[] toJSONBytes() throws IOException
234 {
235 return mapper.writeValueAsBytes(this);
236 }
237
238
239
240
241 @Deprecated
242 public ByteArrayOutputStream toJSON() throws IOException
243 {
244
245 final byte[] bytes = toJSONBytes();
246 final ByteArrayOutputStream out = new ByteArrayOutputStream(bytes.length);
247 out.write(bytes);
248 return out;
249 }
250 }