1 package com
.google
.appengine
.api
.search
.checkers
;
3 import com
.google
.apphosting
.api
.AppEngineInternal
;
4 import com
.google
.apphosting
.api
.search
.DocumentPb
;
7 * Provides checks for Facet names and values: atom or number.
10 public final class FacetChecker
{
13 * Checks whether a facet name is valid. The facet name length must be
14 * between 1 and {@link SearchApiLimits#MAXIMUM_NAME_LENGTH} inclusive, and it should match
15 * {@link SearchApiLimits#FACET_NAME_PATTERN}.
17 * @param name the facet name to check
18 * @return the checked facet name
19 * @throws IllegalArgumentException if the facet name is null or empty
20 * or is longer than {@literal SearchApiLimits#MAXIMUM_NAME_LENGTH} or it doesn't
21 * match {@literal #FACET_NAME_PATTERN}.
23 public static String
checkFacetName(String name
) {
24 return checkFacetName(name
, "facet name");
28 * Checks whether a facet name is valid. The facet name length must be
29 * between 1 and {@link SearchApiLimits#MAXIMUM_NAME_LENGTH} inclusive, and it should match
30 * {@link SearchApiLimits#FACET_NAME_PATTERN}.
32 * @param name the facet name to check
33 * @param callerContext the caller context used for creating error message in case of a failure.
34 * @return the checked facet name
35 * @throws IllegalArgumentException if the facet name is empty
36 * or is longer than {@literal SearchApiLimits#MAXIMUM_NAME_LENGTH} or it doesn't
37 * match {@literal #FACET_NAME_PATTERN}.
39 public static String
checkFacetName(String name
, String callerContext
) {
40 Preconditions
.checkNotNull(name
, "Name is null");
41 return FieldChecker
.checkFieldName(name
, callerContext
);
45 * Checks whether an atom is valid. An atom can be null or a string between
46 * 1 and {@literal SearchApiLimits.MAXIMUM_ATOM_LENGTH} in length, inclusive.
48 * @param value the atom value to check
49 * @return the checked atom
50 * @throws IllegalArgumentException if atom is too long or too short (i.e. empty)
52 public static String
checkAtom(String value
) {
53 Preconditions
.checkNotNull(value
, "Value is null");
54 FieldChecker
.checkAtom(value
);
55 Preconditions
.checkArgument(!value
.isEmpty(), "Value is empty");
60 * Checks whether a number is valid. A number can be null or a value between
61 * {@link SearchApiLimits#MINIMUM_NUMBER_VALUE} and {@link SearchApiLimits#MAXIMUM_NUMBER_VALUE},
64 * @param value the value to check
65 * @return the checked number
66 * @throws IllegalArgumentException if number is out of range
68 public static Double
checkNumber(Double value
) {
69 return FieldChecker
.checkNumber(value
);
73 * Checks whether a facet value is valid.
75 * @param value the facet value to check
76 * @return the checked facet value
77 * @throws IllegalArgumentException if the facet value type is not recognized or
78 * if the facet value string is not valid based on the type. See {@link #checkNumber}
79 * and {@link #checkAtom}.
81 public static DocumentPb
.FacetValue
checkFacetValue(DocumentPb
.FacetValue value
) {
83 switch (value
.getType()) {
85 checkAtom(value
.getStringValue());
88 checkNumber(Double
.parseDouble(value
.getStringValue()));
91 throw new IllegalArgumentException("Unsupported facet type: " + value
.getType());
97 public static DocumentPb
.Facet
checkValid(DocumentPb
.Facet facet
) {
98 checkFacetName(facet
.getName());
99 checkFacetValue(facet
.getValue());
103 private FacetChecker() {}