1 // Copyright 2011 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.api
.search
.checkers
;
5 import com
.google
.appengine
.api
.search
.Util
;
8 * Checks values of {@link com.google.appengine.api.search.Index Indexes}.
11 public class IndexChecker
{
14 * The maximum length for an index name.
16 public static final int MAXIMUM_INDEX_NAME_LENGTH
= 100;
19 * The maximum number of documents allowed per index and delete request.
21 public static final int MAXIMUM_DOCS_PER_REQUEST
= 200;
24 * Checks whether an index name is valid. It must be a ASCII visible
25 * printable string of length between 1 and {@literal
26 * #MAXIMUM_INDEX_NAME_LENGTH}, not start with '!', and not be of
27 * the format __.*__, which are reserved sequences for internal
30 * @param indexName the index name to check
31 * @return the checked index name
32 * @throws IllegalArgumentException if the index name is not valid.
34 public static String
checkName(String indexName
) {
35 Preconditions
.checkArgument(!Util
.isNullOrEmpty(indexName
), "Index name null or empty");
36 Preconditions
.checkArgument(indexName
.length() <= MAXIMUM_INDEX_NAME_LENGTH
,
37 "Index name longer than %d characters: %s", MAXIMUM_INDEX_NAME_LENGTH
, indexName
);
38 Preconditions
.checkArgument(isAsciiVisiblePrintable(indexName
),
39 "indexName must be ASCII visible printable: %s", indexName
);
40 Preconditions
.checkArgument(!isReserved(indexName
),
41 "indexName must not start with !: %s", indexName
);
46 * @return true if all characters are visible ascii printable: that is,
47 * between 33 ('!') and 126 ('~'), inclusive
49 static boolean isAsciiVisiblePrintable(String str
) {
50 for (int i
= 0; i
< str
.length(); ++i
) {
51 if (str
.charAt(i
) < 33 || str
.charAt(i
) > 126) {
59 * @return true if str represents a reserved index name
61 static boolean isReserved(String str
) {
62 return str
.startsWith("!")
63 || (str
.length() > 3 && str
.startsWith("__") && str
.endsWith("__"));