Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / search / checkers / GetIndexesRequestChecker.java
blob7fde48417787ec53a2580898cf7ff1cfc53bcaf1
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.apphosting.api.AppEngineInternal;
8 import com.google.common.base.Strings;
10 /**
11 * Checks values of {@link com.google.appengine.api.search.GetIndexesRequest}.
14 @AppEngineInternal
15 public class GetIndexesRequestChecker {
17 /**
18 * Checks whether the number of indexes to return is between 1 and the
19 * maximum.
21 * @param limit the maximum number of indexes to return in list results
22 * @return the checked number of indexes to return
23 * @throws IllegalArgumentException if the number of indexes to return
24 * is out of range
26 public static int checkLimit(int limit) {
27 Preconditions.checkArgument(limit >= 1 && limit <= SearchApiLimits.GET_INDEXES_MAXIMUM_LIMIT,
28 String.format("The limit %d must be between 1 and %d",
29 limit, SearchApiLimits.GET_INDEXES_MAXIMUM_LIMIT));
30 return limit;
33 /**
34 * Checks whether the offset of the first indexes to return is between 0 and the
35 * maximum.
37 * @param offset the offset of the first index to return in list results
38 * @return the checked offset of the first index to return
39 * @throws IllegalArgumentException if the offset of the first index to return
40 * is out of range
42 public static int checkOffset(int offset) {
43 Preconditions.checkArgument(offset >= 0 && offset <= SearchApiLimits.GET_INDEXES_MAXIMUM_OFFSET,
44 String.format("The limit %d must be between 1 and %d",
45 offset, SearchApiLimits.GET_INDEXES_MAXIMUM_OFFSET));
46 return offset;
49 /**
50 * Checks whether the given index name prefix is legal. This method uses the same
51 * checks as {@link IndexChecker#checkName(String)}.
53 * @param indexNamePrefix the index name prefix to be checked
54 * @return the checked index name prefix
55 * @throws IllegalArgumentException if the index name prefix is illegal.
57 public static String checkIndexNamePrefix(String indexNamePrefix) {
58 if (Strings.isNullOrEmpty(indexNamePrefix)) {
59 return indexNamePrefix;
61 return IndexChecker.checkName(indexNamePrefix);
64 /**
65 * Checks whether the given start index name is legal. This method uses the same
66 * checks as {@link IndexChecker#checkName(String)}.
68 * @param startIndexName the name of the first index to be returned
69 * @return the checked start index name
70 * @throws IllegalArgumentException if the start index name is illegal.
72 public static String checkStartIndexName(String startIndexName) {
73 if (Strings.isNullOrEmpty(startIndexName)) {
74 return startIndexName;
76 return IndexChecker.checkName(startIndexName);
79 /**
80 * Ensures the given protocol buffer parameters are valid. If any of the parameters
81 * fail to pass the checks, this method throws {@link IllegalArgumentException}. If
82 * everything is valid the original parameters are returned.
84 * @param params the parameters to be checked for validity
85 * @return the checked parameters for listing indexes
86 * @throws IllegalArgumentException if any of the values are incorrect
88 public static ListIndexesParams checkListIndexesParams(ListIndexesParams params) {
89 if (params.hasLimit()) {
90 checkLimit(params.getLimit());
92 if (params.hasNamespace()) {
93 NamespaceManager.validateNamespace(params.getNamespace());
95 if (params.hasStartIndexName()) {
96 checkStartIndexName(params.getStartIndexName());
98 if (params.hasIndexNamePrefix()) {
99 checkIndexNamePrefix(params.getIndexNamePrefix());
101 if (params.hasOffset()) {
102 checkOffset(params.getOffset());
104 return params;