forked from google/gdata-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttributeHistogram.java
More file actions
357 lines (312 loc) · 9.79 KB
/
Copy pathAttributeHistogram.java
File metadata and controls
357 lines (312 loc) · 9.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* Copyright (c) 2006 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.api.gbase.client;
import com.google.gdata.util.common.xml.XmlWriter;
import com.google.gdata.data.Extension;
import com.google.gdata.data.ExtensionProfile;
import com.google.gdata.data.AttributeHelper;
import com.google.gdata.data.ExtensionDescription;
import com.google.gdata.util.ParseException;
import com.google.gdata.util.XmlParser;
import org.xml.sax.Attributes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Histogram information for one attribute, described in
* an entry using attributes in the gm: namespace.
*
* Make sure the current feed is an histogram feed and then get the
* AttributeHistogram for the entry using:
* {@link GoogleBaseEntry#getGoogleBaseMetadata()}.getAttributeHistogram().
*/
@ExtensionDescription.Default(
nsAlias = GoogleBaseNamespaces.GM_ALIAS,
nsUri = GoogleBaseNamespaces.GM_URI,
localName = "attribute")
public class AttributeHistogram implements Extension {
private GoogleBaseAttributeId attributeId;
private int totalValueCount;
private final List<UniqueValue> values = new ArrayList<UniqueValue>();
/**
* Creates an unitialized AttributeHistogram.
*/
public AttributeHistogram() {
}
/**
* Creates and initializes an AttributeHistogram.
*
* @param attributeName attribute name
* @param attributeType attribute type
*/
public AttributeHistogram(String attributeName,
GoogleBaseAttributeType attributeType) {
attributeId = new GoogleBaseAttributeId(attributeName, attributeType);
}
/**
* Creates and initializes an AttributeHistogram.
*
* @param attributeId attribute Id
*/
public AttributeHistogram(GoogleBaseAttributeId attributeId) {
this.attributeId = attributeId;
}
/**
* Gets the name of the attribute this histogram describes.
*
* @return attribute name
*/
public String getAttributeName() {
return attributeId.getName();
}
/**
* Gets the type of the attribute this histogram describes.
*
* @return attribute type
*/
public GoogleBaseAttributeType getAttributeType() {
return attributeId.getType();
}
/**
* Gets the name and type of the attribute this histogram describes.
*
* @return attribute id
*/
public GoogleBaseAttributeId getAttributeId() {
return attributeId;
}
/**
* Gets the total number of values found for this attribute in
* the result set for the query.
*
* This is not the total number of unique values, just the number
* of times this attribute was set.
*
* @return total number of values found for this attribute, always
* {@code >= sum(getValues().getCount())}
*/
public int getTotalValueCount() {
return totalValueCount;
}
/**
* Gets a list of unique values for the attribute and
* the count for these values.
*
* Not all unique values might be available. For some types,
* no values are ever available.
*
* @return a list of values and the number of time they
* were found in the result set, never null
*/
public List<? extends UniqueValue> getValues() {
return values;
}
/**
* Gets a list of unique values for the attribute and the count
* of these values, for values repeated at least a certain number
* of times.
*
* @param minimumCount minimum number of times the value should
* have been encountered in the result set to matter
* @return a list of values and the number of time they
* were found in the result set, never null
*/
public List<? extends UniqueValue> getValues(int minimumCount) {
if (minimumCount <= 0) {
return values;
}
List<UniqueValue> retval = new ArrayList<UniqueValue>(values.size());
for (UniqueValue value : values) {
if (value.getCount() >= minimumCount) {
retval.add(value);
}
}
return retval;
}
/**
* Adds a new value into the histogram.
* @param count number of time the value was found
* @param stringRepresentation
* @exception IllegalArgumentException unless count is greater than 0
* @exception NullPointerException if stringRepresentation is null
*/
public void addValue(int count, String stringRepresentation) {
values.add(new UniqueValue(count, stringRepresentation));
}
/**
* Sets the total value count.
*/
public void setTotalValueCount(int count) {
this.totalValueCount = count;
}
/**
* Sets attribute name and type.
*/
public void setAttributeId(String name, GoogleBaseAttributeType type) {
setAttributeId(new GoogleBaseAttributeId(name, type));
}
/**
* Sets attribute name and type.
*/
public void setAttributeId(GoogleBaseAttributeId attributeId) {
this.attributeId = attributeId;
}
/**
* Generates the XML representation for this tag.
*
* @param w XML writer
* @param extProfile extension profile
* @throws IOException thrown if there was an error writing to the XmlWriter
*/
public void generate(XmlWriter w, ExtensionProfile extProfile)
throws IOException {
if (attributeId == null) {
return;
}
List<XmlWriter.Attribute> attrs = new ArrayList<XmlWriter.Attribute>();
attrs.add(new XmlWriter.Attribute("name", attributeId.getName()));
if (attributeId.getType() != null) {
attrs.add(
new XmlWriter.Attribute("type", attributeId.getType().toString()));
}
if (totalValueCount > 0) {
attrs.add(
new XmlWriter.Attribute("count", Integer.toString(totalValueCount)));
}
w.startElement(GoogleBaseNamespaces.GM, "attribute", attrs, null);
if (values != null) {
w.startRepeatingElement();
for (AttributeHistogram.UniqueValue value : values) {
value.generate(w);
}
w.endRepeatingElement();
}
w.endElement();
}
/**
* Creates a handler for this gdata extension tag.
*
* @param extProfile {@inheritDoc}
* @param namespace {@inheritDoc}
* @param localName {@inheritDoc}
* @param attrs {@inheritDoc}
* @return {@inheritDoc}
* @throws ParseException {@inheritDoc}
* @throws IOException {@inheritDoc}
*/
public XmlParser.ElementHandler getHandler(ExtensionProfile extProfile,
String namespace, String localName, Attributes attrs)
throws ParseException, IOException {
AttributeHelper helper = new AttributeHelper(attrs);
GoogleBaseAttributeType type =
GoogleBaseAttributeType.getInstance(helper.consume("type", true));
setAttributeId(
new GoogleBaseAttributeId(helper.consume("name", true), type));
setTotalValueCount(helper.consumeInteger("count", false, 0));
helper.assertAllConsumed();
return new XmlParser.ElementHandler() {
@Override
public XmlParser.ElementHandler getChildHandler(String namespace,
String localName,
Attributes attrs)
throws ParseException, IOException {
if (namespace.equals(GoogleBaseNamespaces.GM_URI)
&& localName.equals("value")) {
return new AddValueHandler(attrs);
} else {
return super.getChildHandler(namespace, localName, attrs);
}
}
};
}
/**
* A value, as a string, and the number of times the value appears
* in the result set for the current query.
*/
public static class UniqueValue {
private final int count;
private final String value;
/**
* Creates a new UniqueValue object.
*
* @param count number of time the value was found
* @param value the value, as a string
*/
private UniqueValue(int count, String value) {
this.count = count;
this.value = value;
}
/**
* Gets the number of time this specific value was found.
*/
public int getCount() {
return count;
}
/**
* Gets the value itself, as a string.
*
* @return string representation for the value
*/
public String getValueAsString() {
return value;
}
/**
* Gets the value itself, as a string.
*/
public String toString() {
return value;
}
/**
* Generates the XML representation for this tag.
*
* @param w XML writer
*/
void generate(XmlWriter w) throws IOException {
List<XmlWriter.Attribute> attrs = null;
if (count > 0) {
attrs = Collections.singletonList(
new XmlWriter.Attribute("count", Integer.toString(count)));
}
w.simpleElement(GoogleBaseNamespaces.GM, "value", attrs, value);
}
}
/**
* Handles one gm:value tag and use it to add a value into a specific
* {@link com.google.api.gbase.client.AttributeHistogram}.
*/
private class AddValueHandler extends XmlParser.ElementHandler {
private final int count;
/**
* Creates an new handler for a gm:value tag.
*
* @param attrs XML attributes for the gm:value tag
*/
private AddValueHandler(Attributes attrs)
throws IOException, ParseException {
AttributeHelper helper = new AttributeHelper(attrs);
this.count = helper.consumeInteger("count", false, 0);
helper.assertAllConsumed();
}
@Override
public void processEndElement() throws ParseException {
String value = this.value;
if ("".equals(value)) {
value = null;
}
addValue(count, value);
}
}
}