1 package com
.google
.appengine
.api
.search
;
3 import com
.google
.appengine
.api
.search
.checkers
.Preconditions
;
4 import com
.google
.common
.collect
.ImmutableList
;
6 import java
.io
.Serializable
;
7 import java
.util
.ArrayList
;
11 * Represents a facet result computed from an extended search result set. A facet result contains
12 * a name, a type, and a set of values. Name is a single facet name and each value has a label and
13 * a count. The value label can be a single facet value name, or a range label
14 * (in "[start,end)" format).
16 public final class FacetResult
implements Serializable
{
18 private static final long serialVersionUID
= 1222792844480713320L;
20 private static final int MAX_VALUE_TO_STRING
= 10;
23 * A builder of facet result. This is not thread-safe.
25 public static final class Builder
{
26 private final List
<FacetResultValue
> values
= new ArrayList
<>();
30 * Constructs a builder for a facet result.
36 * Sets the name of this facet result that is a single facet name.
38 * @param name The name of the facet for this facet result.
39 * @return this builder
40 * @throws NullPointerException if the name is null.
41 * @throws IllegalArgumentException if the name is empty.
43 public Builder
setName(String name
) {
44 Preconditions
.checkNotNull(name
, "name cannot be null");
45 Preconditions
.checkArgument(!name
.isEmpty(), "name cannot be empty.");
51 * Add a value to this facet result.
53 * @param value the value to add.
54 * @return this builder
56 public Builder
addValue(FacetResultValue value
) {
57 Preconditions
.checkNotNull(value
, "value cannot be null");
63 * Builds a facet result. The builder must at least have a name.
65 * @return the facet result built by this builder
66 * @throws NullPointerException if the name is null.
67 * @throws IllegalArgumentException if the name is empty or null.
69 public FacetResult
build() {
70 return new FacetResult(this);
74 private final ImmutableList
<FacetResultValue
> values
;
75 private final String name
;
78 * Constructs a facet result with the given builder.
80 * @param builder the builder capable of building a facet result
82 private FacetResult(Builder builder
) {
83 values
= ImmutableList
.copyOf(builder
.values
);
89 * The list of facet values computed during search. Each value
90 * result has a label, count, and refinement token.
92 * @return an unmodifiable list of values
94 public List
<FacetResultValue
> getValues() {
99 * Name of this facet result that is a single facet name.
101 * @return name as string
103 public String
getName() {
107 public static FacetResult
.Builder
newBuilder() {
108 return new Builder();
111 private void checkValid() {
112 Preconditions
.checkNotNull(name
, "name cannot be null.");
113 Preconditions
.checkArgument(!name
.isEmpty(), "name cannot be empty.");
117 * Creates a new facet result builder from the given protocol
118 * buffer facet result object. All the content of the result will be copied to the builder.
120 * @param facetResult the facet result protocol buffer to build
121 * a facet result object from
122 * @return the facet result builder initialized from a facet
123 * result protocol buffer
125 static FacetResult
.Builder
newBuilder(SearchServicePb
.FacetResult facetResult
) {
126 FacetResult
.Builder frBuilder
= newBuilder();
127 frBuilder
.setName(facetResult
.getName());
128 for (SearchServicePb
.FacetResultValue value
: facetResult
.getValueList()) {
129 frBuilder
.addValue(FacetResultValue
.withProtoMessage(value
));
135 public String
toString() {
136 return new Util
.ToStringHelper("FacetResult")
137 .addField("name", getName())
138 .addIterableField("values", getValues(), MAX_VALUE_TO_STRING
)