Replace to(Upper|Lower)Case with Ascii.to$1Case

The presubmits are warning that toUpperCase() and toLowerCase() are locale-specific, and advise using Ascii.toUpperCase() and Ascii.toLowerCase() as a local-invariant alternative.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=127583677
This commit is contained in:
mountford 2016-07-15 15:10:13 -07:00 committed by Ben McIlwain
parent d9596fa30c
commit e72491e59b
22 changed files with 62 additions and 38 deletions

View file

@ -15,6 +15,7 @@
package google.registry.config;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Ascii;
import javax.annotation.Nullable;
/** Registry environments. */
@ -50,7 +51,7 @@ public enum RegistryEnvironment {
/** Returns environment configured by system property {@value #PROPERTY}. */
public static RegistryEnvironment get() {
return valueOf(System.getProperty(PROPERTY, UNITTEST.name()).toUpperCase());
return valueOf(Ascii.toUpperCase(System.getProperty(PROPERTY, UNITTEST.name())));
}
/**

View file

@ -21,6 +21,7 @@ import static org.joda.time.DateTimeZone.UTC;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Text;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Ascii;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSet;
@ -140,7 +141,7 @@ public class DatastoreBackupInfo {
"Status: " + getStatus(),
"Started: " + startTime,
"Ended: " + completeTime.orNull(),
"Duration: " + getRunningTime().toPeriod().toString().substring(2).toLowerCase(),
"Duration: " + Ascii.toLowerCase(getRunningTime().toPeriod().toString().substring(2)),
"GCS: " + gcsFilename.orNull(),
"Kinds: " + kinds,
"");

View file

@ -14,6 +14,7 @@
package google.registry.model.domain.fee;
import com.google.common.base.Ascii;
import com.google.common.base.CharMatcher;
import google.registry.model.ImmutableObject;
import javax.xml.bind.annotation.XmlAttribute;
@ -56,7 +57,7 @@ public class FeeCommandDescriptor extends ImmutableObject {
// Require the xml string to be lowercase.
if (command != null && CharMatcher.javaLowerCase().matchesAllOf(command)) {
try {
return CommandName.valueOf(command.toUpperCase());
return CommandName.valueOf(Ascii.toUpperCase(command));
} catch (IllegalArgumentException e) {
// Swallow this and return UNKNOWN below because there's no matching CommandName.
}

View file

@ -20,6 +20,7 @@ import static com.google.common.io.BaseEncoding.base16;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import com.google.common.base.Ascii;
import com.google.common.base.CharMatcher;
import com.google.common.base.Optional;
import com.google.common.primitives.Ints;
@ -101,7 +102,7 @@ public class LaunchNotice extends ImmutableObject {
String tcnId = getNoticeId().getTcnId();
checkArgument(tcnId.length() == 27);
int checksum = Ints.fromByteArray(base16().decode(tcnId.substring(0, 8).toUpperCase()));
int checksum = Ints.fromByteArray(base16().decode(Ascii.toUpperCase(tcnId.substring(0, 8))));
String noticeId = tcnId.substring(8);
checkArgument(CharMatcher.inRange('0', '9').matchesAllOf(noticeId));

View file

@ -14,6 +14,7 @@
package google.registry.rde;
import com.google.common.base.Ascii;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSet;
import com.googlecode.objectify.Ref;
@ -280,7 +281,7 @@ final class DomainResourceToXjcConverter {
private static XjcDomainContactType convertDesignatedContact(DesignatedContact model) {
XjcDomainContactType bean = new XjcDomainContactType();
ContactResource contact = model.getContactRef().get();
bean.setType(XjcDomainContactAttrType.fromValue(model.getType().toString().toLowerCase()));
bean.setType(XjcDomainContactAttrType.fromValue(Ascii.toLowerCase(model.getType().toString())));
bean.setValue(contact.getContactId());
return bean;
}

View file

@ -18,6 +18,7 @@ import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.Ascii;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap;
import com.google.re2j.Pattern;
@ -218,7 +219,7 @@ public final class LordnLog implements Iterable<Entry<String, LordnLog.Result>>
// + <Status flag>, whether the LORDN file has been accepted for
// processing by the TMDB. Possible values are "accepted" or
// "rejected".
Status status = Status.valueOf(firstLine.get(4).toUpperCase());
Status status = Status.valueOf(Ascii.toUpperCase(firstLine.get(4)));
// + <Warning flag>, whether the LORDN Log has any warning result
// codes. Possible values are "no-warnings" or "warnings-
@ -229,8 +230,11 @@ public final class LordnLog implements Iterable<Entry<String, LordnLog.Result>>
// processed in the LORDN file.
int dnLines = Integer.parseInt(firstLine.get(6));
int actual = lines.size() - 2;
checkArgument(dnLines == actual,
"Line 1: Number of entries (%d) differs from declaration (%d)", actual, dnLines);
checkArgument(
dnLines == actual,
"Line 1: Number of entries (%s) differs from declaration (%s)",
String.valueOf(actual),
String.valueOf(dnLines));
// Second line contains headers: roid,result-code
checkArgument(lines.get(1).equals("roid,result-code"),
@ -244,8 +248,7 @@ public final class LordnLog implements Iterable<Entry<String, LordnLog.Result>>
"Line %d: Expected 2 elements, found %d", i + 1, currentLine.size()));
String roid = currentLine.get(0);
int code = Integer.parseInt(currentLine.get(1));
Result result = checkNotNull(RESULTS.get(code),
"Line %d: Unknown result code: %d", i, code);
Result result = checkNotNull(RESULTS.get(code), "Line %s: Unknown result code: %s", i, code);
builder.put(roid, result);
}

View file

@ -26,6 +26,7 @@ import static google.registry.tools.CommandUtilities.addHeader;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.common.base.Ascii;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
@ -134,7 +135,7 @@ final class AllocateDomainCommand extends MutatingEppToolCommand {
ImmutableMap.Builder<String, String> contactsMapBuilder = new ImmutableMap.Builder<>();
for (DesignatedContact contact : application.getContacts()) {
contactsMapBuilder.put(
contact.getType().toString().toLowerCase(),
Ascii.toLowerCase(contact.getType().toString()),
contact.getContactRef().get().getForeignKey());
}
LaunchNotice launchNotice = application.getLaunchNotice();

View file

@ -14,6 +14,7 @@
package google.registry.tools;
import com.google.common.base.Ascii;
import com.google.common.base.Strings;
/** Container class for static utility methods. */
@ -25,6 +26,6 @@ class CommandUtilities {
/** Prompts for yes/no input using promptText, defaulting to no. */
static boolean promptForYes(String promptText) {
return System.console().readLine(promptText + " (y/N): ").toUpperCase().startsWith("Y");
return Ascii.toUpperCase(System.console().readLine(promptText + " (y/N): ")).startsWith("Y");
}
}

View file

@ -19,6 +19,7 @@ import static google.registry.util.DomainNameUtils.canonicalizeDomainName;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.common.base.Ascii;
import google.registry.tools.Command.GtechCommand;
import google.registry.util.Idn;
import java.io.IOException;
@ -37,7 +38,7 @@ final class ConvertIdnCommand implements Command, GtechCommand {
public void run() throws IOException {
for (String label : mainParameters) {
if (label.startsWith(ACE_PREFIX)) {
System.out.println(Idn.toUnicode(label.toLowerCase()));
System.out.println(Idn.toUnicode(Ascii.toLowerCase(label)));
} else {
System.out.println(canonicalizeDomainName(label));
}

View file

@ -18,6 +18,7 @@ import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.common.base.Ascii;
import com.google.template.soy.data.SoyMapData;
import google.registry.model.domain.launch.LaunchPhase;
import google.registry.tools.Command.GtechCommand;
@ -53,8 +54,8 @@ final class DomainApplicationInfoCommand extends EppToolCommand implements Gtech
@Override
void initEppToolCommand() {
LaunchPhase launchPhase =
checkArgumentNotNull(LaunchPhase.fromValue(phase.toLowerCase()), "Illegal launch phase.");
LaunchPhase launchPhase = checkArgumentNotNull(
LaunchPhase.fromValue(Ascii.toLowerCase(phase)), "Illegal launch phase.");
setSoyTemplate(
DomainApplicationInfoSoyInfo.getInstance(),

View file

@ -17,6 +17,7 @@ package google.registry.tools;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import com.google.common.base.Ascii;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import google.registry.config.RegistryEnvironment;
@ -61,7 +62,7 @@ enum RegistryToolEnvironment {
* @see #get()
*/
static RegistryToolEnvironment parseFromArgs(String[] args) {
return valueOf(getFlagValue(args, FLAGS).toUpperCase());
return valueOf(Ascii.toUpperCase(getFlagValue(args, FLAGS)));
}
/**

View file

@ -23,6 +23,7 @@ import static google.registry.util.CollectionUtils.isNullOrEmpty;
import static google.registry.util.DomainNameUtils.ACE_PREFIX;
import static java.util.Arrays.asList;
import com.google.common.base.Ascii;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
@ -250,7 +251,7 @@ public class VerifyOteAction implements Runnable, JsonAction {
/** Returns a more human-readable translation of the enum constant. */
String description() {
return this.name().replace('_', ' ').toLowerCase();
return Ascii.toLowerCase(this.name().replace('_', ' '));
}
/** An {@link EppInput} might match multiple actions, so check if this action matches. */

View file

@ -18,6 +18,7 @@ import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import com.google.common.base.Ascii;
import com.google.common.base.CharMatcher;
import com.google.common.base.Function;
import com.google.common.base.Functions;
@ -30,6 +31,7 @@ import com.google.common.collect.Range;
import com.google.re2j.Pattern;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.annotation.Detainted;
@ -560,7 +562,7 @@ public final class FormField<I, O> {
@Nullable
@Override
public String apply(@Nullable String input) {
return input != null ? input.toUpperCase() : null;
return input != null ? input.toUpperCase(Locale.ENGLISH) : null;
}};
private static final Function<String, String> LOWERCASE_FUNCTION =
@ -568,7 +570,7 @@ public final class FormField<I, O> {
@Nullable
@Override
public String apply(@Nullable String input) {
return input != null ? input.toLowerCase() : null;
return input != null ? input.toLowerCase(Locale.ENGLISH) : null;
}};
private static final Function<Object, Object> REQUIRED_FUNCTION =
@ -587,8 +589,8 @@ public final class FormField<I, O> {
@Nullable
@Override
public Object apply(@Nullable Object input) {
return input instanceof CharSequence && ((CharSequence) input).length() == 0
|| input instanceof Collection && ((Collection<?>) input).isEmpty()
return ((input instanceof CharSequence) && (((CharSequence) input).length() == 0))
|| ((input instanceof Collection) && ((Collection<?>) input).isEmpty())
? null : input;
}};
@ -709,7 +711,7 @@ public final class FormField<I, O> {
@Override
public C apply(@Nullable O input) {
try {
return input != null ? Enum.valueOf(enumClass, ((String) input).toUpperCase()) : null;
return input != null ? Enum.valueOf(enumClass, Ascii.toUpperCase((String) input)) : null;
} catch (IllegalArgumentException e) {
throw new FormFieldException(
String.format("Enum %s does not contain '%s'", enumClass.getSimpleName(), input));

View file

@ -19,6 +19,7 @@ import static com.google.common.collect.Range.atMost;
import static com.google.common.collect.Range.closed;
import static google.registry.util.DomainNameUtils.canonicalizeDomainName;
import com.google.common.base.Ascii;
import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
@ -354,7 +355,7 @@ public final class RegistrarFormFields {
}
for (String state : stateField.extractUntyped(args).asSet()) {
if ("US".equals(countryCode)) {
state = state.toUpperCase();
state = Ascii.toUpperCase(state);
if (!StateCode.US_MAP.containsKey(state)) {
throw new FormFieldException(stateField, "Unknown US state code.");
}

View file

@ -57,8 +57,8 @@ public final class Idn {
}
/**
* Translates a string from Unicode to ASCII Compatible Encoding (ACE), as defined by the ToASCII
* operation of <a href="http://www.ietf.org/rfc/rfc3490.txt">RFC 3490</a>.
* Translates a string from ASCII Compatible Encoding (ACE) to Unicode, as defined by the
* ToUnicode operation of <a href="http://www.ietf.org/rfc/rfc3490.txt">RFC 3490</a>.
*
* <p>This method always uses <a href="http://unicode.org/reports/tr46/">UTS46 transitional
* processing</a>.

View file

@ -16,11 +16,13 @@ package google.registry.util;
import static com.google.common.base.CharMatcher.javaLetterOrDigit;
import com.google.common.base.Ascii;
/** Utilities for working with {@code Registrar} objects. */
public class RegistrarUtils {
/** Strip out anything that isn't a letter or digit, and lowercase. */
public static String normalizeRegistrarName(String name) {
return javaLetterOrDigit().retainFrom(name).toLowerCase();
return Ascii.toLowerCase(javaLetterOrDigit().retainFrom(name));
}
/**
@ -29,6 +31,6 @@ public class RegistrarUtils {
* in Datastore, and is suitable for use in email addresses.
*/
public static String normalizeClientId(String clientId) {
return clientId.toLowerCase().replaceAll("[^a-z0-9\\-]", "");
return Ascii.toLowerCase(clientId).replaceAll("[^a-z0-9\\-]", "");
}
}

View file

@ -26,6 +26,7 @@ import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.appengine.api.urlfetch.HTTPHeader;
import com.google.appengine.api.urlfetch.HTTPRequest;
import com.google.appengine.api.urlfetch.HTTPResponse;
import com.google.common.base.Ascii;
import com.google.common.base.Optional;
import com.google.common.net.MediaType;
import java.security.NoSuchAlgorithmException;
@ -49,9 +50,9 @@ public final class UrlFetchUtils {
}
private static Optional<String> getHeaderFirstInternal(Iterable<HTTPHeader> hdrs, String name) {
name = name.toLowerCase();
name = Ascii.toLowerCase(name);
for (HTTPHeader header : hdrs) {
if (header.getName().toLowerCase().equals(name)) {
if (Ascii.toLowerCase(header.getName()).equals(name)) {
return Optional.of(header.getValue());
}
}