1 // Copyright 2011 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.api
.search
.checkers
;
5 import com
.google
.appengine
.api
.NamespaceManager
;
6 import com
.google
.appengine
.api
.search
.SearchServicePb
.ListIndexesParams
;
7 import com
.google
.common
.base
.Strings
;
10 * Checks values of {@link com.google.appengine.api.search.GetIndexesRequest}.
13 public class GetIndexesRequestChecker
{
16 * Checks whether the number of indexes to return is between 1 and the
19 * @param limit the maximum number of indexes to return in list results
20 * @return the checked number of indexes to return
21 * @throws IllegalArgumentException if the number of indexes to return
24 public static int checkLimit(int limit
) {
25 Preconditions
.checkArgument(limit
>= 1 && limit
<= SearchApiLimits
.GET_INDEXES_MAXIMUM_LIMIT
,
26 String
.format("The limit %d must be between 1 and %d",
27 limit
, SearchApiLimits
.GET_INDEXES_MAXIMUM_LIMIT
));
32 * Checks whether the offset of the first indexes to return is between 0 and the
35 * @param offset the offset of the first index to return in list results
36 * @return the checked offset of the first index to return
37 * @throws IllegalArgumentException if the offset of the first index to return
40 public static int checkOffset(int offset
) {
41 Preconditions
.checkArgument(offset
>= 0 && offset
<= SearchApiLimits
.GET_INDEXES_MAXIMUM_OFFSET
,
42 String
.format("The limit %d must be between 1 and %d",
43 offset
, SearchApiLimits
.GET_INDEXES_MAXIMUM_OFFSET
));
48 * Checks whether the given index name prefix is legal. This method uses the same
49 * checks as {@link IndexChecker#checkName(String)}.
51 * @param indexNamePrefix the index name prefix to be checked
52 * @return the checked index name prefix
53 * @throws IllegalArgumentException if the index name prefix is illegal.
55 public static String
checkIndexNamePrefix(String indexNamePrefix
) {
56 if (Strings
.isNullOrEmpty(indexNamePrefix
)) {
57 return indexNamePrefix
;
59 return IndexChecker
.checkName(indexNamePrefix
);
63 * Checks whether the given start index name is legal. This method uses the same
64 * checks as {@link IndexChecker#checkName(String)}.
66 * @param startIndexName the name of the first index to be returned
67 * @return the checked start index name
68 * @throws IllegalArgumentException if the start index name is illegal.
70 public static String
checkStartIndexName(String startIndexName
) {
71 if (Strings
.isNullOrEmpty(startIndexName
)) {
72 return startIndexName
;
74 return IndexChecker
.checkName(startIndexName
);
78 * Ensures the given protocol buffer parameters are valid. If any of the parameters
79 * fail to pass the checks, this method throws {@link IllegalArgumentException}. If
80 * everything is valid the original parameters are returned.
82 * @param params the parameters to be checked for validity
83 * @return the checked parameters for listing indexes
84 * @throws IllegalArgumentException if any of the values are incorrect
86 public static ListIndexesParams
checkListIndexesParams(ListIndexesParams params
) {
87 if (params
.hasLimit()) {
88 checkLimit(params
.getLimit());
90 if (params
.hasNamespace()) {
91 NamespaceManager
.validateNamespace(params
.getNamespace());
93 if (params
.hasStartIndexName()) {
94 checkStartIndexName(params
.getStartIndexName());
96 if (params
.hasIndexNamePrefix()) {
97 checkIndexNamePrefix(params
.getIndexNamePrefix());
99 if (params
.hasOffset()) {
100 checkOffset(params
.getOffset());