1.9.30 sync.
[gae.git] / java / src / main / com / google / appengine / api / search / checkers / IndexChecker.java
bloba6b99f73eb196f11a8b4a0bbfae663c378287187
1 // Copyright 2011 Google Inc. All Rights Reserved.
3 package com.google.appengine.api.search.checkers;
5 import com.google.common.base.Strings;
7 /**
8 * Checks values of {@link com.google.appengine.api.search.Index Indexes}.
11 public class IndexChecker {
13 /**
14 * Checks whether an index name is valid. It must be a ASCII visible
15 * printable string of length between 1 and {@literal
16 * #MAXIMUM_INDEX_NAME_LENGTH}, not start with '!', and not be of
17 * the format __.*__, which are reserved sequences for internal
18 * index names.
20 * @param indexName the index name to check
21 * @return the checked index name
22 * @throws IllegalArgumentException if the index name is not valid.
24 public static String checkName(String indexName) {
25 Preconditions.checkArgument(!Strings.isNullOrEmpty(indexName), "Index name null or empty");
26 Preconditions.checkArgument(indexName.length() <= SearchApiLimits.MAXIMUM_INDEX_NAME_LENGTH,
27 "Index name longer than %d characters: %s",
28 SearchApiLimits.MAXIMUM_INDEX_NAME_LENGTH, indexName);
29 Preconditions.checkArgument(isAsciiVisiblePrintable(indexName),
30 "Index name must be ASCII visible printable: %s", indexName);
31 Preconditions.checkArgument(!isReserved(indexName),
32 "Index name must not start with !: %s", indexName);
33 return indexName;
36 /**
37 * @return true if all characters are visible ascii printable: that is,
38 * between 33 ('!') and 126 ('~'), inclusive
40 static boolean isAsciiVisiblePrintable(String str) {
41 for (int i = 0; i < str.length(); ++i) {
42 if (str.charAt(i) < 33 || str.charAt(i) > 126) {
43 return false;
46 return true;
49 /**
50 * @return true if str represents a reserved index name
52 static boolean isReserved(String str) {
53 return str.startsWith("!")
54 || (str.length() > 3 && str.startsWith("__") && str.endsWith("__"));