Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / search / FacetResultValue.java
blob2f00a72d2fe34c32c0991caeaf1e4677e56ac40a
1 package com.google.appengine.api.search;
3 import com.google.appengine.api.search.checkers.Preconditions;
5 import java.io.Serializable;
7 /**
8 * Represents a single facet result value. The value has a label, a count, and a refinementToken.
9 */
10 public final class FacetResultValue implements Serializable {
12 private static final long serialVersionUID = 1171761338331659834L;
14 /**
15 * Creates and returns a facet result value.
17 * @param label The label of the result returned by the backend that is the name of facet
18 * for atom facets and the string "[start,end)" for number facets.
19 * @param count an integer representing how many times this value is repeated in the result.
20 * @param refinementToken the token string for further refinement of the search result. To
21 * combine values for a single facet, add each of them separately to FacetRequest. There
22 * will be a disjunction between refinements for the same facet.
23 * @return an instance of {@link FacetResultValue}.
24 * @throws IllegalArgumentException if label or refinementToken are empty.
26 public static FacetResultValue create(String label, int count, String refinementToken) {
27 return new FacetResultValue(label, count, refinementToken);
30 private final String label;
31 private final int count;
32 private final String refinementToken;
34 /**
35 * Constructs a facet result value.
37 private FacetResultValue(String label, int count, String refinementToken) {
38 Preconditions.checkNotNull(label, "label cannot be null");
39 Preconditions.checkNotNull(refinementToken, "refinementToken cannot be null");
40 Preconditions.checkArgument(!label.isEmpty(), "label cannot be empty");
41 Preconditions.checkArgument(!refinementToken.isEmpty(), "refinementToken cannot be empty");
42 this.label = label;
43 this.count = count;
44 this.refinementToken = refinementToken;
45 checkValid();
48 /**
49 * Returns the label of this facet result value. The value label returned by the backend can be a
50 * single facet value name, or a range label in "[start,end)" format.
52 * @return label as string
54 public String getLabel() {
55 return label;
58 /**
59 * Returns the refinement token for this result value. This token can be used to
60 * filter the result of new searches using this facet value.
62 * @return the refinement token string.
64 public String getRefinementToken() {
65 return refinementToken;
68 /**
69 * Returns the count of the result value, which is an integer representing
70 * how many times this value is repeated in the result for the given facet value or range.
72 * @return the count of this label in facet result.
74 public int getCount() {
75 return count;
78 private void checkValid() {
79 Preconditions.checkState(label != null && !label.isEmpty(), "Label cannot be empty.");
80 Preconditions.checkState(refinementToken != null && !refinementToken.isEmpty(),
81 "Refinement token cannot be empty.");
84 /**
85 * Creates a new facet result value from the given protocol
86 * buffer facet result value object.
88 * @param facetResultValue the facet result value protocol buffer to build
89 * a facet result value object from.
90 * @return the facet result value initialized from a facet result value protocol buffer.
92 static FacetResultValue withProtoMessage(
93 SearchServicePb.FacetResultValue facetResultValue) {
94 return create(
95 facetResultValue.getName(),
96 facetResultValue.getCount(),
97 FacetRefinement.withProtoMessage(facetResultValue.getRefinement()).toTokenString());
100 @Override
101 public String toString() {
102 return new Util.ToStringHelper("FacetResultValue")
103 .addField("label", getLabel())
104 .addField("count", getCount())
105 .addField("refinementToken", getRefinementToken())
106 .finish();