1 // Copyright 2011 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.api
.search
.checkers
;
5 import com
.google
.apphosting
.api
.AppEngineInternal
;
6 import com
.google
.common
.base
.Strings
;
9 * Checks values of {@link com.google.appengine.api.search.Index Indexes}.
13 public class IndexChecker
{
16 * Checks whether an index name is valid. It must be a ASCII visible
17 * printable string of length between 1 and {@literal
18 * #MAXIMUM_INDEX_NAME_LENGTH}, not start with '!', and not be of
19 * the format __.*__, which are reserved sequences for internal
22 * @param indexName the index name to check
23 * @return the checked index name
24 * @throws IllegalArgumentException if the index name is not valid.
26 public static String
checkName(String indexName
) {
27 Preconditions
.checkArgument(!Strings
.isNullOrEmpty(indexName
), "Index name null or empty");
28 Preconditions
.checkArgument(indexName
.length() <= SearchApiLimits
.MAXIMUM_INDEX_NAME_LENGTH
,
29 "Index name longer than %d characters: %s",
30 SearchApiLimits
.MAXIMUM_INDEX_NAME_LENGTH
, indexName
);
31 Preconditions
.checkArgument(isAsciiVisiblePrintable(indexName
),
32 "Index name must be ASCII visible printable: %s", indexName
);
33 Preconditions
.checkArgument(!isReserved(indexName
),
34 "Index name must not start with !: %s", indexName
);
39 * @return true if all characters are visible ascii printable: that is,
40 * between 33 ('!') and 126 ('~'), inclusive
42 static boolean isAsciiVisiblePrintable(String str
) {
43 for (int i
= 0; i
< str
.length(); ++i
) {
44 if (str
.charAt(i
) < 33 || str
.charAt(i
) > 126) {
52 * @return true if str represents a reserved index name
54 static boolean isReserved(String str
) {
55 return str
.startsWith("!")
56 || (str
.length() > 3 && str
.startsWith("__") && str
.endsWith("__"));