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
.appengine
.api
.search
.Util
;
10 * Checks values of {@link com.google.appengine.api.search.ListIndexesRequest}.
13 public class ListIndexesRequestChecker
{
16 * The maximum offset of the first index returned in list indexes results.
18 public static final int MAXIMUM_OFFSET
= 1000;
21 * The maximum number of indexes that can be requested to be returned in
22 * list indexes results.
24 public static final int MAXIMUM_LIMIT
= 1000;
27 * The default number of indexes we will try to return.
29 public static final int DEFAULT_LIMIT
= 20;
32 * Checks whether the number of indexes to return is between 1 and the
35 * @param limit the maximum number of indexes to return in list results
36 * @return the checked number of indexes to return
37 * @throws IllegalArgumentException if the number of indexes to return
40 public static int checkLimit(int limit
) {
41 Preconditions
.checkArgument(limit
>= 1 && limit
<= MAXIMUM_LIMIT
,
42 String
.format("The limit %d must be between 1 and %d",
43 limit
, MAXIMUM_LIMIT
));
48 * Checks whether the offset of the first indexes to return is between 0 and the
51 * @param offset the offset of the first index to return in list results
52 * @return the checked offset of the first index to return
53 * @throws IllegalArgumentException if the offset of the first index to return
56 public static int checkOffset(int offset
) {
57 Preconditions
.checkArgument(offset
>= 0 && offset
<= MAXIMUM_OFFSET
,
58 String
.format("The limit %d must be between 1 and %d",
59 offset
, MAXIMUM_OFFSET
));
64 * Checks whether the given index name prefix is legal. This method uses the same
65 * checks as {@link IndexChecker#checkName(String)}.
67 * @param indexNamePrefix the index name prefix to be checked
68 * @return the checked index name prefix
69 * @throws IllegalArgumentException if the index name prefix is illegal.
71 public static String
checkIndexNamePrefix(String indexNamePrefix
) {
72 if (Util
.isNullOrEmpty(indexNamePrefix
)) {
73 return indexNamePrefix
;
75 return IndexChecker
.checkName(indexNamePrefix
);
79 * Checks whether the given start index name is legal. This method uses the same
80 * checks as {@link IndexChecker#checkName(String)}.
82 * @param startIndexName the name of the first index to be returned
83 * @return the checked start index name
84 * @throws IllegalArgumentException if the start index name is illegal.
86 public static String
checkStartIndexName(String startIndexName
) {
87 if (Util
.isNullOrEmpty(startIndexName
)) {
88 return startIndexName
;
90 return IndexChecker
.checkName(startIndexName
);
94 * Ensures the given protocol buffer parameters are valid. If any of the parameters
95 * fail to pass the checks, this method throws {@link IllegalArgumentException}. If
96 * everything is valid the original parameters are returned.
98 * @param params the parameters to be checked for validity
99 * @return the checked parameters for listing indexes
100 * @throws IllegalArgumentException if any of the values are incorrect
102 public static ListIndexesParams
checkListIndexesParams(ListIndexesParams params
) {
103 if (params
.hasLimit()) {
104 checkLimit(params
.getLimit());
106 if (params
.hasNamespace()) {
107 NamespaceManager
.validateNamespace(params
.getNamespace());
109 if (params
.hasStartIndexName()) {
110 checkStartIndexName(params
.getStartIndexName());
112 if (params
.hasIndexNamePrefix()) {
113 checkIndexNamePrefix(params
.getIndexNamePrefix());
115 if (params
.hasOffset()) {
116 checkOffset(params
.getOffset());