Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / search / checkers / IndexChecker.java
blob7569bd554e5a4933da367c4bfddfe65665b5cef7
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;
8 /**
9 * Checks values of {@link com.google.appengine.api.search.Index Indexes}.
12 @AppEngineInternal
13 public class IndexChecker {
15 /**
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
20 * index names.
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);
35 return indexName;
38 /**
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) {
45 return false;
48 return true;
51 /**
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("__"));